From 07bc489b310e8173e4929193f3f283e1e50fa87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E7=97=95?= <745280746@qq.com> Date: Sun, 14 Jul 2024 03:52:40 +0800 Subject: [PATCH] fix(worker): nested inlined worker always fallbacked to data URI worker instead of using blob worker (#17509) --- packages/vite/src/node/plugins/worker.ts | 8 ++++---- playground/worker/__tests__/es/worker-es.spec.ts | 6 +++--- playground/worker/__tests__/iife/worker-iife.spec.ts | 4 ++-- .../__tests__/relative-base/worker-relative-base.spec.ts | 4 ++-- .../sourcemap-hidden/worker-sourcemap-hidden.spec.ts | 4 ++-- .../sourcemap-inline/worker-sourcemap-inline.spec.ts | 4 ++-- .../worker/__tests__/sourcemap/worker-sourcemap.spec.ts | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 4094b581a52b63..dcaed0473e13b1 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -305,7 +305,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { workerConstructor === 'Worker' ? `${encodedJs} const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0)); - const blob = typeof window !== "undefined" && window.Blob && new Blob([${ + const blob = typeof self !== "undefined" && self.Blob && new Blob([${ workerType === 'classic' ? '' : // `URL` is always available, in `Worker[type="module"]` @@ -314,11 +314,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { export default function WorkerWrapper(options) { let objURL; try { - objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob); + objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob); if (!objURL) throw '' const worker = new ${workerConstructor}(objURL, ${workerTypeOption}); worker.addEventListener("error", () => { - (window.URL || window.webkitURL).revokeObjectURL(objURL); + (self.URL || self.webkitURL).revokeObjectURL(objURL); }); return worker; } catch(e) { @@ -331,7 +331,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { // otherwise the worker fails to run workerType === 'classic' ? ` finally { - objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL); + objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL); }` : '' } diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts index 3656b07c18d576..080ab455950a4c 100644 --- a/playground/worker/__tests__/es/worker-es.spec.ts +++ b/playground/worker/__tests__/es/worker-es.spec.ts @@ -127,10 +127,10 @@ describe.runIf(isBuild)('build', () => { expect(content).toMatch(`new Worker("/es/assets`) expect(content).toMatch(`new SharedWorker("/es/assets`) // inlined worker - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) expect(content).toMatch( - /try\{if\(\w+=\w+&&\(window\.URL\|\|window\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/, + /try\{if\(\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/, ) // inlined shared worker expect(content).toMatch( diff --git a/playground/worker/__tests__/iife/worker-iife.spec.ts b/playground/worker/__tests__/iife/worker-iife.spec.ts index 77547e8426fbd1..d168b6f9e3ddd9 100644 --- a/playground/worker/__tests__/iife/worker-iife.spec.ts +++ b/playground/worker/__tests__/iife/worker-iife.spec.ts @@ -91,8 +91,8 @@ describe.runIf(isBuild)('build', () => { expect(content).toMatch(`new Worker("/iife/assets`) expect(content).toMatch(`new SharedWorker("/iife/assets`) // inlined - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) }) test('worker emitted and import.meta.url in nested worker (build)', async () => { diff --git a/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts b/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts index 6c3180c7e2aa31..36a2e4bf2b3b83 100644 --- a/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts +++ b/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts @@ -90,8 +90,8 @@ describe.runIf(isBuild)('build', () => { expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`) expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`) // inlined - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) }) test('worker emitted and import.meta.url in nested worker (build)', async () => { diff --git a/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts b/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts index f5034cbe6c0d05..780ce34df6795a 100644 --- a/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts +++ b/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts @@ -106,8 +106,8 @@ describe.runIf(isBuild)('build', () => { ) // inlined - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) expect(workerNestedWorkerContent).toMatch( `new Worker("/iife-sourcemap-hidden/assets/sub-worker`, diff --git a/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts b/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts index 3e7392b82d8a7b..10933fee2ff7ce 100644 --- a/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts +++ b/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts @@ -87,8 +87,8 @@ describe.runIf(isBuild)('build', () => { ) // inlined - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) expect(workerNestedWorkerContent).toMatch( `new Worker("/iife-sourcemap-inline/assets/sub-worker`, diff --git a/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts b/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts index e3e98c036852af..47905cd3d33673 100644 --- a/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts +++ b/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts @@ -107,8 +107,8 @@ describe.runIf(isBuild)('build', () => { ) // inlined - expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) - expect(content).toMatch(`window.Blob`) + expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`) + expect(content).toMatch(`self.Blob`) expect(workerNestedWorkerContent).toMatch( `new Worker("/iife-sourcemap/assets/sub-worker`,