Skip to content

Commit

Permalink
fix(worker): mock worker instance for hydrate builds
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 27, 2020
1 parent 801f39e commit 207ce44
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/compiler/bundle/worker-plugin.ts
Expand Up @@ -4,18 +4,19 @@ import { bundleOutput } from './bundle-output';
import { normalizeFsPath, hasError } from '@utils';
import { optimizeModule } from '../optimize/optimize-module';

export const workerPlugin = (
config: d.Config,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
platform: string,
inlineWorkers: boolean
): Plugin => {
export const workerPlugin = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, platform: string, inlineWorkers: boolean): Plugin => {
if (platform === 'worker' || platform === 'hydrate') {
return {
name: 'workerPlugin',
transform(_, id) {
if (id.endsWith('?worker') || id.endsWith('?worker-inline')) {
return getMockedWorkerMain();
}
return null;
},
};
}

const workersMap = new Map<string, WorkerMeta>();

return {
Expand Down Expand Up @@ -62,7 +63,6 @@ export const workerPlugin = (
code: getWorkerMain(referenceId, workerName, workerMsgId),
moduleSideEffects: false,
};

} else if (id.endsWith('?worker-inline')) {
const workerEntryPath = normalizeFsPath(id);
const workerName = getWorkerName(workerEntryPath);
Expand Down Expand Up @@ -362,6 +362,18 @@ URL.revokeObjectURL(url);
`;
};

const getMockedWorkerMain = () => {
// for the hydrate build the workers won't actually work
// however, we still need to make the {worker} export
// kick-in otherwise bundling chokes
return `
export const workerName = 'mocked-worker';
export const workerMsgId = workerName;
export const workerPath = workerName;
export const worker = { name: workerName };
`;
};

const getWorkerProxy = (workerEntryPath: string, exportedMethods: string[]) => {
return `
import { createWorkerProxy } from '${WORKER_HELPER_ID}';
Expand Down

0 comments on commit 207ce44

Please sign in to comment.