diff --git a/README.md b/README.md index c833136..029b035 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A Vite plugin to polyfill Node's Core Modules for browser environments. Supports [`node:` protocol imports](https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#node-imports). -### Why do I need this? +## Why do I need this? ``` Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code. @@ -35,7 +35,9 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills' export default defineConfig({ plugins: [ nodePolyfills({ - // To exclude specific polyfills, add them to this list. + // To add only specific polyfills, add them here. If no option is passed, adds all polyfills + include: ['path'] + // To exclude specific polyfills, add them to this list. Note: if include is provided, this has no effect exclude: [ 'fs', // Excludes the polyfill for `fs` and `node:fs`. ], @@ -51,3 +53,49 @@ export default defineConfig({ ], }) ``` + +### All polyfills + + - If protocolImports is true, also adds node:[module] +```js + 'assert', + 'buffer', + 'child_process', + 'cluster', + 'console', + 'constants', + 'crypto', + 'dgram', + 'dns', + 'domain', + 'events', + 'fs', + 'http', + 'https', + 'http2', + 'module', + 'net', + 'os', + 'path', + 'punycode', + 'process', + 'querystring', + 'readline', + 'repl', + 'stream', + '_stream_duplex', + '_stream_passthrough', + '_stream_readable', + '_stream_transform', + '_stream_writable', + 'string_decoder', + 'sys', + 'timers/promises', + 'timers', + 'tls', + 'tty', + 'url', + 'util', + 'vm', + 'zlib', +``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 9c4aabf..ec0a87b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,17 @@ export type ModuleName = keyof typeof stdLibBrowser export type ModuleNameWithoutNodePrefix = T extends `node:${infer P}` ? P : never export type PolyfillOptions = { + /** + * Includes specific modules. If empty, includes all modules + * @example + * + * ```ts + * nodePolyfills({ + * include: ['fs', 'path'], + * }) + * ``` + */ + include?: ModuleNameWithoutNodePrefix[], /** * @example * @@ -51,6 +62,7 @@ export type PolyfillOptions = { } export type PolyfillOptionsResolved = { + include: ModuleNameWithoutNodePrefix[], exclude: ModuleNameWithoutNodePrefix[], globals: { Buffer: BooleanOrBuildTarget, @@ -112,6 +124,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner') const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8') const optionsResolved: PolyfillOptionsResolved = { + include: [], exclude: [], protocolImports: true, ...options, @@ -123,10 +136,15 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { }, } + const compareExcludedModuleNames = (moduleName: string, excludedName: string) => { + return moduleName === excludedName || moduleName === `node:${excludedName}`; + } + const isExcluded = (name: string) => { - return optionsResolved.exclude.some((excludedName) => { - return name === excludedName || name === `node:${excludedName}` - }) + if (optionsResolved.include.length) { + return !optionsResolved.include.some((excludedName) => compareExcludedModuleNames(name, excludedName)); + }; + return optionsResolved.exclude.some((excludedName) => compareExcludedModuleNames(name, excludedName)); } const toOverride = (name: string): string | void => { diff --git a/test/vite.config.ts b/test/vite.config.ts index 3d241bb..1d39eda 100644 --- a/test/vite.config.ts +++ b/test/vite.config.ts @@ -10,6 +10,7 @@ export default defineConfig({ // react(), vue(), nodePolyfills({ + // include: ['fs', 'path', 'buffer'], exclude: ['fs'], globals: { process: 'build',