From d6b73b1bbf0024ab93baa8577193408db9801f85 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Sat, 4 Jul 2020 10:34:20 +0200 Subject: [PATCH] fix(worker): use importScript() to load cross-origin worker --- src/compiler/bundle/worker-plugin.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/compiler/bundle/worker-plugin.ts b/src/compiler/bundle/worker-plugin.ts index 5dd014d14e2..910187b8f37 100644 --- a/src/compiler/bundle/worker-plugin.ts +++ b/src/compiler/bundle/worker-plugin.ts @@ -67,9 +67,14 @@ export const workerPlugin = ( const workerEntryPath = normalizeFsPath(id); const workerName = getWorkerName(workerEntryPath); const { code, dependencies, workerMsgId } = await getWorker(config, compilerCtx, buildCtx, this, workersMap, workerEntryPath); + const referenceId = this.emitFile({ + type: 'asset', + source: code, + name: workerName + '.js', + }); dependencies.forEach(id => this.addWatchFile(id)); return { - code: getInlineWorker(code, workerName, workerMsgId), + code: getInlineWorker(referenceId, workerName, workerMsgId), moduleSideEffects: false, }; } @@ -344,12 +349,15 @@ export const worker = /*@__PURE__*/createWorker(workerPath, workerName, workerMs `; }; -const getInlineWorker = (code: string, workerName: string, workerMsgId: string) => { +const getInlineWorker = (referenceId: string, workerName: string, workerMsgId: string) => { return ` import { createWorker } from '${WORKER_HELPER_ID}'; -const blob = new Blob([${JSON.stringify(code)}], { type: 'text/javascript' }); +export const workerName = '${workerName}'; +export const workerMsgId = '${workerMsgId}'; +export const workerPath = /*@__PURE__*/import.meta.ROLLUP_FILE_URL_${referenceId}; +const blob = new Blob(['importScripts("' + workerPath + '")'], { type: 'text/javascript' }); const url = URL.createObjectURL(blob); -export const worker = createWorker(url, '${workerName}', '${workerMsgId}'); +export const worker = /*@__PURE__*/createWorker(workerPath, workerName, workerMsgId); URL.revokeObjectURL(url); `; };