Skip to content

Commit 0eb6373

Browse files
authored
Use inject plugin instead of esbuild.banner (#152)
* fix: use inject plugin instead of `esbuild.banner` * test: update expected output * fix: incorrect filter
1 parent bef9c95 commit 0eb6373

3 files changed

Lines changed: 43 additions & 90 deletions

File tree

src/index.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
66
import type { Plugin } from 'vite'
77
import { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'
88

9+
type TransformHook = Extract<Plugin['transform'], Function>
10+
911
export type BuildTarget = 'build' | 'dev'
1012
export type BooleanOrBuildTarget = boolean | BuildTarget
1113
export type ModuleName = keyof typeof stdLibBrowser
@@ -130,7 +132,7 @@ const globalShimBanners = {
130132
* })
131133
* ```
132134
*/
133-
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
135+
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
134136
const optionsResolved: PolyfillOptionsResolved = {
135137
include: [],
136138
exclude: [],
@@ -199,7 +201,23 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
199201
``,
200202
].join('\n')
201203

202-
return {
204+
let rawInjectPlugin: Plugin | false | undefined
205+
function transform(this: ThisParameterType<TransformHook>, code: string, id: string, opts: Parameters<TransformHook>[2]) {
206+
if (rawInjectPlugin === undefined) {
207+
throw new Error('transform called before inject plugin initialization')
208+
}
209+
if (rawInjectPlugin === false) {
210+
return
211+
}
212+
return (rawInjectPlugin.transform as TransformHook).call(this, code, id, opts)
213+
}
214+
const injectPlugin: Plugin = {
215+
name: 'vite-plugin-node-polyfills:inject',
216+
enforce: 'post',
217+
transform,
218+
}
219+
220+
const plugin: Plugin = {
203221
name: 'vite-plugin-node-polyfills',
204222
config(config, env) {
205223
const isDev = env.command === 'serve'
@@ -220,6 +238,17 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
220238
...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
221239
}
222240

241+
const isNativeInjectAvailable = env.command === 'build' && isRolldownVite
242+
rawInjectPlugin = (Object.keys(shimsToInject).length > 0 && !isNativeInjectAvailable) ? inject(shimsToInject) as Plugin : false
243+
if (rawInjectPlugin === false) {
244+
delete injectPlugin.transform
245+
} else {
246+
// hook filters are only supported in Vite 6.3.0+
247+
(transform as any).filter = {
248+
code: new RegExp(Object.keys(shimsToInject).join('|')),
249+
}
250+
}
251+
223252
return {
224253
build: {
225254
rollupOptions: {
@@ -232,17 +261,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
232261
rollupWarn(warning)
233262
})
234263
},
235-
...Object.keys(shimsToInject).length > 0
236-
? isRolldownVite
237-
? { transform: { inject: shimsToInject } }
238-
: { plugins: [inject(shimsToInject)] }
264+
...(Object.keys(shimsToInject).length > 0 && isRolldownVite)
265+
? { transform: { inject: shimsToInject } }
239266
: {},
240267
},
241268
},
242-
esbuild: {
243-
// 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.
244-
banner: isDev ? globalShimsBanner : undefined,
245-
},
246269
optimizeDeps: {
247270
exclude: [
248271
...globalShimPaths,
@@ -308,4 +331,8 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
308331
}
309332
},
310333
}
334+
return [
335+
injectPlugin,
336+
plugin,
337+
]
311338
}

test/integration/global-references/index.test.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ describe('import globals', () => {
77
const result = await transformDev(`Buffer.from('test')`)
88

99
expect(result?.code).toEqual(formatWhitespace(`
10-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
11-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
12-
import __global_polyfill from "/shims/global/dist/index.js"
13-
globalThis.global = globalThis.global || __global_polyfill
14-
import __process_polyfill from "/shims/process/dist/index.js"
15-
globalThis.process = globalThis.process || __process_polyfill
10+
import { default as Buffer } from "/shims/buffer/dist/index.js";
1611
1712
Buffer.from("test");
1813
`))
@@ -28,8 +23,7 @@ describe('import globals', () => {
2823
})
2924

3025
expect(result?.code).toEqual(formatWhitespace(`
31-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
32-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
26+
import { default as Buffer } from "/shims/buffer/dist/index.js";
3327
3428
Buffer.from("test");
3529
`))
@@ -41,12 +35,7 @@ describe('import globals', () => {
4135
const result = await transformDev(`console.log(global)`)
4236

4337
expect(result?.code).toEqual(formatWhitespace(`
44-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
45-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
46-
import __global_polyfill from "/shims/global/dist/index.js"
47-
globalThis.global = globalThis.global || __global_polyfill
48-
import __process_polyfill from "/shims/process/dist/index.js"
49-
globalThis.process = globalThis.process || __process_polyfill
38+
import { default as global } from "/shims/global/dist/index.js";
5039
5140
console.log(global);
5241
`))
@@ -62,8 +51,7 @@ describe('import globals', () => {
6251
})
6352

6453
expect(result?.code).toEqual(formatWhitespace(`
65-
import __global_polyfill from "/shims/global/dist/index.js"
66-
globalThis.global = globalThis.global || __global_polyfill
54+
import { default as global } from "/shims/global/dist/index.js";
6755
6856
console.log(global);
6957
`))
@@ -75,12 +63,7 @@ describe('import globals', () => {
7563
const result = await transformDev(`console.log(process)`)
7664

7765
expect(result?.code).toEqual(formatWhitespace(`
78-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
79-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
80-
import __global_polyfill from "/shims/global/dist/index.js"
81-
globalThis.global = globalThis.global || __global_polyfill
82-
import __process_polyfill from "/shims/process/dist/index.js"
83-
globalThis.process = globalThis.process || __process_polyfill
66+
import { default as process } from "/shims/process/dist/index.js";
8467
8568
console.log(process);
8669
`))
@@ -96,8 +79,7 @@ describe('import globals', () => {
9679
})
9780

9881
expect(result?.code).toEqual(formatWhitespace(`
99-
import __process_polyfill from "/shims/process/dist/index.js"
100-
globalThis.process = globalThis.process || __process_polyfill
82+
import { default as process } from "/shims/process/dist/index.js";
10183
10284
console.log(process);
10385
`))

test/integration/import-globals/index.test.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ describe('import globals', () => {
1010
`)
1111

1212
expect(result?.code).toEqual(formatWhitespace(`
13-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
14-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
15-
import __global_polyfill from "/shims/global/dist/index.js"
16-
globalThis.global = globalThis.global || __global_polyfill
17-
import __process_polyfill from "/shims/process/dist/index.js"
18-
globalThis.process = globalThis.process || __process_polyfill
19-
2013
import Buffer from "/shims/buffer/dist/index.js";
2114
console.log(Buffer);
2215
`))
@@ -29,13 +22,6 @@ describe('import globals', () => {
2922
`)
3023

3124
expect(result?.code).toEqual(formatWhitespace(`
32-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
33-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
34-
import __global_polyfill from "/shims/global/dist/index.js"
35-
globalThis.global = globalThis.global || __global_polyfill
36-
import __process_polyfill from "/shims/process/dist/index.js"
37-
globalThis.process = globalThis.process || __process_polyfill
38-
3925
import Buffer from "/shims/buffer/dist/index.js";
4026
console.log(Buffer);
4127
`))
@@ -48,13 +34,6 @@ describe('import globals', () => {
4834
`)
4935

5036
expect(result?.code).toEqual(formatWhitespace(`
51-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
52-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
53-
import __global_polyfill from "/shims/global/dist/index.js"
54-
globalThis.global = globalThis.global || __global_polyfill
55-
import __process_polyfill from "/shims/process/dist/index.js"
56-
globalThis.process = globalThis.process || __process_polyfill
57-
5837
import Buffer from "/shims/buffer/dist/index.js";
5938
console.log(Buffer);
6039
`))
@@ -67,13 +46,6 @@ describe('import globals', () => {
6746
`)
6847

6948
expect(result?.code).toEqual(formatWhitespace(`
70-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
71-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
72-
import __global_polyfill from "/shims/global/dist/index.js"
73-
globalThis.global = globalThis.global || __global_polyfill
74-
import __process_polyfill from "/shims/process/dist/index.js"
75-
globalThis.process = globalThis.process || __process_polyfill
76-
7749
import Buffer from "/shims/buffer/dist/index.js";
7850
console.log(Buffer);
7951
`))
@@ -88,13 +60,6 @@ describe('import globals', () => {
8860
`)
8961

9062
expect(result?.code).toEqual(formatWhitespace(`
91-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
92-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
93-
import __global_polyfill from "/shims/global/dist/index.js"
94-
globalThis.global = globalThis.global || __global_polyfill
95-
import __process_polyfill from "/shims/process/dist/index.js"
96-
globalThis.process = globalThis.process || __process_polyfill
97-
9863
import process from "/shims/process/dist/index.js";
9964
console.log(process);
10065
`))
@@ -107,13 +72,6 @@ describe('import globals', () => {
10772
`)
10873

10974
expect(result?.code).toEqual(formatWhitespace(`
110-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
111-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
112-
import __global_polyfill from "/shims/global/dist/index.js"
113-
globalThis.global = globalThis.global || __global_polyfill
114-
import __process_polyfill from "/shims/process/dist/index.js"
115-
globalThis.process = globalThis.process || __process_polyfill
116-
11775
import process from "/shims/process/dist/index.js";
11876
console.log(process);
11977
`))
@@ -126,13 +84,6 @@ describe('import globals', () => {
12684
`)
12785

12886
expect(result?.code).toEqual(formatWhitespace(`
129-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
130-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
131-
import __global_polyfill from "/shims/global/dist/index.js"
132-
globalThis.global = globalThis.global || __global_polyfill
133-
import __process_polyfill from "/shims/process/dist/index.js"
134-
globalThis.process = globalThis.process || __process_polyfill
135-
13687
import process from "/shims/process/dist/index.js";
13788
console.log(process);
13889
`))
@@ -145,13 +96,6 @@ describe('import globals', () => {
14596
`)
14697

14798
expect(result?.code).toEqual(formatWhitespace(`
148-
import __buffer_polyfill from "/shims/buffer/dist/index.js"
149-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
150-
import __global_polyfill from "/shims/global/dist/index.js"
151-
globalThis.global = globalThis.global || __global_polyfill
152-
import __process_polyfill from "/shims/process/dist/index.js"
153-
globalThis.process = globalThis.process || __process_polyfill
154-
15599
import process from "/shims/process/dist/index.js";
156100
console.log(process);
157101
`))

0 commit comments

Comments
 (0)