Skip to content

Commit 5f36379

Browse files
authored
Avoid creating an inject plugin when there are no shims to inject (#101)
1 parent c40a478 commit 5f36379

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/index.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
204204
config: (config, env) => {
205205
const isDev = env.command === 'serve'
206206

207+
const shimsToInject = {
208+
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
209+
...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),
210+
...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),
211+
...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
212+
}
213+
207214
return {
208215
build: {
209216
rollupOptions: {
@@ -216,16 +223,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
216223
rollupWarn(warning)
217224
})
218225
},
219-
plugins: [
220-
{
221-
...inject({
222-
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
223-
...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),
224-
...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),
225-
...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
226-
}),
227-
},
228-
],
226+
plugins: Object.keys(shimsToInject).length > 0 ? [inject(shimsToInject)] : [],
229227
},
230228
},
231229
esbuild: {

0 commit comments

Comments
 (0)