Skip to content

Commit

Permalink
fix(worker): string interpolation in dynamic worker options (#19476)
Browse files Browse the repository at this point in the history
Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
christoph-pflueger and sapphi-red authored Feb 21, 2025
1 parent e01573a commit 07091a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
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

0 comments on commit 07091a1

Please sign in to comment.