Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: missing backtick
  • Loading branch information
christoph-pflueger committed Feb 20, 2025
commit b6a9e2f6ccd2de23db250051eaeac368548777e8
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
9 changes: 6 additions & 3 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ 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).trim()

// strip trailing comma for parsing
if (workerOptString.endsWith(',')) {
workerOptString = workerOptString.slice(0, -1)
}

const hasViteIgnore = hasViteIgnoreRE.test(workerOptString)
if (hasViteIgnore) {
Expand Down