From d67aac4d9a68888eb579ad6b5967938f02c8e88a Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 2 Apr 2023 12:41:26 +0800 Subject: [PATCH] refactor: add webWorkerPostPlugin to resolveBuildPlugins --- packages/vite/src/node/build.ts | 2 ++ packages/vite/src/node/plugins/worker.ts | 28 ++++++++++--------- .../worker/__tests__/iife/iife-worker.spec.ts | 9 ++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 5f980bb130a961..1c65e087e97178 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -58,6 +58,7 @@ import { ESBUILD_MODULES_TARGET, VERSION } from './constants' import { resolveChokidarOptions } from './watch' import { completeSystemWrapPlugin } from './plugins/completeSystemWrap' import { mergeConfig } from './publicUtils' +import { webWorkerPostPlugin } from './plugins/worker' export interface BuildOptions { /** @@ -445,6 +446,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ : [rollupOptionsPlugins], ) ).filter(Boolean) as Plugin[]), + ...(config.isWorker ? [webWorkerPostPlugin()] : []), ], post: [ buildImportAnalysisPlugin(config), diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 9a3c5e18a41ab0..b26a549a7dcf2a 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -64,18 +64,6 @@ export async function bundleWorkerEntry( return promise } -const workerPostPlugin: Plugin = { - name: 'vite:worker-post', - resolveImportMeta(property, { chunkId, format }) { - // document is undefined in the worker, so we need to avoid it in iife - if (property === 'url' && format === 'iife') { - return `new URL('${chunkId}', self.location.href).href` - } - - return null - }, -} - async function serialBundleWorkerEntry( config: ResolvedConfig, id: string, @@ -87,7 +75,7 @@ async function serialBundleWorkerEntry( const bundle = await rollup({ ...rollupOptions, input: cleanUrl(id), - plugins: plugins.concat(workerPostPlugin), + plugins, onwarn(warning, warn) { onRollupWarning(warning, warn, config) }, @@ -197,6 +185,20 @@ export async function workerFileToUrl( return encodeWorkerAssetFileName(fileName, workerMap) } +export function webWorkerPostPlugin(): Plugin { + return { + name: 'vite:worker-post', + resolveImportMeta(property, { chunkId, format }) { + // document is undefined in the worker, so we need to avoid it in iife + if (property === 'url' && format === 'iife') { + return `new URL('${chunkId}', self.location.href).href` + } + + return null + }, + } +} + export function webWorkerPlugin(config: ResolvedConfig): Plugin { const isBuild = config.command === 'build' let server: ViteDevServer diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts index d24e37ab4adc8b..196e8c9bece24b 100644 --- a/playground/worker/__tests__/iife/iife-worker.spec.ts +++ b/playground/worker/__tests__/iife/iife-worker.spec.ts @@ -84,16 +84,19 @@ describe.runIf(isBuild)('build', () => { test('module worker', async () => { await untilUpdated( - () => page.textContent('.worker-import-meta-url'), - /A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/, + async () => page.textContent('.worker-import-meta-url'), + /A\sstring.*\/iife\/.+url-worker\.js/, + true, ) await untilUpdated( () => page.textContent('.worker-import-meta-url-resolve'), - /A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/, + /A\sstring.*\/iife\/.+url-worker\.js/, + true, ) await untilUpdated( () => page.textContent('.shared-worker-import-meta-url'), 'A string', + true, ) })