https://github.com/zheeeng/vite-plugin-shared-modules
Share node_modules in monorepos. Best friend for pnpm's module isolation and module singletons sharing.
https://github.com/zheeeng/vite-plugin-shared-modules
Last synced: 5 months ago
JSON representation
Share node_modules in monorepos. Best friend for pnpm's module isolation and module singletons sharing.
- Host: GitHub
- URL: https://github.com/zheeeng/vite-plugin-shared-modules
- Owner: zheeeng
- Created: 2021-10-06T20:36:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-17T19:47:30.000Z (7 months ago)
- Last Synced: 2024-09-24T09:48:20.833Z (7 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/vite-plugin-shared-modules
- Size: 204 KB
- Stars: 27
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- fucking-awesome-vite - vite-plugin-shared-modules - Share node_modules in monorepos. (Plugins / Framework-agnostic Plugins)
- awesome-vite - vite-plugin-shared-modules - Share node_modules in monorepos. (Plugins / Framework-agnostic Plugins)
README
# vite-plugin-shared-modules
[![Known Vulnerabilities][known-vulnerabilities-image]][known-vulnerabilities-url]
[![Maintainability][maintainability-image]][maintainability-url]
![publish workflow][publish-workflow-image]
[![license][license-image]][license-url]
[![GitHub issues][github-issues-image]][github-issues-url]
![NPM bundle size(minified + gzip)][bundle-size-image][known-vulnerabilities-image]: https://snyk.io/test/github/zheeeng/vite-plugin-shared-modules/badge.svg
[known-vulnerabilities-url]: https://snyk.io/test/github/zheeeng/vite-plugin-shared-modules[maintainability-image]: https://api.codeclimate.com/v1/badges/d3eaf22221bf57742429/maintainability
[maintainability-url]: https://codeclimate.com/github/zheeeng/vite-plugin-shared-modules/maintainability[publish-workflow-image]: https://github.com/zheeeng/vite-plugin-shared-modules/actions/workflows/publish.yml/badge.svg
[license-image]: https://img.shields.io/github/license/mashape/apistatus.svg
[license-url]: https://github.com/zheeeng/vite-plugin-shared-modules/blob/master/LICENSE[github-issues-image]: https://img.shields.io/github/issues/zheeeng/vite-plugin-shared-modules
[github-issues-url]: https://github.com/zheeeng/vite-plugin-shared-modules/issues[bundle-size-image]: https://img.shields.io/bundlephobia/minzip/vite-plugin-shared-modules.svg
[](https://nodei.co/npm/vite-plugin-shared-modules/)
> Share node_modules in monorepos. Best friend for pnpm's module isolation and module singletons sharing.
Use it as simple as:
```ts
// vite.config.ts
import { defineConfig } from 'vite';
import sharedModulesPlugin from 'vite-plugin-shared-modules'
import tsconfigPaths from 'rollup-plugin-tsconfig-paths';export default defineConfig({
plugins: [
sharedModulesPlugin({
packageName: '@monorepo/shared',
}),
// necessary for resolving modules in `node_modules`
tsconfigPaths({
// specify the project's tsconfig.json, which configured paths mapping.
tsConfigPath: join(__dirname, '../../tsconfig.json')
}),
]
});
```then you can import singletons by this way:
```ts
import foo from '@monorepo/shared/foo'
import bar from '@monorepo/shared/bar'
```is equivalent to
```ts
import foo from '@monorepo/shared/node_modules/foo'
import bar from '@monorepo/shared/node_modules/bar'
```moreover for getting type-safe, add tsconfig paths mapping:
```json
// tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@monorepo/shared/*": ["./packages/shared/node_modules/*", "./packages/shared/node_modules/@types/*"]
}
}
}
```the example above we assume the package `@monorepo/shared` is located under `./packages/shared`.
---
## Full Option
The plugin options signatures:
```ts
export type SharedModulesPluginOption = {
packageName: string,
subpath?: string,
nodeModules?: string,
sourceMap?: boolean,
}
```The default options:
```ts
export const defaultSharedModules = {
subpath: '',
nodeModules: 'node_modules',
sourceMap: true,
}
```