From 07091a1e804e5934208ef0b6324a04317dd0d815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pfl=C3=BCger?= Date: Fri, 21 Feb 2025 12:10:02 +0100 Subject: [PATCH] fix(worker): string interpolation in dynamic worker options (#19476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- .../plugins/workerImportMetaUrl.spec.ts | 40 +++++++++++++++++++ .../src/node/plugins/workerImportMetaUrl.ts | 18 ++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts b/packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts index 42b130c2941d13..559b51a8d51cbd 100644 --- a/packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts @@ -71,6 +71,16 @@ describe('workerImportMetaUrlPlugin', async () => { ) }) + test('with interpolated dynamic name field in worker options', async () => { + expect( + await transform( + 'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: `worker-${id}` })', + ), + ).toMatchInlineSnapshot( + `"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", import.meta.url), { name: \`worker-\${id}\` })"`, + ) + }) + test('with dynamic name field and static type in worker options', async () => { expect( await transform( @@ -81,6 +91,16 @@ describe('workerImportMetaUrlPlugin', async () => { ) }) + test('with interpolated dynamic name field and static type in worker options', async () => { + expect( + await transform( + 'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: `worker-${id}`, type: "module" })', + ), + ).toMatchInlineSnapshot( + `"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { name: \`worker-\${id}\`, type: "module" })"`, + ) + }) + test('with parenthesis inside of worker options', async () => { expect( await transform( @@ -113,6 +133,26 @@ worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.strin "`) }) + test('trailing comma', async () => { + expect( + await transform(` +new Worker( + new URL('./worker.js', import.meta.url), + { + type: 'module' + }, // }, +) +`), + ).toMatchInlineSnapshot(`" +new Worker( + new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), + { + type: 'module' + }, // }, +) +"`) + }) + test('throws an error when non-static worker options are provided', async () => { await expect( transform( diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 58758e05279c0b..ed0eae7801154b 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -149,21 +149,27 @@ async function getWorkerType( } // need to find in comment code - const workerOptString = raw - .substring(commaIndex + 1, endIndex) - .replace(/\}[\s\S]*,/g, '}') // strip trailing comma for parsing - + let workerOptString = raw.substring(commaIndex + 1, endIndex) const hasViteIgnore = hasViteIgnoreRE.test(workerOptString) if (hasViteIgnore) { return 'ignore' } // need to find in no comment code - const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim() - if (!cleanWorkerOptString.length) { + const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex) + const trimmedCleanWorkerOptString = cleanWorkerOptString.trim() + if (!trimmedCleanWorkerOptString.length) { return 'classic' } + // strip trailing comma for evalValue + if (trimmedCleanWorkerOptString.endsWith(',')) { + workerOptString = workerOptString.slice( + 0, + cleanWorkerOptString.lastIndexOf(','), + ) + } + const workerOpts = await parseWorkerOptions(workerOptString, commaIndex + 1) if ( workerOpts.type &&