Skip to content

Commit

Permalink
fix(worker): fix web worker type detection (#19462)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 18, 2025
1 parent 00deea4 commit edc65ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,21 @@ worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.strin
'Expected object spread to be used before the definition of the type property. Vite needs a static value for the type property to correctly infer it.',
)
})

test('find closing parenthesis correctly', async () => {
expect(
await transform(
`(() => { new Worker(new URL('./worker', import.meta.url)); repro({ test: "foo", }); })();`,
),
).toMatchInlineSnapshot(
`"(() => { new Worker(new URL(/* @vite-ignore */ "/worker?worker_file&type=classic", import.meta.url)); repro({ test: "foo", }); })();"`,
)
expect(
await transform(
`repro(new Worker(new URL('./worker', import.meta.url)), { type: "module" })`,
),
).toMatchInlineSnapshot(
`"repro(new Worker(new URL(/* @vite-ignore */ "/worker?worker_file&type=classic", import.meta.url)), { type: "module" })"`,
)
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function err(e: string, pos: number) {
function findClosingParen(input: string, fromIndex: number) {
let count = 1

for (let i = fromIndex + 1; i < input.length; i++) {
for (let i = fromIndex; i < input.length; i++) {
if (input[i] === '(') count++
if (input[i] === ')') count--
if (count === 0) return i
Expand Down

0 comments on commit edc65ea

Please sign in to comment.