From 69f048b7afc81065471b16e14a65789190a560aa Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 28 Mar 2023 18:49:36 +0800 Subject: [PATCH 1/4] fix(worker): worker import.meta.url should not depends on `document` in iife mode --- packages/vite/src/node/plugins/worker.ts | 14 +++++++++++++- playground/test-utils.ts | 9 +++++++-- .../worker/__tests__/iife/iife-worker.spec.ts | 4 ++-- playground/worker/url-worker.js | 9 ++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 7fd7e328d6c301..f859d7a2b0cdee 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -64,6 +64,18 @@ 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 iife generate it + if (property === 'url' && format === 'iife') { + return `new URL('${chunkId}', self.location.href).href` + } + + return null + }, +} + async function serialBundleWorkerEntry( config: ResolvedConfig, id: string, @@ -75,7 +87,7 @@ async function serialBundleWorkerEntry( const bundle = await rollup({ ...rollupOptions, input: cleanUrl(id), - plugins, + plugins: plugins.concat(workerPostPlugin), onwarn(warning, warn) { onRollupWarning(warning, warn, config) }, diff --git a/playground/test-utils.ts b/playground/test-utils.ts index f791d534c0c2ad..0a44449ecefb41 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -152,14 +152,19 @@ export function readManifest(base = ''): Manifest { */ export async function untilUpdated( poll: () => string | Promise, - expected: string, + expected: string | RegExp, runInBuild = false, ): Promise { if (isBuild && !runInBuild) return const maxTries = process.env.CI ? 200 : 50 for (let tries = 0; tries < maxTries; tries++) { const actual = (await poll()) ?? '' - if (actual.indexOf(expected) > -1 || tries === maxTries - 1) { + if ( + (typeof expected === 'string' + ? actual.indexOf(expected) > -1 + : actual.match(expected)) || + tries === maxTries - 1 + ) { expect(actual).toMatch(expected) break } else { diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts index f37c70643f5fc4..d24e37ab4adc8b 100644 --- a/playground/worker/__tests__/iife/iife-worker.spec.ts +++ b/playground/worker/__tests__/iife/iife-worker.spec.ts @@ -85,11 +85,11 @@ describe.runIf(isBuild)('build', () => { test('module worker', async () => { await untilUpdated( () => page.textContent('.worker-import-meta-url'), - 'A string', + /A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/, ) await untilUpdated( () => page.textContent('.worker-import-meta-url-resolve'), - 'A string', + /A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/, ) await untilUpdated( () => page.textContent('.shared-worker-import-meta-url'), diff --git a/playground/worker/url-worker.js b/playground/worker/url-worker.js index 1ba50225ee339d..bede21cc42cc49 100644 --- a/playground/worker/url-worker.js +++ b/playground/worker/url-worker.js @@ -1,4 +1,11 @@ -self.postMessage('A string' + import.meta.env.BASE_URL + self.location.url) +self.postMessage( + [ + 'A string', + import.meta.env.BASE_URL, + self.location.url, + import.meta.url, + ].join(' '), +) // for sourcemap console.log('url-worker.js') From 8a76fecfdcf81873af71e92cdea74c8561415f9e Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 29 Mar 2023 21:32:52 +0200 Subject: [PATCH 2/4] chore: comment wording --- packages/vite/src/node/plugins/worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index f859d7a2b0cdee..9a3c5e18a41ab0 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -67,7 +67,7 @@ export async function bundleWorkerEntry( const workerPostPlugin: Plugin = { name: 'vite:worker-post', resolveImportMeta(property, { chunkId, format }) { - // document is undefined in the worker, so we need to avoid iife generate it + // 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` } From d67aac4d9a68888eb579ad6b5967938f02c8e88a Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 2 Apr 2023 12:41:26 +0800 Subject: [PATCH 3/4] 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, ) }) From fdf13b2e9459884b6978b3dc08055b6c6d632cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 2 Apr 2023 16:45:39 +0900 Subject: [PATCH 4/4] fix: use `self.location.href` --- packages/vite/src/node/plugins/worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index b26a549a7dcf2a..4293b494937312 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -191,7 +191,7 @@ export function webWorkerPostPlugin(): Plugin { 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 'self.location.href' } return null