@@ -5,6 +5,7 @@ import stdLibBrowser from 'node-stdlib-browser'
5
5
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
6
6
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
7
7
import type { Plugin } from 'vite'
8
+ import { compareModuleNames , isEnabled , isNodeProtocolImport , toRegExp , withoutNodeProtocol } from './utils'
8
9
9
10
export type BuildTarget = 'build' | 'dev'
10
11
export type BooleanOrBuildTarget = boolean | BuildTarget
@@ -87,28 +88,6 @@ export type PolyfillOptionsResolved = {
87
88
protocolImports : boolean ,
88
89
}
89
90
90
- const isBuildEnabled = ( value : BooleanOrBuildTarget ) => {
91
- if ( ! value ) return false
92
- if ( value === true ) return true
93
-
94
- return value === 'build'
95
- }
96
-
97
- const isDevEnabled = ( value : BooleanOrBuildTarget ) => {
98
- if ( ! value ) return false
99
- if ( value === true ) return true
100
-
101
- return value === 'dev'
102
- }
103
-
104
- const isProtocolImport = ( name : string ) => {
105
- return name . startsWith ( 'node:' )
106
- }
107
-
108
- const stripNodePrefix = ( name : ModuleName ) : ModuleNameWithoutNodePrefix => {
109
- return name . replace ( / ^ n o d e : / , '' ) as ModuleNameWithoutNodePrefix
110
- }
111
-
112
91
/**
113
92
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
114
93
*
@@ -160,27 +139,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
160
139
} ,
161
140
}
162
141
163
- const compareExcludedModuleNames = ( moduleName : string , excludedName : string ) => {
164
- return moduleName === excludedName || moduleName === `node:${ excludedName } `
165
- }
166
-
167
- const isExcluded = ( name : string ) => {
168
- if ( optionsResolved . include . length ) {
169
- return ! optionsResolved . include . some ( ( excludedName ) => compareExcludedModuleNames ( name , excludedName ) )
142
+ const isExcluded = ( moduleName : ModuleName ) => {
143
+ if ( optionsResolved . include . length > 0 ) {
144
+ return ! optionsResolved . include . some ( ( includedName ) => compareModuleNames ( moduleName , includedName ) )
170
145
}
171
- return optionsResolved . exclude . some ( ( excludedName ) => compareExcludedModuleNames ( name , excludedName ) )
146
+
147
+ return optionsResolved . exclude . some ( ( excludedName ) => compareModuleNames ( moduleName , excludedName ) )
172
148
}
173
149
174
150
const toOverride = ( name : ModuleNameWithoutNodePrefix ) : string | void => {
175
- if ( isDevEnabled ( optionsResolved . globals . Buffer ) && / ^ b u f f e r $ / . test ( name ) ) {
151
+ if ( isEnabled ( optionsResolved . globals . Buffer , 'dev' ) && / ^ b u f f e r $ / . test ( name ) ) {
176
152
return 'vite-plugin-node-polyfills/shims/buffer'
177
153
}
178
154
179
- if ( isDevEnabled ( optionsResolved . globals . global ) && / ^ g l o b a l $ / . test ( name ) ) {
155
+ if ( isEnabled ( optionsResolved . globals . global , 'dev' ) && / ^ g l o b a l $ / . test ( name ) ) {
180
156
return 'vite-plugin-node-polyfills/shims/global'
181
157
}
182
158
183
- if ( isDevEnabled ( optionsResolved . globals . process ) && / ^ p r o c e s s $ / . test ( name ) ) {
159
+ if ( isEnabled ( optionsResolved . globals . process , 'dev' ) && / ^ p r o c e s s $ / . test ( name ) ) {
184
160
return 'vite-plugin-node-polyfills/shims/process'
185
161
}
186
162
@@ -189,23 +165,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
189
165
}
190
166
}
191
167
168
+ const polyfills = ( Object . entries ( stdLibBrowser ) as Array < [ ModuleName , string ] > ) . reduce < Record < ModuleName , string > > ( ( included , [ name , value ] ) => {
169
+ if ( ! optionsResolved . protocolImports ) {
170
+ if ( isNodeProtocolImport ( name ) ) {
171
+ return included
172
+ }
173
+ }
174
+
175
+ if ( ! isExcluded ( name ) ) {
176
+ included [ name ] = toOverride ( withoutNodeProtocol ( name ) ) || value
177
+ }
178
+
179
+ return included
180
+ } , { } as Record < ModuleName , string > )
181
+
192
182
return {
193
183
name : 'vite-plugin-node-polyfills' ,
194
184
config : ( config , env ) => {
195
185
const isDev = env . command === 'serve'
196
- const polyfills = ( Object . entries ( stdLibBrowser ) as Array < [ ModuleName , string ] > ) . reduce < Record < ModuleName , string > > ( ( included , [ name , value ] ) => {
197
- if ( ! optionsResolved . protocolImports ) {
198
- if ( isProtocolImport ( name ) ) {
199
- return included
200
- }
201
- }
202
-
203
- if ( ! isExcluded ( name ) ) {
204
- included [ name ] = toOverride ( stripNodePrefix ( name ) ) || value
205
- }
206
-
207
- return included
208
- } , { } as Record < ModuleName , string > )
209
186
210
187
return {
211
188
build : {
@@ -223,9 +200,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
223
200
{
224
201
...inject ( {
225
202
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
226
- ...( isBuildEnabled ( optionsResolved . globals . Buffer ) ? { Buffer : 'vite-plugin-node-polyfills/shims/buffer' } : { } ) ,
227
- ...( isBuildEnabled ( optionsResolved . globals . global ) ? { global : 'vite-plugin-node-polyfills/shims/global' } : { } ) ,
228
- ...( isBuildEnabled ( optionsResolved . globals . process ) ? { process : 'vite-plugin-node-polyfills/shims/process' } : { } ) ,
203
+ ...( isEnabled ( optionsResolved . globals . Buffer , 'build' ) ? { Buffer : 'vite-plugin-node-polyfills/shims/buffer' } : { } ) ,
204
+ ...( isEnabled ( optionsResolved . globals . global , 'build' ) ? { global : 'vite-plugin-node-polyfills/shims/global' } : { } ) ,
205
+ ...( isEnabled ( optionsResolved . globals . process , 'build' ) ? { process : 'vite-plugin-node-polyfills/shims/process' } : { } ) ,
229
206
} ) ,
230
207
} ,
231
208
] ,
@@ -243,9 +220,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
243
220
banner : isDev ? { js : globalShimsBanner } : undefined ,
244
221
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
245
222
define : {
246
- ...( ( isDev && isDevEnabled ( optionsResolved . globals . Buffer ) ) ? { Buffer : 'Buffer' } : { } ) ,
247
- ...( ( isDev && isDevEnabled ( optionsResolved . globals . global ) ) ? { global : 'global' } : { } ) ,
248
- ...( ( isDev && isDevEnabled ( optionsResolved . globals . process ) ) ? { process : 'process' } : { } ) ,
223
+ ...( ( isDev && isEnabled ( optionsResolved . globals . Buffer , 'dev' ) ) ? { Buffer : 'Buffer' } : { } ) ,
224
+ ...( ( isDev && isEnabled ( optionsResolved . globals . global , 'dev' ) ) ? { global : 'global' } : { } ) ,
225
+ ...( ( isDev && isEnabled ( optionsResolved . globals . process , 'dev' ) ) ? { process : 'process' } : { } ) ,
249
226
} ,
250
227
inject : [
251
228
...globalShimPaths ,
@@ -259,9 +236,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
259
236
name : 'vite-plugin-node-polyfills-shims-resolver' ,
260
237
setup ( build ) {
261
238
for ( const globalShimPath of globalShimPaths ) {
262
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
263
- const escapedGlobalShimPath = globalShimPath . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
264
- const globalShimsFilter = new RegExp ( `^${ escapedGlobalShimPath } $` )
239
+ const globalShimsFilter = toRegExp ( globalShimPath )
265
240
266
241
// https://esbuild.github.io/plugins/#on-resolve
267
242
build . onResolve ( { filter : globalShimsFilter } , ( ) => {
0 commit comments