Skip to content

Commit 0542e7c

Browse files
authored
fix(worker): support trailing comma (#10211)
1 parent 9c7a331 commit 0542e7c

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
3838
}
3939

4040
// need to find in comment code
41-
const workerOptString = raw.substring(commaIndex + 1, endIndex)
41+
let workerOptString = raw.substring(commaIndex + 1, endIndex).trim()
42+
// strip trailing comma for parsing
43+
if (workerOptString.endsWith(',')) {
44+
workerOptString = workerOptString.slice(0, -1)
45+
}
4246

4347
const hasViteIgnore = ignoreFlagRE.test(workerOptString)
4448
if (hasViteIgnore) {
4549
return 'ignore'
4650
}
4751

4852
// need to find in no comment code
49-
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex)
50-
if (!cleanWorkerOptString.trim().length) {
53+
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim()
54+
if (!cleanWorkerOptString.length) {
5155
return 'classic'
5256
}
5357

playground/worker/worker/main-classic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ classicWorker.addEventListener('message', ({ data }) => {
1616
})
1717
classicWorker.postMessage('ping')
1818

19+
// prettier-ignore
20+
// test trailing comma
1921
const classicSharedWorker = new SharedWorker(
2022
new URL('../classic-shared-worker.js', import.meta.url),
2123
{
2224
type: 'classic'
23-
}
25+
},
2426
)
2527
classicSharedWorker.port.addEventListener('message', (ev) => {
2628
text('.classic-shared-worker', JSON.stringify(ev.data))

0 commit comments

Comments
 (0)