Skip to content

Commit

Permalink
fix(worker): support trailing comma (#10211)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 23, 2022
1 parent 9c7a331 commit 0542e7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
}

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

const hasViteIgnore = ignoreFlagRE.test(workerOptString)
if (hasViteIgnore) {
return 'ignore'
}

// need to find in no comment code
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex)
if (!cleanWorkerOptString.trim().length) {
const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim()
if (!cleanWorkerOptString.length) {
return 'classic'
}

Expand Down
4 changes: 3 additions & 1 deletion playground/worker/worker/main-classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ classicWorker.addEventListener('message', ({ data }) => {
})
classicWorker.postMessage('ping')

// prettier-ignore
// test trailing comma
const classicSharedWorker = new SharedWorker(
new URL('../classic-shared-worker.js', import.meta.url),
{
type: 'classic'
}
},
)
classicSharedWorker.port.addEventListener('message', (ev) => {
text('.classic-shared-worker', JSON.stringify(ev.data))
Expand Down

0 comments on commit 0542e7c

Please sign in to comment.