From bb4ba9f448da01782f585369f80c4517da087000 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 6 Aug 2024 23:08:34 +0900 Subject: [PATCH] fix(worker): fix `importScripts` injection breaking iife code (#17827) --- .prettierignore | 1 + packages/vite/src/node/plugins/worker.ts | 2 +- playground/worker/classic-worker.js | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index 0fc71ddd1..95541be45 100644 --- a/.prettierignore +++ b/.prettierignore @@ -11,3 +11,4 @@ playground/html/invalid.html playground/html/valid.html playground/external/public/slash@3.0.0.js playground/ssr-html/public/slash@3.0.0.js +playground/worker/classic-worker.js diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index dcaed0473..2e614ec7e 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -265,7 +265,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } if (injectEnv) { const s = new MagicString(raw) - s.prepend(injectEnv) + s.prepend(injectEnv + ';\n') return { code: s.toString(), map: s.generateMap({ hires: 'boundary' }), diff --git a/playground/worker/classic-worker.js b/playground/worker/classic-worker.js index be6fa357e..d68c61c14 100644 --- a/playground/worker/classic-worker.js +++ b/playground/worker/classic-worker.js @@ -1,3 +1,5 @@ +(() => {})() // this is to test `importScripts` injection doesn't break the code + let base = `/${self.location.pathname.split('/')[1]}` if (base.endsWith('.js') || base === `/worker-entries`) base = '' // for dev