From 9e867450298b1adc851d6f89f361c39ebb05a738 Mon Sep 17 00:00:00 2001 From: yoho Date: Thu, 14 Apr 2022 11:35:01 +0800 Subject: [PATCH] refactor: stop replace import.meta.url to self.location.href --- .../worker/__tests__/es/es-worker.spec.ts | 11 -------- .../worker/__tests__/iife/worker.spec.ts | 11 -------- packages/playground/worker/sub-worker.js | 2 +- packages/playground/worker/url-worker.js | 2 +- .../playground/worker/worker-nested-worker.js | 2 +- packages/vite/src/node/build.ts | 2 -- packages/vite/src/node/plugins/worker.ts | 27 ------------------- 7 files changed, 3 insertions(+), 54 deletions(-) diff --git a/packages/playground/worker/__tests__/es/es-worker.spec.ts b/packages/playground/worker/__tests__/es/es-worker.spec.ts index 77e8f6394a0f09..e68081c413471f 100644 --- a/packages/playground/worker/__tests__/es/es-worker.spec.ts +++ b/packages/playground/worker/__tests__/es/es-worker.spec.ts @@ -85,17 +85,6 @@ if (isBuild) { }) test('worker emitted and import.meta.url in nested worker (build)', async () => { - // import.meta.url will minify in esbuild in build mode so can't use runtime result. - const files = fs.readdirSync(assetsDir) - const nestedWorkerFile = files.find((f) => - f.includes('worker-nested-worker') - ) - const content = fs.readFileSync( - path.resolve(assetsDir, nestedWorkerFile), - 'utf-8' - ) - expect(content).toMatch('self.location.href') - expect(await page.textContent('.nested-worker-module')).toMatch( '"type":"module"' ) diff --git a/packages/playground/worker/__tests__/iife/worker.spec.ts b/packages/playground/worker/__tests__/iife/worker.spec.ts index 0e5e09dbd04b19..9e7bd6ca9ce79d 100644 --- a/packages/playground/worker/__tests__/iife/worker.spec.ts +++ b/packages/playground/worker/__tests__/iife/worker.spec.ts @@ -85,17 +85,6 @@ if (isBuild) { }) test('worker emitted and import.meta.url in nested worker (build)', async () => { - // import.meta.url will minify in esbuild in build mode so can't use runtime result. - const files = fs.readdirSync(assetsDir) - const nestedWorkerFile = files.find((f) => - f.includes('worker-nested-worker') - ) - const content = fs.readFileSync( - path.resolve(assetsDir, nestedWorkerFile), - 'utf-8' - ) - expect(content).toMatch('self.location.href') - expect(await page.textContent('.nested-worker-module')).toMatch( '"type":"module"' ) diff --git a/packages/playground/worker/sub-worker.js b/packages/playground/worker/sub-worker.js index add7cf734b3503..37d5d4effabb27 100644 --- a/packages/playground/worker/sub-worker.js +++ b/packages/playground/worker/sub-worker.js @@ -1,6 +1,6 @@ self.onmessage = (event) => { if (event.data === 'ping') { - self.postMessage(`pong ${import.meta.url}`) + self.postMessage(`pong ${self.location.href}`) } } diff --git a/packages/playground/worker/url-worker.js b/packages/playground/worker/url-worker.js index d850a2190a75b7..1ba50225ee339d 100644 --- a/packages/playground/worker/url-worker.js +++ b/packages/playground/worker/url-worker.js @@ -1,4 +1,4 @@ -self.postMessage('A string' + import.meta.env.BASE_URL + import.meta.url) +self.postMessage('A string' + import.meta.env.BASE_URL + self.location.url) // for sourcemap console.log('url-worker.js') diff --git a/packages/playground/worker/worker-nested-worker.js b/packages/playground/worker/worker-nested-worker.js index 1fb851539f1090..e74d1db760409b 100644 --- a/packages/playground/worker/worker-nested-worker.js +++ b/packages/playground/worker/worker-nested-worker.js @@ -8,7 +8,7 @@ self.onmessage = (event) => { } } -self.postMessage(import.meta.url) +self.postMessage(self.location.href) subWorker.onmessage = (ev) => { self.postMessage({ diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 9ad8a640a12f7b..ff03352a20d7a7 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -40,7 +40,6 @@ import { assetImportMetaUrlPlugin } from './plugins/assetImportMetaUrl' import { loadFallbackPlugin } from './plugins/loadFallback' import { watchPackageDataPlugin } from './packages' import { ensureWatchPlugin } from './plugins/ensureWatch' -import { redirectWorkerImportMetaUrlPlugin } from './plugins/worker' export interface BuildOptions { /** @@ -316,7 +315,6 @@ export function resolveBuildPlugins(config: ResolvedConfig): { dataURIPlugin(), dynamicImportVars(options.dynamicImportVarsOptions), assetImportMetaUrlPlugin(config), - redirectWorkerImportMetaUrlPlugin(config), ...(options.rollupOptions.plugins ? (options.rollupOptions.plugins.filter(Boolean) as Plugin[]) : []) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 14298b25928473..40d76dd81d5fea 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -7,7 +7,6 @@ import { ENV_PUBLIC_PATH } from '../constants' import path from 'path' import { onRollupWarning } from '../build' import type { TransformPluginContext, EmittedFile } from 'rollup' -import MagicString from 'magic-string' interface WorkerCache { // save worker bundle emitted files avoid overwrites the same file. @@ -287,29 +286,3 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } } } - -// just run in build mode -export function redirectWorkerImportMetaUrlPlugin( - config: ResolvedConfig -): Plugin { - const isWorker = config.isWorker - - return { - name: 'vite:workerImportMetaUrl', - transform(code, id) { - if (isWorker) { - // if build with iife it will polyfill with `rollup` will be used document in the worker - // else if build with es it will replace with `esbuild` will got a unexpected data. - // so replace import.meta.url break the default handle. - // And it must be done at this (after `(?new Worker)new URL('xxx', import.meta.url)` match), - // Otherwise, the following regex will be incorrectly matched. - const s = new MagicString(code, {}) - s.replace(/\bimport.meta.url\b/g, 'self.location.href') - return { - code: s.toString(), - map: s.generateMap({ source: id, hires: true }) - } - } - } - } -}