From e4620844ef439dc5adf6d148a171781f6e7edb2d Mon Sep 17 00:00:00 2001 From: Juan Lasheras Date: Sat, 27 Jul 2024 15:11:24 -0700 Subject: [PATCH] Avoid creating an inject plugin when there are no shims to inject --- src/index.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 89a4055..3359a85 100644 --- a/src/index.ts +++ b/src/index.ts @@ -204,6 +204,13 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { config: (config, env) => { const isDev = env.command === 'serve' + const shimsToInject = { + // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite + ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}), + ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}), + ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}), + } + return { build: { rollupOptions: { @@ -216,16 +223,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { rollupWarn(warning) }) }, - plugins: [ - { - ...inject({ - // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite - ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}), - ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}), - ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}), - }), - }, - ], + plugins: Object.keys(shimsToInject).length > 0 ? [inject(shimsToInject)] : [], }, }, esbuild: {