From 09503d4e493325a6d5fcdb9a4dfa5bf194268405 Mon Sep 17 00:00:00 2001
From: saimskywalker
Date: Sun, 21 Dec 2025 03:26:02 +0700
Subject: [PATCH] fix: handle trailing comma in Worker `new URL` multiline
format
The Worker plugin regex did not handle trailing commas after
`import.meta.url` when formatted by Prettier across multiple lines.
This aligns the Worker plugin regex with the Asset plugin regex
which already handles this case correctly.
---
.../vite/src/node/plugins/workerImportMetaUrl.ts | 2 +-
playground/worker/__tests__/es/worker-es.spec.ts | 6 ++++++
playground/worker/__tests__/iife/worker-iife.spec.ts | 6 ++++++
playground/worker/index.html | 6 ++++++
playground/worker/worker/main-module.js | 12 ++++++++++++
5 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
index a8b25a262ff0f4..991783b64e075b 100644
--- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts
+++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
@@ -209,7 +209,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
let s: MagicString | undefined
const cleanString = stripLiteral(code)
const workerImportMetaUrlRE =
- /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg
+ /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\))/dg
let match: RegExpExecArray | null
while ((match = workerImportMetaUrlRE.exec(cleanString))) {
diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts
index 95282d30fa27fb..e24ef67646c0e2 100644
--- a/playground/worker/__tests__/es/worker-es.spec.ts
+++ b/playground/worker/__tests__/es/worker-es.spec.ts
@@ -148,6 +148,12 @@ test('module worker', async () => {
.toMatch('A string')
})
+test('new URL(..., import.meta.url) (multiline with trailing comma)', async () => {
+ await expect
+ .poll(() => page.textContent('.worker-import-meta-url-multiline'))
+ .toMatch('A string')
+})
+
test('classic worker', async () => {
await expect
.poll(() => page.textContent('.classic-worker'))
diff --git a/playground/worker/__tests__/iife/worker-iife.spec.ts b/playground/worker/__tests__/iife/worker-iife.spec.ts
index 07ae7ab0bc9221..645c55d90f9061 100644
--- a/playground/worker/__tests__/iife/worker-iife.spec.ts
+++ b/playground/worker/__tests__/iife/worker-iife.spec.ts
@@ -133,6 +133,12 @@ test('module worker', async () => {
.toMatch('A string')
})
+test('new URL(..., import.meta.url) (multiline with trailing comma)', async () => {
+ await expect
+ .poll(() => page.textContent('.worker-import-meta-url-multiline'))
+ .toMatch(/A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/)
+})
+
test('classic worker', async () => {
await expect
.poll(() => page.textContent('.classic-worker'))
diff --git a/playground/worker/index.html b/playground/worker/index.html
index 8ece3711324053..e26b4cdfad39cf 100644
--- a/playground/worker/index.html
+++ b/playground/worker/index.html
@@ -88,6 +88,12 @@
+
+ new Worker(new URL(..., import.meta.url,), ...) (multiline with trailing comma)
+ .worker-import-meta-url-multiline
+
+
+
new Worker(new URL('@/url-worker', import.meta.url), { type: 'module' })
.worker-import-meta-url-resolve
diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js
index a659aa438fd5ca..3ea13fba7fbdc4 100644
--- a/playground/worker/worker/main-module.js
+++ b/playground/worker/worker/main-module.js
@@ -118,6 +118,18 @@ w.addEventListener('message', (ev) =>
text('.worker-import-meta-url', JSON.stringify(ev.data)),
)
+// url import worker (multiline with trailing comma - formatted by prettier)
+const wMultiline = new Worker(
+ new URL(
+ '../url-worker.js',
+ import.meta.url,
+ ),
+ { type: 'module' },
+)
+wMultiline.addEventListener('message', (ev) =>
+ text('.worker-import-meta-url-multiline', JSON.stringify(ev.data)),
+)
+
// url import worker with alias path
const wResolve = new Worker(
new URL('@/url-worker.js', import.meta.url),