vite-plugin-shared-modules
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

vite-plugin-shared-modules

Known Vulnerabilities Maintainability publish workflow license GitHub issues NPM bundle size(minified + gzip)

NPM

Share node_modules in monorepos. Best friend for pnpm's module isolation and module singletons sharing.

Use it as simple as:

// 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:

import foo from '@monorepo/shared/foo'
import bar from '@monorepo/shared/bar'

is equivalent to

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:

// 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:

export type SharedModulesPluginOption = {
  packageName: string,
  subpath?: string,
  nodeModules?: string,
  sourceMap?: boolean,
}

The default options:

export const defaultSharedModules = {
  subpath: '',
  nodeModules: 'node_modules',
  sourceMap: true,
}

Package Sidebar

Install

npm i vite-plugin-shared-modules

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

9.76 kB

Total Files

5

Last publish

Collaborators

  • zheeeng