Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 12 additions & 6 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down