Skip to content

Commit a1bc855

Browse files
itrapashkodavidmyersdev
authored andcommitted
Define globals conditionally
1 parent b61a5bb commit a1bc855

File tree

4 files changed

+101
-49
lines changed

4 files changed

+101
-49
lines changed

src/index.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,20 @@ export type PolyfillOptionsResolved = {
8787
protocolImports: boolean,
8888
}
8989

90-
const globalShimsBanner = [
91-
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
92-
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
93-
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
94-
``,
95-
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
96-
`globalThis.global = globalThis.global || __global_polyfill`,
97-
`globalThis.process = globalThis.process || __process_polyfill`,
98-
``,
99-
].join('\n')
90+
const globalShimBanners = {
91+
buffer: [
92+
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
93+
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
94+
],
95+
global: [
96+
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
97+
`globalThis.global = globalThis.global || __global_polyfill`,
98+
],
99+
process: [
100+
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
101+
`globalThis.process = globalThis.process || __process_polyfill`,
102+
],
103+
}
100104

101105
/**
102106
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
@@ -127,12 +131,6 @@ const globalShimsBanner = [
127131
* ```
128132
*/
129133
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
130-
const require = createRequire(import.meta.url)
131-
const globalShimPaths = [
132-
require.resolve('vite-plugin-node-polyfills/shims/buffer'),
133-
require.resolve('vite-plugin-node-polyfills/shims/global'),
134-
require.resolve('vite-plugin-node-polyfills/shims/process'),
135-
]
136134
const optionsResolved: PolyfillOptionsResolved = {
137135
include: [],
138136
exclude: [],
@@ -187,6 +185,20 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
187185
return included
188186
}, {} as Record<ModuleName, string>)
189187

188+
const require = createRequire(import.meta.url)
189+
const globalShimPaths = [
190+
...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),
191+
...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),
192+
...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),
193+
]
194+
195+
const globalShimsBanner = [
196+
...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),
197+
...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),
198+
...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),
199+
``,
200+
].join('\n')
201+
190202
return {
191203
name: 'vite-plugin-node-polyfills',
192204
config: (config, env) => {

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ describe('import globals', () => {
88

99
expect(result?.code).toEqual(formatWhitespace(`
1010
import __buffer_polyfill from "/shims/buffer/dist/index.js"
11+
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
1112
import __global_polyfill from "/shims/global/dist/index.js"
13+
globalThis.global = globalThis.global || __global_polyfill
1214
import __process_polyfill from "/shims/process/dist/index.js"
15+
globalThis.process = globalThis.process || __process_polyfill
1316
17+
Buffer.from("test");
18+
`))
19+
})
20+
21+
it('injects Buffer only', async () => {
22+
const result = await transformDev(`Buffer.from('test')`, {
23+
globals: {
24+
Buffer: true,
25+
global: false,
26+
process: false,
27+
},
28+
})
29+
30+
expect(result?.code).toEqual(formatWhitespace(`
31+
import __buffer_polyfill from "/shims/buffer/dist/index.js"
1432
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
15-
globalThis.global = globalThis.global || __global_polyfill
16-
globalThis.process = globalThis.process || __process_polyfill
1733
1834
Buffer.from("test");
1935
`))
@@ -26,12 +42,28 @@ describe('import globals', () => {
2642

2743
expect(result?.code).toEqual(formatWhitespace(`
2844
import __buffer_polyfill from "/shims/buffer/dist/index.js"
45+
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
2946
import __global_polyfill from "/shims/global/dist/index.js"
47+
globalThis.global = globalThis.global || __global_polyfill
3048
import __process_polyfill from "/shims/process/dist/index.js"
49+
globalThis.process = globalThis.process || __process_polyfill
3150
32-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
51+
console.log(global);
52+
`))
53+
})
54+
55+
it('injects global only', async () => {
56+
const result = await transformDev(`console.log(global)`, {
57+
globals: {
58+
Buffer: false,
59+
global: true,
60+
process: false,
61+
},
62+
})
63+
64+
expect(result?.code).toEqual(formatWhitespace(`
65+
import __global_polyfill from "/shims/global/dist/index.js"
3366
globalThis.global = globalThis.global || __global_polyfill
34-
globalThis.process = globalThis.process || __process_polyfill
3567
3668
console.log(global);
3769
`))
@@ -44,11 +76,27 @@ describe('import globals', () => {
4476

4577
expect(result?.code).toEqual(formatWhitespace(`
4678
import __buffer_polyfill from "/shims/buffer/dist/index.js"
79+
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
4780
import __global_polyfill from "/shims/global/dist/index.js"
81+
globalThis.global = globalThis.global || __global_polyfill
4882
import __process_polyfill from "/shims/process/dist/index.js"
83+
globalThis.process = globalThis.process || __process_polyfill
4984
50-
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
51-
globalThis.global = globalThis.global || __global_polyfill
85+
console.log(process);
86+
`))
87+
})
88+
89+
it('injects process only', async () => {
90+
const result = await transformDev(`console.log(process)`, {
91+
globals: {
92+
Buffer: false,
93+
global: false,
94+
process: true,
95+
},
96+
})
97+
98+
expect(result?.code).toEqual(formatWhitespace(`
99+
import __process_polyfill from "/shims/process/dist/index.js"
52100
globalThis.process = globalThis.process || __process_polyfill
53101
54102
console.log(process);

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ describe('import globals', () => {
1111

1212
expect(result?.code).toEqual(formatWhitespace(`
1313
import __buffer_polyfill from "/shims/buffer/dist/index.js"
14-
import __global_polyfill from "/shims/global/dist/index.js"
15-
import __process_polyfill from "/shims/process/dist/index.js"
16-
1714
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
15+
import __global_polyfill from "/shims/global/dist/index.js"
1816
globalThis.global = globalThis.global || __global_polyfill
17+
import __process_polyfill from "/shims/process/dist/index.js"
1918
globalThis.process = globalThis.process || __process_polyfill
2019
2120
import Buffer from "/shims/buffer/dist/index.js";
@@ -31,11 +30,10 @@ describe('import globals', () => {
3130

3231
expect(result?.code).toEqual(formatWhitespace(`
3332
import __buffer_polyfill from "/shims/buffer/dist/index.js"
34-
import __global_polyfill from "/shims/global/dist/index.js"
35-
import __process_polyfill from "/shims/process/dist/index.js"
36-
3733
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
34+
import __global_polyfill from "/shims/global/dist/index.js"
3835
globalThis.global = globalThis.global || __global_polyfill
36+
import __process_polyfill from "/shims/process/dist/index.js"
3937
globalThis.process = globalThis.process || __process_polyfill
4038
4139
import Buffer from "/shims/buffer/dist/index.js";
@@ -51,11 +49,10 @@ describe('import globals', () => {
5149

5250
expect(result?.code).toEqual(formatWhitespace(`
5351
import __buffer_polyfill from "/shims/buffer/dist/index.js"
54-
import __global_polyfill from "/shims/global/dist/index.js"
55-
import __process_polyfill from "/shims/process/dist/index.js"
56-
5752
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
53+
import __global_polyfill from "/shims/global/dist/index.js"
5854
globalThis.global = globalThis.global || __global_polyfill
55+
import __process_polyfill from "/shims/process/dist/index.js"
5956
globalThis.process = globalThis.process || __process_polyfill
6057
6158
import Buffer from "/shims/buffer/dist/index.js";
@@ -71,11 +68,10 @@ describe('import globals', () => {
7168

7269
expect(result?.code).toEqual(formatWhitespace(`
7370
import __buffer_polyfill from "/shims/buffer/dist/index.js"
74-
import __global_polyfill from "/shims/global/dist/index.js"
75-
import __process_polyfill from "/shims/process/dist/index.js"
76-
7771
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
72+
import __global_polyfill from "/shims/global/dist/index.js"
7873
globalThis.global = globalThis.global || __global_polyfill
74+
import __process_polyfill from "/shims/process/dist/index.js"
7975
globalThis.process = globalThis.process || __process_polyfill
8076
8177
import Buffer from "/shims/buffer/dist/index.js";
@@ -93,11 +89,10 @@ describe('import globals', () => {
9389

9490
expect(result?.code).toEqual(formatWhitespace(`
9591
import __buffer_polyfill from "/shims/buffer/dist/index.js"
96-
import __global_polyfill from "/shims/global/dist/index.js"
97-
import __process_polyfill from "/shims/process/dist/index.js"
98-
9992
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
93+
import __global_polyfill from "/shims/global/dist/index.js"
10094
globalThis.global = globalThis.global || __global_polyfill
95+
import __process_polyfill from "/shims/process/dist/index.js"
10196
globalThis.process = globalThis.process || __process_polyfill
10297
10398
import process from "/shims/process/dist/index.js";
@@ -113,11 +108,10 @@ describe('import globals', () => {
113108

114109
expect(result?.code).toEqual(formatWhitespace(`
115110
import __buffer_polyfill from "/shims/buffer/dist/index.js"
116-
import __global_polyfill from "/shims/global/dist/index.js"
117-
import __process_polyfill from "/shims/process/dist/index.js"
118-
119111
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
112+
import __global_polyfill from "/shims/global/dist/index.js"
120113
globalThis.global = globalThis.global || __global_polyfill
114+
import __process_polyfill from "/shims/process/dist/index.js"
121115
globalThis.process = globalThis.process || __process_polyfill
122116
123117
import process from "/shims/process/dist/index.js";
@@ -133,11 +127,10 @@ describe('import globals', () => {
133127

134128
expect(result?.code).toEqual(formatWhitespace(`
135129
import __buffer_polyfill from "/shims/buffer/dist/index.js"
136-
import __global_polyfill from "/shims/global/dist/index.js"
137-
import __process_polyfill from "/shims/process/dist/index.js"
138-
139130
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
131+
import __global_polyfill from "/shims/global/dist/index.js"
140132
globalThis.global = globalThis.global || __global_polyfill
133+
import __process_polyfill from "/shims/process/dist/index.js"
141134
globalThis.process = globalThis.process || __process_polyfill
142135
143136
import process from "/shims/process/dist/index.js";
@@ -153,11 +146,10 @@ describe('import globals', () => {
153146

154147
expect(result?.code).toEqual(formatWhitespace(`
155148
import __buffer_polyfill from "/shims/buffer/dist/index.js"
156-
import __global_polyfill from "/shims/global/dist/index.js"
157-
import __process_polyfill from "/shims/process/dist/index.js"
158-
159149
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
150+
import __global_polyfill from "/shims/global/dist/index.js"
160151
globalThis.global = globalThis.global || __global_polyfill
152+
import __process_polyfill from "/shims/process/dist/index.js"
161153
globalThis.process = globalThis.process || __process_polyfill
162154
163155
import process from "/shims/process/dist/index.js";

test/utils/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createServer } from 'vite'
2-
import { nodePolyfills } from 'vite-plugin-node-polyfills'
2+
import { nodePolyfills, PolyfillOptions } from 'vite-plugin-node-polyfills'
33

44
/**
55
* Format code by removing the smallest indentation from each line.
@@ -25,14 +25,14 @@ export const formatWhitespace = (code: string) => {
2525
return `${formatted}\n`
2626
}
2727

28-
export const transformDev = async (code: string) => {
28+
export const transformDev = async (code: string, options?: PolyfillOptions) => {
2929
const server = await createServer({
3030
configFile: false,
3131
esbuild: {
3232
format: 'esm',
3333
},
3434
plugins: [
35-
nodePolyfills(),
35+
nodePolyfills(options),
3636
{
3737
name: 'test-code-loader',
3838
load(id) {

0 commit comments

Comments
 (0)