fix(worker): nested inlined worker always fallbacked to data URI work… · vitejs/vite@07bc489 (original) (raw)
File tree
- packages/vite/src/node/plugins
- playground/worker/__tests__
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -305,7 +305,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { | ||
| 305 | 305 | workerConstructor === 'Worker' |
| 306 | 306 | ? `${encodedJs} |
| 307 | 307 | 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([${ | |
| 309 | 309 | workerType === 'classic' |
| 310 | 310 | ? '' |
| 311 | 311 | : // `URL` is always available, in `Worker[type="module"]` |
| @@ -314,11 +314,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { | ||
| 314 | 314 | export default function WorkerWrapper(options) { |
| 315 | 315 | let objURL; |
| 316 | 316 | try { |
| 317 | - objURL = blob && (window.URL | | |
| 317 | + objURL = blob && (self.URL | | |
| 318 | 318 | if (!objURL) throw '' |
| 319 | 319 | const worker = new workerConstructor(objURL,{workerConstructor}(objURL, workerConstructor(objURL,{workerTypeOption}); |
| 320 | 320 | worker.addEventListener("error", () => { |
| 321 | - (window.URL | | |
| 321 | + (self.URL | | |
| 322 | 322 | }); |
| 323 | 323 | return worker; |
| 324 | 324 | } catch(e) { |
| @@ -331,7 +331,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { | ||
| 331 | 331 | // otherwise the worker fails to run |
| 332 | 332 | workerType === 'classic' |
| 333 | 333 | ? ` finally { |
| 334 | - objURL && (window.URL | | |
| 334 | + objURL && (self.URL | | |
| 335 | 335 | }` |
| 336 | 336 | : '' |
| 337 | 337 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -127,10 +127,10 @@ describe.runIf(isBuild)('build', () => { | ||
| 127 | 127 | expect(content).toMatch(`new Worker("/es/assets`) |
| 128 | 128 | expect(content).toMatch(`new SharedWorker("/es/assets`) |
| 129 | 129 | // inlined worker |
| 130 | -expect(content).toMatch(`(window.URL| | |
| 131 | -expect(content).toMatch(`window.Blob`) | |
| 130 | +expect(content).toMatch(`(self.URL| | |
| 131 | +expect(content).toMatch(`self.Blob`) | |
| 132 | 132 | expect(content).toMatch( |
| 133 | -/try\{if\(\w+=\w+&&\(window\.URL\|\ | |
| 133 | +/try\{if\(\w+=\w+&&\(self\.URL\|\ | |
| 134 | 134 | ) |
| 135 | 135 | // inlined shared worker |
| 136 | 136 | expect(content).toMatch( |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -91,8 +91,8 @@ describe.runIf(isBuild)('build', () => { | ||
| 91 | 91 | expect(content).toMatch(`new Worker("/iife/assets`) |
| 92 | 92 | expect(content).toMatch(`new SharedWorker("/iife/assets`) |
| 93 | 93 | // inlined |
| 94 | -expect(content).toMatch(`(window.URL| | |
| 95 | -expect(content).toMatch(`window.Blob`) | |
| 94 | +expect(content).toMatch(`(self.URL| | |
| 95 | +expect(content).toMatch(`self.Blob`) | |
| 96 | 96 | }) |
| 97 | 97 | |
| 98 | 98 | test('worker emitted and import.meta.url in nested worker (build)', async () => { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -90,8 +90,8 @@ describe.runIf(isBuild)('build', () => { | ||
| 90 | 90 | expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`) |
| 91 | 91 | expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`) |
| 92 | 92 | // inlined |
| 93 | -expect(content).toMatch(`(window.URL| | |
| 94 | -expect(content).toMatch(`window.Blob`) | |
| 93 | +expect(content).toMatch(`(self.URL| | |
| 94 | +expect(content).toMatch(`self.Blob`) | |
| 95 | 95 | }) |
| 96 | 96 | |
| 97 | 97 | test('worker emitted and import.meta.url in nested worker (build)', async () => { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -106,8 +106,8 @@ describe.runIf(isBuild)('build', () => { | ||
| 106 | 106 | ) |
| 107 | 107 | |
| 108 | 108 | // inlined |
| 109 | -expect(content).toMatch(`(window.URL| | |
| 110 | -expect(content).toMatch(`window.Blob`) | |
| 109 | +expect(content).toMatch(`(self.URL| | |
| 110 | +expect(content).toMatch(`self.Blob`) | |
| 111 | 111 | |
| 112 | 112 | expect(workerNestedWorkerContent).toMatch( |
| 113 | 113 | `new Worker("/iife-sourcemap-hidden/assets/sub-worker`, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -87,8 +87,8 @@ describe.runIf(isBuild)('build', () => { | ||
| 87 | 87 | ) |
| 88 | 88 | |
| 89 | 89 | // inlined |
| 90 | -expect(content).toMatch(`(window.URL| | |
| 91 | -expect(content).toMatch(`window.Blob`) | |
| 90 | +expect(content).toMatch(`(self.URL| | |
| 91 | +expect(content).toMatch(`self.Blob`) | |
| 92 | 92 | |
| 93 | 93 | expect(workerNestedWorkerContent).toMatch( |
| 94 | 94 | `new Worker("/iife-sourcemap-inline/assets/sub-worker`, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -107,8 +107,8 @@ describe.runIf(isBuild)('build', () => { | ||
| 107 | 107 | ) |
| 108 | 108 | |
| 109 | 109 | // inlined |
| 110 | -expect(content).toMatch(`(window.URL| | |
| 111 | -expect(content).toMatch(`window.Blob`) | |
| 110 | +expect(content).toMatch(`(self.URL| | |
| 111 | +expect(content).toMatch(`self.Blob`) | |
| 112 | 112 | |
| 113 | 113 | expect(workerNestedWorkerContent).toMatch( |
| 114 | 114 | `new Worker("/iife-sourcemap/assets/sub-worker`, |