Skip to content

Commit db46c3f

Browse files
committed
Allow specific modules to be excluded
1 parent e74bb31 commit db46c3f

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/index.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,34 @@ import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rol
55
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
66
import type { Plugin } from 'vite'
77

8+
export type ModuleName = keyof typeof stdLibBrowser
9+
export type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never
10+
811
interface PolyfillOptions {
9-
protocolImports: boolean
12+
/**
13+
* @example
14+
*
15+
* ```ts
16+
* nodePolyfills({
17+
* exclude: ['fs', 'path']
18+
* })
19+
* ```
20+
*/
21+
exclude: ModuleNameWithoutNodePrefix[],
22+
/**
23+
* @default true
24+
*/
25+
protocolImports: boolean,
26+
}
27+
28+
const isProtocolImport = (name: string) => {
29+
return name.startsWith('node:')
1030
}
1131

1232
/**
1333
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
1434
*
15-
* @example Use it in `vite.config.ts`
35+
* @example
1636
*
1737
* ```ts
1838
* // vite.config.ts
@@ -22,6 +42,8 @@ interface PolyfillOptions {
2242
* export default defineConfig({
2343
* plugins: [
2444
* nodePolyfills({
45+
* // Specific modules that should not be polyfilled.
46+
* exclude: [],
2547
* // Whether to polyfill `node:` protocol imports.
2648
* protocolImports: true,
2749
* }),
@@ -33,27 +55,34 @@ export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin =>
3355
const require = createRequire(import.meta.url)
3456
const globalShimsPath = require.resolve('node-stdlib-browser/helpers/esbuild/shim')
3557
const optionsResolved: PolyfillOptions = {
58+
exclude: [],
3659
protocolImports: true,
3760
// User options take priority.
3861
...options,
3962
}
4063

64+
const isExcluded = (name: string) => {
65+
return optionsResolved.exclude.some((excludedName) => {
66+
return name === excludedName || name === `node:${excludedName}`
67+
})
68+
}
69+
4170
return {
4271
name: 'vite-plugin-node-polyfills',
4372
config: (_config, _env) => {
44-
const polyfills = Object.entries(stdLibBrowser).reduce((included: Record<string, string>, [name, value]) => {
73+
const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {
4574
if (!optionsResolved.protocolImports) {
46-
const isProtocolImport = /^node:/.test(name)
47-
48-
if (isProtocolImport) {
75+
if (isProtocolImport(name)) {
4976
return included
5077
}
5178
}
5279

53-
included[name] = value
80+
if (!isExcluded(name)) {
81+
included[name] = value
82+
}
5483

5584
return included
56-
}, {})
85+
}, {} as Record<ModuleName, string>)
5786

5887
return {
5988
build: {

test/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
66
export default defineConfig({
77
plugins: [
88
nodePolyfills({
9+
exclude: ['fs'],
910
protocolImports: true,
1011
}),
1112
],

0 commit comments

Comments
 (0)