Skip to content

Commit 07bc489

Browse files
authored
fix(worker): nested inlined worker always fallbacked to data URI worker instead of using blob worker (#17509)
1 parent 167006e commit 07bc489

File tree

7 files changed

+17
-17
lines changed

7 files changed

+17
-17
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
305305
workerConstructor === 'Worker'
306306
? `${encodedJs}
307307
const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
308-
const blob = typeof window !== "undefined" && window.Blob && new Blob([${
308+
const blob = typeof self !== "undefined" && self.Blob && new Blob([${
309309
workerType === 'classic'
310310
? ''
311311
: // `URL` is always available, in `Worker[type="module"]`
@@ -314,11 +314,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
314314
export default function WorkerWrapper(options) {
315315
let objURL;
316316
try {
317-
objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
317+
objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
318318
if (!objURL) throw ''
319319
const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
320320
worker.addEventListener("error", () => {
321-
(window.URL || window.webkitURL).revokeObjectURL(objURL);
321+
(self.URL || self.webkitURL).revokeObjectURL(objURL);
322322
});
323323
return worker;
324324
} catch(e) {
@@ -331,7 +331,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
331331
// otherwise the worker fails to run
332332
workerType === 'classic'
333333
? ` finally {
334-
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
334+
objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL);
335335
}`
336336
: ''
337337
}

playground/worker/__tests__/es/worker-es.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ describe.runIf(isBuild)('build', () => {
127127
expect(content).toMatch(`new Worker("/es/assets`)
128128
expect(content).toMatch(`new SharedWorker("/es/assets`)
129129
// inlined worker
130-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
131-
expect(content).toMatch(`window.Blob`)
130+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
131+
expect(content).toMatch(`self.Blob`)
132132
expect(content).toMatch(
133-
/try\{if\(\w+=\w+&&\(window\.URL\|\|window\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
133+
/try\{if\(\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
134134
)
135135
// inlined shared worker
136136
expect(content).toMatch(

playground/worker/__tests__/iife/worker-iife.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe.runIf(isBuild)('build', () => {
9191
expect(content).toMatch(`new Worker("/iife/assets`)
9292
expect(content).toMatch(`new SharedWorker("/iife/assets`)
9393
// inlined
94-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
95-
expect(content).toMatch(`window.Blob`)
94+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
95+
expect(content).toMatch(`self.Blob`)
9696
})
9797

9898
test('worker emitted and import.meta.url in nested worker (build)', async () => {

playground/worker/__tests__/relative-base/worker-relative-base.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ describe.runIf(isBuild)('build', () => {
9090
expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`)
9191
expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`)
9292
// inlined
93-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
94-
expect(content).toMatch(`window.Blob`)
93+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
94+
expect(content).toMatch(`self.Blob`)
9595
})
9696

9797
test('worker emitted and import.meta.url in nested worker (build)', async () => {

playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ describe.runIf(isBuild)('build', () => {
106106
)
107107

108108
// inlined
109-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
110-
expect(content).toMatch(`window.Blob`)
109+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
110+
expect(content).toMatch(`self.Blob`)
111111

112112
expect(workerNestedWorkerContent).toMatch(
113113
`new Worker("/iife-sourcemap-hidden/assets/sub-worker`,

playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ describe.runIf(isBuild)('build', () => {
8787
)
8888

8989
// inlined
90-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
91-
expect(content).toMatch(`window.Blob`)
90+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
91+
expect(content).toMatch(`self.Blob`)
9292

9393
expect(workerNestedWorkerContent).toMatch(
9494
`new Worker("/iife-sourcemap-inline/assets/sub-worker`,

playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ describe.runIf(isBuild)('build', () => {
107107
)
108108

109109
// inlined
110-
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
111-
expect(content).toMatch(`window.Blob`)
110+
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
111+
expect(content).toMatch(`self.Blob`)
112112

113113
expect(workerNestedWorkerContent).toMatch(
114114
`new Worker("/iife-sourcemap/assets/sub-worker`,

0 commit comments

Comments
 (0)