Skip to content

Commit edc65ea

Browse files
authored
fix(worker): fix web worker type detection (#19462)
1 parent 00deea4 commit edc65ea

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,21 @@ worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.strin
160160
'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.',
161161
)
162162
})
163+
164+
test('find closing parenthesis correctly', async () => {
165+
expect(
166+
await transform(
167+
`(() => { new Worker(new URL('./worker', import.meta.url)); repro({ test: "foo", }); })();`,
168+
),
169+
).toMatchInlineSnapshot(
170+
`"(() => { new Worker(new URL(/* @vite-ignore */ "/worker?worker_file&type=classic", import.meta.url)); repro({ test: "foo", }); })();"`,
171+
)
172+
expect(
173+
await transform(
174+
`repro(new Worker(new URL('./worker', import.meta.url)), { type: "module" })`,
175+
),
176+
).toMatchInlineSnapshot(
177+
`"repro(new Worker(new URL(/* @vite-ignore */ "/worker?worker_file&type=classic", import.meta.url)), { type: "module" })"`,
178+
)
179+
})
163180
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function err(e: string, pos: number) {
3030
function findClosingParen(input: string, fromIndex: number) {
3131
let count = 1
3232

33-
for (let i = fromIndex + 1; i < input.length; i++) {
33+
for (let i = fromIndex; i < input.length; i++) {
3434
if (input[i] === '(') count++
3535
if (input[i] === ')') count--
3636
if (count === 0) return i

0 commit comments

Comments
 (0)