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

import.meta.resolve with npm packages #21298

Closed
oscarotero opened this issue Nov 22, 2023 · 4 comments · Fixed by #21716
Closed

import.meta.resolve with npm packages #21298

oscarotero opened this issue Nov 22, 2023 · 4 comments · Fixed by #21716
Labels
bug Something isn't working correctly runtime Relates to code in the runtime crate
Milestone

Comments

@oscarotero
Copy link

Related: #16089

Every time I try to resolve a npm package:

const preact_specifier = import.meta.resolve("npm:preact");

I get the error:

Uncaught TypeError: "npm:" specifiers are currently not supported in import.meta.resolve()

I would rather the old behavior, returning simply npm:preact. This error is very unfortunated because now it's not possible to get the final specifier if the specifier is mapped in the import map:

{
  "imports": {
    "npm:preact": "https://esm.sh/preact"
  }
}

I guess this is the intended behavior would like to know the explanation for that decision.

@guybedford
Copy link
Contributor

Strictly speaking, the specifier being loaded in the module registry is npm:preact in this case (and so far as it only has one representation in a given Deno application lifetime). So that this does sound like the sensible behaviour to me, while still treating the real cache location etc as an internal implementation detail.

@oscarotero
Copy link
Author

To clarify, the sensible behavior to me is:

import.meta.resolve("npm:preact"); // Returns "npm:preact"

With this import map:

{
  "imports": {
    "npm:preact": "https://esm.sh/preact"
  }
}
import.meta.resolve("npm:preact"); // Returns "https://esm.sh/preact"

@lucacasonato
Copy link
Member

I agree.

@lucacasonato lucacasonato added bug Something isn't working correctly runtime Relates to code in the runtime crate labels Nov 23, 2023
@bartlomieju
Copy link
Member

Opened denoland/deno_core#354 that will make it easier to customize.

bartlomieju added a commit to denoland/deno_core that referenced this issue Nov 27, 2023
This commits adds "RuntimeOptions::import_meta_resolve_callback" option
that allows to customize behavior of `import.meta.resolve` API.

Required for denoland/deno#21298.
@bartlomieju bartlomieju added this to the 1.40 milestone Dec 17, 2023
bartlomieju added a commit that referenced this issue Dec 28, 2023
Closes #21298.

"npm:" specifiers are matched against import map entries
and if no match is found they are passed through.
bartlomieju added a commit that referenced this issue Jan 4, 2024
Closes #21298.

"npm:" specifiers are matched against import map entries
and if no match is found they are passed through.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly runtime Relates to code in the runtime crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants