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

feat: use import.meta.url instead of self.location #14377

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)`,
Comment on lines -183 to +184
Copy link
Member Author

Choose a reason for hiding this comment

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

/foo gets replaced with /@fs/foo by vite:asset-import-meta-url plugin. Maybe we should add /* @vite-ignore */ for new URL as well?

Copy link
Member

Choose a reason for hiding this comment

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

Using /* @vite-ignore */ works for me 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make a separate PR for that.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't that changing this code to use /* @vite-ignore */ instead, or is there more to it?

Copy link
Member Author

Choose a reason for hiding this comment

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

new URL(foo, import.meta.url) doesn't support /* @vite-ignore */ now. So adding support for that is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay I thought it's already supported. Good to merge then.

)
}

Expand Down
2 changes: 1 addition & 1 deletion playground/assets/index.html
Original file line number Diff line number Diff line change
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