-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Support bundling modules referenced by import.meta.resolve(…) #14500
Copy link
Copy link
Open
Labels
Description
Description
Vite and other bundlers support using new URL("path", import.meta.url) to resolve relative paths.
This was a useful convention for a while, but now we have import.meta.resolve(…) to express a relative path resolution.
It is supported by all major browsers and all major runtimes.
Suggested solution
Recognize and transpile import.meta.resolve(…) as part of the import graph. It would be pretty similar to dynamic import(…) but without actually importing code.
- For recent target environments it can be left as
import.meta.resolve(…)(just transform the argument). - For older target environments, it can be transpiled down to
new URL(…, import.meta.url).import.meta.resolve(…)is not an ECMAScript feature, but I would recommend classifying it as ES2022 or ES2023 since all major browsers and runtimes landed support over the last year.
Alternative
The existing new URL(…, import.meta.url) support could be considered sufficient. However, this has significant drawbacks for library authors:
- For web worker code, it requires an instantiation attempts for each relative path resolution fallback. These can be slow. If you want your library to work on a CDN, you also have to perform each attempt a second time using a trampoline. My library currently performs seven attempted worker instantiations, but it could be reduced to one or two if bundlers supported
import.meta.resolve(…). - Bundlers transform
import.meta.urlin different ways, which can cause compatibility bugs. It would be nice to avoid the need to publish code thatimport.meta.urlfor relative path resolution. - Going by the spec,
new URL(…, import.meta.url)does not support bare imports. However, some bundlers overload the syntax to allow this. This means thatnew URL("bare-import", import.meta.url)has two meanings (import a relative file named e.g.bare-import.js, or import the packagebare-import), which means that a library install can break unrelated working code due to ambiguity.import.meta.resolve(…)supports package (or import map) paths without ambiguity.
Additional context
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Reactions are currently unavailable