Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(worker): use importScript() to load cross-origin worker
  • Loading branch information
manucorporat committed Jul 4, 2020
1 parent 69ccbf1 commit d6b73b1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/compiler/bundle/worker-plugin.ts
Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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);
`;
};
Expand Down

0 comments on commit d6b73b1

Please sign in to comment.