Skip to content

Commit f462a2e

Browse files
Merge pull request #37 from leticiavna/24-add-include-config
Feat: add include polyfill option
2 parents ec47b5a + a58e00b commit f462a2e

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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).
44

5-
### Why do I need this?
5+
## Why do I need this?
66

77
```
88
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'
3535
export default defineConfig({
3636
plugins: [
3737
nodePolyfills({
38-
// To exclude specific polyfills, add them to this list.
38+
// To add only specific polyfills, add them here. If no option is passed, adds all polyfills
39+
include: ['path']
40+
// To exclude specific polyfills, add them to this list. Note: if include is provided, this has no effect
3941
exclude: [
4042
'fs', // Excludes the polyfill for `fs` and `node:fs`.
4143
],
@@ -51,3 +53,49 @@ export default defineConfig({
5153
],
5254
})
5355
```
56+
57+
### All polyfills
58+
59+
- If protocolImports is true, also adds node:[module]
60+
```js
61+
'assert',
62+
'buffer',
63+
'child_process',
64+
'cluster',
65+
'console',
66+
'constants',
67+
'crypto',
68+
'dgram',
69+
'dns',
70+
'domain',
71+
'events',
72+
'fs',
73+
'http',
74+
'https',
75+
'http2',
76+
'module',
77+
'net',
78+
'os',
79+
'path',
80+
'punycode',
81+
'process',
82+
'querystring',
83+
'readline',
84+
'repl',
85+
'stream',
86+
'_stream_duplex',
87+
'_stream_passthrough',
88+
'_stream_readable',
89+
'_stream_transform',
90+
'_stream_writable',
91+
'string_decoder',
92+
'sys',
93+
'timers/promises',
94+
'timers',
95+
'tls',
96+
'tty',
97+
'url',
98+
'util',
99+
'vm',
100+
'zlib',
101+
```

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ export type ModuleName = keyof typeof stdLibBrowser
1212
export type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never
1313

1414
export type PolyfillOptions = {
15+
/**
16+
* Includes specific modules. If empty, includes all modules
17+
* @example
18+
*
19+
* ```ts
20+
* nodePolyfills({
21+
* include: ['fs', 'path'],
22+
* })
23+
* ```
24+
*/
25+
include?: ModuleNameWithoutNodePrefix[],
1526
/**
1627
* @example
1728
*
@@ -51,6 +62,7 @@ export type PolyfillOptions = {
5162
}
5263

5364
export type PolyfillOptionsResolved = {
65+
include: ModuleNameWithoutNodePrefix[],
5466
exclude: ModuleNameWithoutNodePrefix[],
5567
globals: {
5668
Buffer: BooleanOrBuildTarget,
@@ -112,6 +124,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
112124
const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner')
113125
const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8')
114126
const optionsResolved: PolyfillOptionsResolved = {
127+
include: [],
115128
exclude: [],
116129
protocolImports: true,
117130
...options,
@@ -123,10 +136,15 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
123136
},
124137
}
125138

139+
const compareExcludedModuleNames = (moduleName: string, excludedName: string) => {
140+
return moduleName === excludedName || moduleName === `node:${excludedName}`;
141+
}
142+
126143
const isExcluded = (name: string) => {
127-
return optionsResolved.exclude.some((excludedName) => {
128-
return name === excludedName || name === `node:${excludedName}`
129-
})
144+
if (optionsResolved.include.length) {
145+
return !optionsResolved.include.some((excludedName) => compareExcludedModuleNames(name, excludedName));
146+
};
147+
return optionsResolved.exclude.some((excludedName) => compareExcludedModuleNames(name, excludedName));
130148
}
131149

132150
const toOverride = (name: string): string | void => {

test/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
// react(),
1111
vue(),
1212
nodePolyfills({
13+
// include: ['fs', 'path', 'buffer'],
1314
exclude: ['fs'],
1415
globals: {
1516
process: 'build',

0 commit comments

Comments
 (0)