Skip to content

Commit 5f89ba1

Browse files
committed
Use globalThis instead of window (and restrict to dev)
1 parent fc9d003 commit 5f89ba1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
128128

129129
return {
130130
name: 'vite-plugin-node-polyfills',
131-
config: (_config, _env) => {
131+
config: (_config, env) => {
132+
const isDev = env.mode === 'development'
132133
const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {
133134
if (!optionsResolved.protocolImports) {
134135
if (isProtocolImport(name)) {
@@ -164,9 +165,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
164165
esbuild: {
165166
// 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.
166167
banner: [
167-
isDevEnabled(optionsResolved.globals.Buffer) ? `import { Buffer as BufferPolyfill } from '${globalShimsPath}'\nwindow.Buffer = BufferPolyfill` : '',
168-
isDevEnabled(optionsResolved.globals.global) ? `import { global as globalPolyfill } from '${globalShimsPath}'\nwindow.global = globalPolyfill` : '',
169-
isDevEnabled(optionsResolved.globals.process) ? `import { process as processPolyfill } from '${globalShimsPath}'\nwindow.process = processPolyfill` : '',
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` : '',
170171
].join('\n'),
171172
},
172173
optimizeDeps: {

0 commit comments

Comments
 (0)