@@ -5,6 +5,7 @@ import stdLibBrowser from 'node-stdlib-browser'
55import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
66import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
77import type { Plugin } from 'vite'
8+ import { compareModuleNames , isEnabled , isNodeProtocolImport , toRegExp , withoutNodeProtocol } from './utils'
89
910export type BuildTarget = 'build' | 'dev'
1011export type BooleanOrBuildTarget = boolean | BuildTarget
@@ -87,28 +88,6 @@ export type PolyfillOptionsResolved = {
8788 protocolImports : boolean ,
8889}
8990
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-
11291/**
11392 * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
11493 *
@@ -160,27 +139,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
160139 } ,
161140 }
162141
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 ) )
170145 }
171- return optionsResolved . exclude . some ( ( excludedName ) => compareExcludedModuleNames ( name , excludedName ) )
146+
147+ return optionsResolved . exclude . some ( ( excludedName ) => compareModuleNames ( moduleName , excludedName ) )
172148 }
173149
174150 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 ) ) {
176152 return 'vite-plugin-node-polyfills/shims/buffer'
177153 }
178154
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 ) ) {
180156 return 'vite-plugin-node-polyfills/shims/global'
181157 }
182158
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 ) ) {
184160 return 'vite-plugin-node-polyfills/shims/process'
185161 }
186162
@@ -189,23 +165,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
189165 }
190166 }
191167
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+
192182 return {
193183 name : 'vite-plugin-node-polyfills' ,
194184 config : ( config , env ) => {
195185 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 > )
209186
210187 return {
211188 build : {
@@ -223,9 +200,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
223200 {
224201 ...inject ( {
225202 // 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' } : { } ) ,
229206 } ) ,
230207 } ,
231208 ] ,
@@ -243,9 +220,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
243220 banner : isDev ? { js : globalShimsBanner } : undefined ,
244221 // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
245222 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' } : { } ) ,
249226 } ,
250227 inject : [
251228 ...globalShimPaths ,
@@ -259,9 +236,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
259236 name : 'vite-plugin-node-polyfills-shims-resolver' ,
260237 setup ( build ) {
261238 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 )
265240
266241 // https://esbuild.github.io/plugins/#on-resolve
267242 build . onResolve ( { filter : globalShimsFilter } , ( ) => {
0 commit comments