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: use global location for import.meta.url rewrite (fix #5087) #5113

Merged
merged 2 commits into from Sep 28, 2021
Merged
Changes from all commits
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
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -46,13 +46,15 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
if (templateLiteral.expressions.length) {
const pattern = buildGlobPattern(templateLiteral)
// Note: native import.meta.url is not supported in the baseline
// target so we use window.location here -
// target so we use the global location here. It can be
// window.location or self.location in case it is used in a Web Worker.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self
s.overwrite(
index,
index + exp.length,
`new URL(import.meta.globEagerDefault(${JSON.stringify(
pattern
)})[${rawUrl}], window.location)`
)})[${rawUrl}], self.location)`
)
continue
}
Expand All @@ -64,7 +66,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
s.overwrite(
index,
index + exp.length,
`new URL(${JSON.stringify(builtUrl)}, window.location)`
`new URL(${JSON.stringify(builtUrl)}, self.location)`
)
}
if (s) {
Expand Down