From b4316f0c813e3375ae33563997a684d6a44fdcae Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 19 May 2021 14:03:20 +0200 Subject: [PATCH 1/5] Fix inline webworker safari support --- packages/vite/src/node/plugins/worker.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index e9091b7d1815d5..0ac81713d6ad67 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -43,7 +43,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { let url: string if (isBuild) { if (query.inline != null) { - // bundle the file as entry to support imports and inline as base64 + // bundle the file as entry to support imports and inline as blob // data url const rollup = require('rollup') as typeof Rollup const bundle = await rollup.rollup({ @@ -55,9 +55,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { format: 'es', sourcemap: config.build.sourcemap }) - url = `data:application/javascript;base64,${Buffer.from( - output[0].code - ).toString('base64')}` + + return `export default function WorkerWrapper() { + const blob = new Blob([atob(\"${Buffer.from(output[0].code).toString('base64')}\")], { type: 'text/javascript;charset=utf-8' }); + return new Worker((window.URL || window.webkitURL).createObjectURL(blob)); + }` } finally { await bundle.close() } From 662e9090b51be5b4c5cc3965ca66a8cc71e407a3 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 19 May 2021 14:40:54 +0200 Subject: [PATCH 2/5] Update worker.spec.ts --- packages/playground/worker/__tests__/worker.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index eb0d457754209d..2b271f4025753a 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -28,6 +28,6 @@ if (isBuild) { // chunk expect(content).toMatch(`new Worker("/assets`) // inlined - expect(content).toMatch(`new Worker("data:application/javascript`) + expect(content).toMatch(`new Worker((window.URL||window.webkitURL)`) }) } From 57bdd4f253171c152d4686a9b367c8f5e7a7da37 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 20 May 2021 18:38:19 +0200 Subject: [PATCH 3/5] add revokeObjectURL --- packages/vite/src/node/plugins/worker.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 0ac81713d6ad67..c061bc38ec224d 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -56,9 +56,14 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { sourcemap: config.build.sourcemap }) - return `export default function WorkerWrapper() { - const blob = new Blob([atob(\"${Buffer.from(output[0].code).toString('base64')}\")], { type: 'text/javascript;charset=utf-8' }); - return new Worker((window.URL || window.webkitURL).createObjectURL(blob)); + return `const blob = new Blob([atob(\"${Buffer.from(output[0].code).toString('base64')}\")], { type: 'text/javascript;charset=utf-8' }); + export default function WorkerWrapper() { + const objURL = (window.URL || window.webkitURL).createObjectURL(blob); + try { + return new Worker(objURL); + } finally { + (window.URL || window.webkitURL).revokeObjectURL(objURL); + } }` } finally { await bundle.close() From c1939c02d6fdc4fab959553fb5edfe4a547fd575 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 20 May 2021 18:40:05 +0200 Subject: [PATCH 4/5] Update worker.spec.ts --- packages/playground/worker/__tests__/worker.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index 2b271f4025753a..bde4790ca84b02 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -28,6 +28,6 @@ if (isBuild) { // chunk expect(content).toMatch(`new Worker("/assets`) // inlined - expect(content).toMatch(`new Worker((window.URL||window.webkitURL)`) + expect(content).toMatch(`new Worker(objURL`) }) } From 2cfe0362ee459bb8ce0239307b60d88c7eb5517d Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 21 May 2021 15:32:33 +0200 Subject: [PATCH 5/5] Update worker.spec.ts --- packages/playground/worker/__tests__/worker.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index bde4790ca84b02..a096472547df46 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -28,6 +28,6 @@ if (isBuild) { // chunk expect(content).toMatch(`new Worker("/assets`) // inlined - expect(content).toMatch(`new Worker(objURL`) + expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) }) }