module: change default resolver to not throw on unknown scheme#47824
Merged
nodejs-github-bot merged 12 commits intoMay 19, 2023
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an example toy loader that supports HTTP[S]. This code should work (in a simplistic way):
Unfortunately, this won't work. If you use it, you will get an
ERR_UNSUPPORTED_ESM_URL_SCHEMEbecause the default Node.js resolver doesn't support HTTPS, and throws on unknown schemes. So unfortunately I had to write that this loader needs a resolver, even though the only thing it does is support loading HTTPS. And the resolver I had to wrote was non-trivial, and IIUC replicates some of what the default resolver does:Thinking about it more, I realized that the only reason I need to write this resolver is that Node.js doesn't recognize the HTTP scheme in its resolver. But if Node.js would have not recognized the scheme in the loader, then I wouldn't have needed to write this resolver at all.
So my point is this: both checks for unknown scheme and unknown file extension should move from the default Node.js resolver to the default Node.js loader. This will obviate the need for some loaders to implement a
resolvewhich mostly replicates the Node.js logic, just for the sake of avoiding these exceptions.This PR implements exactly that.
Fixes nodejs/loaders#138
Notable change:
The ESM loader no longer throws on unknown protocol/file extension in the resolve phase, so
import.meta.resolvebehavior matches the behavior of other runtimes. This change is potentially for loader hooks authors that rely on the old behavior.