Skip to content

Commit 07091a1

Browse files
christoph-pfluegersapphi-red
andauthoredFeb 21, 2025··
fix(worker): string interpolation in dynamic worker options (#19476)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent e01573a commit 07091a1

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed
 

‎packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ describe('workerImportMetaUrlPlugin', async () => {
7171
)
7272
})
7373

74+
test('with interpolated dynamic name field in worker options', async () => {
75+
expect(
76+
await transform(
77+
'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: `worker-${id}` })',
78+
),
79+
).toMatchInlineSnapshot(
80+
`"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", import.meta.url), { name: \`worker-\${id}\` })"`,
81+
)
82+
})
83+
7484
test('with dynamic name field and static type in worker options', async () => {
7585
expect(
7686
await transform(
@@ -81,6 +91,16 @@ describe('workerImportMetaUrlPlugin', async () => {
8191
)
8292
})
8393

94+
test('with interpolated dynamic name field and static type in worker options', async () => {
95+
expect(
96+
await transform(
97+
'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: `worker-${id}`, type: "module" })',
98+
),
99+
).toMatchInlineSnapshot(
100+
`"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { name: \`worker-\${id}\`, type: "module" })"`,
101+
)
102+
})
103+
84104
test('with parenthesis inside of worker options', async () => {
85105
expect(
86106
await transform(
@@ -113,6 +133,26 @@ worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.strin
113133
"`)
114134
})
115135

136+
test('trailing comma', async () => {
137+
expect(
138+
await transform(`
139+
new Worker(
140+
new URL('./worker.js', import.meta.url),
141+
{
142+
type: 'module'
143+
}, // },
144+
)
145+
`),
146+
).toMatchInlineSnapshot(`"
147+
new Worker(
148+
new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url),
149+
{
150+
type: 'module'
151+
}, // },
152+
)
153+
"`)
154+
})
155+
116156
test('throws an error when non-static worker options are provided', async () => {
117157
await expect(
118158
transform(

‎packages/vite/src/node/plugins/workerImportMetaUrl.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,27 @@ async function getWorkerType(
149149
}
150150

151151
// need to find in comment code
152-
const workerOptString = raw
153-
.substring(commaIndex + 1, endIndex)
154-
.replace(/\}[\s\S]*,/g, '}') // strip trailing comma for parsing
155-
152+
let workerOptString = raw.substring(commaIndex + 1, endIndex)
156153
const hasViteIgnore = hasViteIgnoreRE.test(workerOptString)
157154
if (hasViteIgnore) {
158155
return 'ignore'
159156
}
160157

161158
// need to find in no comment code
162-
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim()
163-
if (!cleanWorkerOptString.length) {
159+
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex)
160+
const trimmedCleanWorkerOptString = cleanWorkerOptString.trim()
161+
if (!trimmedCleanWorkerOptString.length) {
164162
return 'classic'
165163
}
166164

165+
// strip trailing comma for evalValue
166+
if (trimmedCleanWorkerOptString.endsWith(',')) {
167+
workerOptString = workerOptString.slice(
168+
0,
169+
cleanWorkerOptString.lastIndexOf(','),
170+
)
171+
}
172+
167173
const workerOpts = await parseWorkerOptions(workerOptString, commaIndex + 1)
168174
if (
169175
workerOpts.type &&

0 commit comments

Comments
 (0)
Please sign in to comment.