Skip to content

Support bundling modules referenced by import.meta.resolve(…) #14500

@lgarron

Description

@lgarron

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.url in different ways, which can cause compatibility bugs. It would be nice to avoid the need to publish code that import.meta.url for 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 that new URL("bare-import", import.meta.url) has two meanings (import a relative file named e.g. bare-import.js, or import the package bare-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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions