Skip to content

Commit ac1efc9

Browse files
author
Letícia
committed
feat: add include to PolyfillOptions
1 parent ec47b5a commit ac1efc9

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/index.ts

Lines changed: 23 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 => {
@@ -153,6 +171,8 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
153171
return included
154172
}, {} as Record<ModuleName, string>)
155173

174+
console.log({ ArrayNames: Object.keys(polyfills) });
175+
156176
return {
157177
build: {
158178
rollupOptions: {

0 commit comments

Comments
 (0)