Skip to content

Commit 7959ad0

Browse files
committed
Generate a banner for globals in dev
1 parent e70d94f commit 7959ad0

File tree

7 files changed

+122
-15
lines changed

7 files changed

+122
-15
lines changed

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@
2929
"import": "./dist/index.js",
3030
"types": "./dist/index.d.ts"
3131
},
32-
"./shims": "./shims/dist/index.js"
32+
"./shims": {
33+
"require": "./shims/dist/index.cjs",
34+
"import": "./shims/dist/index.js"
35+
},
36+
"./shims/banner": {
37+
"require": "./shims/banner/dist/index.cjs",
38+
"import": "./shims/banner/dist/index.cjs"
39+
}
3340
},
3441
"scripts": {
35-
"build": "run-s build:core build:shims build:types",
42+
"build": "run-s build:core build:shims build:shims:banner build:types",
3643
"build:core": "vite build",
3744
"build:shims": "vite build ./shims",
45+
"build:shims:banner": "vite-node ./shims/banner/build.ts",
3846
"build:types": "tsc",
3947
"test": "run-s test:build test:dev",
4048
"test:build": "vite -c test/vite.config.ts build",
@@ -55,6 +63,7 @@
5563
"rollup": "^3.8.1",
5664
"typescript": "4.8.3",
5765
"vite": "^4.0.4",
66+
"vite-node": "^0.34.1",
5867
"vite-plugin-externalize-deps": "^0.1.5",
5968
"vite-plugin-inspect": "^0.6.0",
6069
"vite-plugin-node-polyfills": "workspace:*",

pnpm-lock.yaml

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shims/banner/build.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { dirname, join } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { build } from 'esbuild'
4+
5+
const thisDir = dirname(fileURLToPath(import.meta.url))
6+
7+
await build({
8+
bundle: true,
9+
entryPoints: [join(thisDir, './index.cjs')],
10+
format: 'iife',
11+
inject: [join(thisDir, '..')],
12+
outdir: join(thisDir, './dist'),
13+
outExtension: { '.js': '.cjs' },
14+
})

shims/banner/index.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
globalThis.Buffer = globalThis.Buffer || Buffer
2+
globalThis.global = globalThis.global || global
3+
globalThis.process = globalThis.process || process

shims/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"type": "module",
33
"sideEffects": false,
44
"exports": {
5-
".": "./dist/index.js"
5+
".": {
6+
"import": "./dist/index.js",
7+
"require": "./dist/index.cjs"
8+
}
69
},
7-
"main": "./dist/index.js",
10+
"main": "./dist/index.cjs",
811
"module": "./dist/index.js"
912
}

shims/vite.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export default defineConfig({
1616
exports: 'named',
1717
format: 'es',
1818
},
19+
{
20+
exports: 'named',
21+
format: 'cjs',
22+
inlineDynamicImports: true,
23+
interop: 'auto',
24+
},
1925
],
2026
},
2127
sourcemap: true,

src/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from 'node:fs'
12
import { createRequire } from 'node:module'
23
import inject from '@rollup/plugin-inject'
34
import stdLibBrowser from 'node-stdlib-browser'
@@ -57,8 +58,6 @@ export type PolyfillOptionsResolved = {
5758
protocolImports: boolean,
5859
}
5960

60-
const globals = ['buffer', 'global', 'process'].flatMap(name => [name, `node:${name}`])
61-
6261
const isBuildEnabled = (value: BooleanOrBuildTarget) => {
6362
if (!value) return false
6463
if (value === true) return true
@@ -108,6 +107,8 @@ const isProtocolImport = (name: string) => {
108107
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
109108
const require = createRequire(import.meta.url)
110109
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')
111112
const optionsResolved: PolyfillOptionsResolved = {
112113
exclude: [],
113114
protocolImports: true,
@@ -126,6 +127,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
126127
})
127128
}
128129

130+
const toOverride = (name: string): string | void => {
131+
if (isDevEnabled(optionsResolved.globals.Buffer) && /^(?:node:)?buffer$/.test(name)) {
132+
return require.resolve('buffer-polyfill')
133+
}
134+
135+
if (isDevEnabled(optionsResolved.globals.process) && /^(?:node:)?process$/.test(name)) {
136+
return require.resolve('process-polyfill')
137+
}
138+
}
139+
129140
return {
130141
name: 'vite-plugin-node-polyfills',
131142
config: (_config, env) => {
@@ -138,7 +149,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
138149
}
139150

140151
if (!isExcluded(name)) {
141-
included[name] = globals.includes(name) ? globalShimsPath : value
152+
included[name] = toOverride(name) || value
142153
}
143154

144155
return included
@@ -164,19 +175,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
164175
},
165176
esbuild: {
166177
// 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,
172179
},
173180
optimizeDeps: {
174181
esbuildOptions: {
182+
banner: isDev ? { js: globalShimsBanner } : undefined,
175183
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
176184
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' } : {}),
180188
},
181189
inject: [
182190
globalShimsPath,

0 commit comments

Comments
 (0)