Skip to content

Commit

Permalink
fix: worker relative base should use import.meta.url (#9204)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jul 22, 2022
1 parent d420f01 commit 0358b04
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
13 changes: 12 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === './'
Expand Down Expand Up @@ -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',
Expand Down
14 changes: 2 additions & 12 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -123,8 +115,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
'asset',
chunk.fileName,
'js',
config,
toRelative
config
)
const replacementString =
typeof replacement === 'string'
Expand All @@ -147,8 +138,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
'public',
chunk.fileName,
'js',
config,
toRelative
config
)
const replacementString =
typeof replacement === 'string'
Expand Down
12 changes: 1 addition & 11 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -343,8 +334,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
'asset',
chunk.fileName,
'js',
config,
toStaticRelativePath
config
)
const replacementString =
typeof replacement === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down

0 comments on commit 0358b04

Please sign in to comment.