Skip to content

Commit

Permalink
feat: use import.meta.url instead of self.location (#14377)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Sep 15, 2023
1 parent 55a3b4f commit e9b1e85
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
8 changes: 2 additions & 6 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -92,18 +92,14 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
// A hack to allow 'as' & 'query' exist at the same time
query: injectQuery(queryString, 'url'),
}
// Note: native import.meta.url is not supported in the baseline
// 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.update(
index,
index + exp.length,
`new URL((import.meta.glob(${JSON.stringify(
pattern,
)}, ${JSON.stringify(
globOptions,
)}))[${pureUrl}], self.location)`,
)}))[${pureUrl}], import.meta.url)`,
)
continue
}
Expand Down Expand Up @@ -154,7 +150,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
s.update(
index,
index + exp.length,
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
`new URL(${JSON.stringify(builtUrl)}, import.meta.url)`,
)
}
if (s) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -180,7 +180,8 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
s.update(
urlIndex,
urlIndex + exp.length,
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
// add `'' +` to skip vite:asset-import-meta-url plugin
`new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion playground/assets/index.html
Expand Up @@ -418,7 +418,7 @@ <h3>assets in noscript</h3>
document.querySelector('.import-meta-url-dep-img').src = metaUrlDep

// testing URLs for public assets served at the public base path
// equivalent to `new URL(`${import.meta.env.BASE_URL}/icon.png`, self.location)
// equivalent to `new URL(`${import.meta.env.BASE_URL}/icon.png`, import.meta.url)
const metaUrlBasePath = new URL('/icon.png', import.meta.url)
text('.import-meta-url-base-path', metaUrlBasePath)
document.querySelector('.import-meta-url-base-path-img').src = metaUrlBasePath
Expand Down

0 comments on commit e9b1e85

Please sign in to comment.