1
+ import { readFileSync } from 'node:fs'
1
2
import { createRequire } from 'node:module'
2
3
import inject from '@rollup/plugin-inject'
3
4
import stdLibBrowser from 'node-stdlib-browser'
@@ -57,8 +58,6 @@ export type PolyfillOptionsResolved = {
57
58
protocolImports : boolean ,
58
59
}
59
60
60
- const globals = [ 'buffer' , 'global' , 'process' ] . flatMap ( name => [ name , `node:${ name } ` ] )
61
-
62
61
const isBuildEnabled = ( value : BooleanOrBuildTarget ) => {
63
62
if ( ! value ) return false
64
63
if ( value === true ) return true
@@ -108,6 +107,8 @@ const isProtocolImport = (name: string) => {
108
107
export const nodePolyfills = ( options : PolyfillOptions = { } ) : Plugin => {
109
108
const require = createRequire ( import . meta. url )
110
109
const globalShimsPath = require . resolve ( 'vite-plugin-node-polyfills/shims' )
110
+ const globalShimsBannerPath = require . resolve ( 'vite-plugin-node-polyfills/shims/banner' )
111
+ const globalShimsBanner = readFileSync ( globalShimsBannerPath , 'utf-8' )
111
112
const optionsResolved : PolyfillOptionsResolved = {
112
113
exclude : [ ] ,
113
114
protocolImports : true ,
@@ -126,6 +127,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
126
127
} )
127
128
}
128
129
130
+ const toOverride = ( name : string ) : string | void => {
131
+ if ( isDevEnabled ( optionsResolved . globals . Buffer ) && / ^ (?: n o d e : ) ? b u f f e r $ / . test ( name ) ) {
132
+ return require . resolve ( 'buffer-polyfill' )
133
+ }
134
+
135
+ if ( isDevEnabled ( optionsResolved . globals . process ) && / ^ (?: n o d e : ) ? p r o c e s s $ / . test ( name ) ) {
136
+ return require . resolve ( 'process-polyfill' )
137
+ }
138
+ }
139
+
129
140
return {
130
141
name : 'vite-plugin-node-polyfills' ,
131
142
config : ( _config , env ) => {
@@ -138,7 +149,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
138
149
}
139
150
140
151
if ( ! isExcluded ( name ) ) {
141
- included [ name ] = globals . includes ( name ) ? globalShimsPath : value
152
+ included [ name ] = toOverride ( name ) || value
142
153
}
143
154
144
155
return included
@@ -164,19 +175,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
164
175
} ,
165
176
esbuild : {
166
177
// In dev, the global polyfills need to be injected as a banner in order for isolated scripts (such as Vue SFCs) to have access to them.
167
- banner : [
168
- isDev && isDevEnabled ( optionsResolved . globals . Buffer ) ? `import { Buffer as BufferPolyfill } from '${ globalShimsPath } '\nglobalThis.Buffer = BufferPolyfill` : '' ,
169
- isDev && isDevEnabled ( optionsResolved . globals . global ) ? `import { global as globalPolyfill } from '${ globalShimsPath } '\nglobalThis.global = globalPolyfill` : '' ,
170
- isDev && isDevEnabled ( optionsResolved . globals . process ) ? `import { process as processPolyfill } from '${ globalShimsPath } '\nglobalThis.process = processPolyfill` : '' ,
171
- ] . join ( '\n' ) ,
178
+ banner : isDev ? globalShimsBanner : undefined ,
172
179
} ,
173
180
optimizeDeps : {
174
181
esbuildOptions : {
182
+ banner : isDev ? { js : globalShimsBanner } : undefined ,
175
183
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
176
184
define : {
177
- ...( isDevEnabled ( optionsResolved . globals . Buffer ) ? { Buffer : 'Buffer' } : { } ) ,
178
- ...( isDevEnabled ( optionsResolved . globals . global ) ? { global : 'global' } : { } ) ,
179
- ...( isDevEnabled ( optionsResolved . globals . process ) ? { process : 'process' } : { } ) ,
185
+ ...( isDev && isDevEnabled ( optionsResolved . globals . Buffer ) ? { Buffer : 'Buffer' } : { } ) ,
186
+ ...( isDev && isDevEnabled ( optionsResolved . globals . global ) ? { global : 'global' } : { } ) ,
187
+ ...( isDev && isDevEnabled ( optionsResolved . globals . process ) ? { process : 'process' } : { } ) ,
180
188
} ,
181
189
inject : [
182
190
globalShimsPath ,
0 commit comments