From 88540b1adc2387a5eadf6b152e322f3311d94a68 Mon Sep 17 00:00:00 2001 From: sapenlei <2503404258gl@gmail.com> Date: Fri, 27 Dec 2024 21:10:47 +0800 Subject: [PATCH] fix(plugin-legacy): fix legacy plugin not working in worker context --- packages/plugin-legacy/src/index.ts | 7 +++++++ packages/vite/src/node/plugins/esbuild.ts | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index d356793b231c72..a22d15df2b8084 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -270,6 +270,13 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { ), ) } + if (config.isWorker) { + config.logger.warn( + colors.yellow( + `Workers are not supported when generating legacy chunks. Ensure that workers are not used in your application when targeting legacy browsers.`, + ), + ) + } }, } diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 83fa1af4248ab5..ffcfafdf882c0b 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -336,6 +336,10 @@ export const buildEsbuildPlugin = (): Plugin => { return null } + if (chunk.name === 'worker' && opts.format === 'iife') { + options.format = 'iife' + } + const res = await transformWithEsbuild( code, chunk.fileName,