From ff3cf0d24bf74dd5466f779de525d46c4f957569 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Tue, 19 Jul 2022 00:12:41 +0200 Subject: [PATCH 1/2] fix: worker relative base should use import.meta.url --- packages/vite/src/node/build.ts | 13 ++++++++++++- packages/vite/src/node/plugins/asset.ts | 14 ++------------ packages/vite/src/node/plugins/worker.ts | 12 +----------- .../relative-base/relative-base-worker.spec.ts | 5 +++-- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 64c32e616813ca..12f123413534a4 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -843,7 +843,7 @@ export function toOutputFilePathInString( toRelative: ( filename: string, hostType: string - ) => string | { runtime: string } + ) => string | { runtime: string } = toImportMetaURLBasedRelativePath ): string | { runtime: string } { const { renderBuiltUrl } = config.experimental let relative = config.base === '' || config.base === './' @@ -871,6 +871,17 @@ export function toOutputFilePathInString( return config.base + filename } +function toImportMetaURLBasedRelativePath( + filename: string, + importer: string +): { runtime: string } { + return { + runtime: `new URL(${JSON.stringify( + path.posix.relative(path.dirname(importer), filename) + )},import.meta.url).href` + } +} + export function toOutputFilePathWithoutRuntime( filename: string, type: 'asset' | 'public', diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 0403009d4b69c4..648b34f1d6d6f1 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -94,14 +94,6 @@ export function assetPlugin(config: ResolvedConfig): Plugin { let match: RegExpExecArray | null let s: MagicString | undefined - const toRelative = (filename: string, importer: string) => { - return { - runtime: `new URL(${JSON.stringify( - path.posix.relative(path.dirname(importer), filename) - )},import.meta.url).href` - } - } - // Urls added with JS using e.g. // imgElement.src = "__VITE_ASSET__5aa0ddc0__" are using quotes @@ -123,8 +115,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin { 'asset', chunk.fileName, 'js', - config, - toRelative + config ) const replacementString = typeof replacement === 'string' @@ -147,8 +138,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin { 'public', chunk.fileName, 'js', - config, - toRelative + config ) const replacementString = typeof replacement === 'string' diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 29c05a880d998c..fe2f209b915b60 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -144,15 +144,6 @@ function emitSourcemapForWorkerEntry( return chunk } -// TODO:base review why we aren't using import.meta.url here -function toStaticRelativePath(filename: string, importer: string) { - let outputFilepath = path.posix.relative(path.dirname(importer), filename) - if (!outputFilepath.startsWith('.')) { - outputFilepath = './' + outputFilepath - } - return outputFilepath -} - export const workerAssetUrlRE = /__VITE_WORKER_ASSET__([a-z\d]{8})__/g function encodeWorkerAssetFileName( @@ -343,8 +334,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { 'asset', chunk.fileName, 'js', - config, - toStaticRelativePath + config ) const replacementString = typeof replacement === 'string' diff --git a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts index ef8accdad72d99..ba5a9d0f78d383 100644 --- a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts +++ b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts @@ -68,8 +68,9 @@ describe.runIf(isBuild)('build', () => { expect(workerContent).not.toMatch(`import`) expect(workerContent).not.toMatch(`export`) // chunk - expect(content).toMatch(`new Worker("../worker-entries/`) - expect(content).toMatch(`new SharedWorker("../worker-entries/`) + console.log(content) + expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`) + expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`) // inlined expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) expect(content).toMatch(`window.Blob`) From 04bf0b20af49db0beb29042c812314b111de7407 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 20 Jul 2022 19:34:09 +0200 Subject: [PATCH 2/2] chore: update Co-authored-by: Tony Trinh --- .../worker/__tests__/relative-base/relative-base-worker.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts index ba5a9d0f78d383..89c042ba322b27 100644 --- a/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts +++ b/playground/worker/__tests__/relative-base/relative-base-worker.spec.ts @@ -68,7 +68,6 @@ describe.runIf(isBuild)('build', () => { expect(workerContent).not.toMatch(`import`) expect(workerContent).not.toMatch(`export`) // chunk - console.log(content) expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`) expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`) // inlined