Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,36 @@
`"repro(new Worker(new URL(/* @vite-ignore */ "/worker?worker_file&type=classic", '' + import.meta.url)), { type: "module" })"`,
)
})

test('with multi-line new URL and trailing comma', async () => {
expect(
await transform(`new Worker(
new URL(
"./worker.js",
import.meta.url,
)
)`),
).toMatchInlineSnapshot(`
"new Worker(
new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", '' + import.meta.url)
)"
`)
})

test('with multi-line new URL, trailing comma, and worker options', async () => {
expect(
await transform(`const worker = new Worker(
new URL(
"../some-loooooooooooooooooong-file-path-of-module-name.js",
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
import.meta.url,
),
{ type: "module" },
)`),
).toMatchInlineSnapshot(`

Check failure on line 249 in packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-24, macos-latest

packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts > workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options

Error: Snapshot `workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options 1` mismatched - Expected + Received "const worker = new Worker( - new URL(/* @vite-ignore */ "/@fs/home/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), + new URL(/* @vite-ignore */ "/@fs/Users/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), { type: "module" }, )" ❯ packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts:249:7

Check failure on line 249 in packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-22, macos-latest (js plugins)

packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts > workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options

Error: Snapshot `workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options 1` mismatched - Expected + Received "const worker = new Worker( - new URL(/* @vite-ignore */ "/@fs/home/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), + new URL(/* @vite-ignore */ "/@fs/Users/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), { type: "module" }, )" ❯ packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts:249:7

Check failure on line 249 in packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-22, windows-latest (js plugins)

packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts > workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options

Error: Snapshot `workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options 1` mismatched - Expected + Received "const worker = new Worker( - new URL(/* @vite-ignore */ "/@fs/home/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), + new URL(/* @vite-ignore */ "/@fs/D:/a/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), { type: "module" }, )" ❯ packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts:249:7

Check failure on line 249 in packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-24, windows-latest

packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts > workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options

Error: Snapshot `workerImportMetaUrlPlugin > with multi-line new URL, trailing comma, and worker options 1` mismatched - Expected + Received "const worker = new Worker( - new URL(/* @vite-ignore */ "/@fs/home/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), + new URL(/* @vite-ignore */ "/@fs/D:/a/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url), { type: "module" }, )" ❯ packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts:249:7
"const worker = new Worker(
new URL(/* @vite-ignore */ "/@fs/home/runner/work/vite/some-loooooooooooooooooong-file-path-of-module-name.js?worker_file&type=module", '' + import.meta.url),
{ type: "module" },
)"
`)
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
Loading