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..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,8 +68,8 @@ 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/`) + 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`)