diff --git a/src/compiler/bundle/worker-plugin.ts b/src/compiler/bundle/worker-plugin.ts index 6b7703cfbca..164230c4957 100644 --- a/src/compiler/bundle/worker-plugin.ts +++ b/src/compiler/bundle/worker-plugin.ts @@ -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(); return { @@ -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); @@ -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}';