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

Support import.meta.resolve(…) #14500

Open
4 tasks done
lgarron opened this issue Sep 28, 2023 · 4 comments
Open
4 tasks done

Support import.meta.resolve(…) #14500

lgarron opened this issue Sep 28, 2023 · 4 comments

Comments

@lgarron
Copy link
Contributor

lgarron commented Sep 28, 2023

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

@sapphi-red
Copy link
Member

Recently I wrote down some thoughts: #14405

IMO it'd be nice to implement this as a plugin first before including it to the core.

@lgarron
Copy link
Contributor Author

lgarron commented Sep 29, 2023

Recently I wrote down some thoughts: #14405

ooooh, glad to see it! 🤩

IMO it'd be nice to implement this as a plugin first before including it to the core.

That does look like that's been a good prototyping path in the past!

For what it's worth, as a library author I have two main concerns:

  • There is no single interoperable way to publish web worker instantiation code that works with all bundlers out of the box. new URL(…, import.meta.url) got close, but not quite.
  • I can put a lot of work into using standardized vanilla JS workarounds, but people will still ask me what went wrong if my workarounds cause other interoperability issues.

Now that import.meta.resolve(…) is the interoperable solution supported by all browsers and runtimes, it would save me lot of headaches for it to work out of the box in a few major bundlers (particularly Vite). So if there's something that makes it as easy as possible to land in the core initially (e.g. leaving out baseURL support initially), this would help wrap up over a decade of pain for library authors like me.

I'd also be happy help author a plugin or a PR if it helps, but unfortunately I wouldn't be able to commit any time to ongoing maintenance.

@Swivelgames
Copy link

Swivelgames commented Oct 11, 2023

@sapphi-red I would have to agree with @lgarron on this one. This is core language feature. And the benefits of including import.meta.resolve() as a first-class citizen to the core, rather than an afterthought, would resolve some of the issues that people have run into with interoperability issues. Not to mention that, in reality, it may never end up making it in to the core if it's introduced as a plugin now. It would save a lot of headache down the road if it was included in core from the get-go.

@lgarron
Copy link
Contributor Author

lgarron commented Oct 12, 2023

I drafted a PR for this by adapting assetImportMetaUrl.ts. Unfortunately, it's not very effective:

Also, #11823 is rather funny. 😄

I could use suggestions on a path forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants