Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
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
6 changes: 6 additions & 0 deletions playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/__tests__/iife/worker-iife.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="worker-import-meta-url"></code>

<p>
new Worker(new URL(..., import.meta.url,), ...) (multiline with trailing comma)
<span class="classname">.worker-import-meta-url-multiline</span>
</p>
<code class="worker-import-meta-url-multiline"></code>

<p>
new Worker(new URL('@/url-worker', import.meta.url), { type: 'module' })
<span class="classname">.worker-import-meta-url-resolve</span>
Expand Down
12 changes: 12 additions & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading