Skip to content

Commit

Permalink
fix: use global location for import.meta.url rewrite (fix #5087) (#5113)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 28, 2021
1 parent 9db3005 commit 0b54853
Showing 1 changed file with 5 additions and 3 deletions.
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

0 comments on commit 0b54853

Please sign in to comment.