Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: worker relative base should use import.meta.url #9204

Merged
merged 2 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/vite/src/node/build.ts
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
)},import.meta.url).href`
)}, import.meta.url).href`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Is the return value supposed to be minified output, where that space is intentionally omitted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I think we could add the space. It will be minified later 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should review all these if we do in a separate PR (there are several snippets like this in the code base)

}
}

export function toOutputFilePathWithoutRuntime(
filename: string,
type: 'asset' | 'public',
Expand Down
14 changes: 2 additions & 12 deletions packages/vite/src/node/plugins/asset.ts
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
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
Expand Up @@ -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)
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
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