-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add import.meta.resolve() #5572
Conversation
This looks like a good start. A useful feature can be to permit a second argument to I can provide some spec along these lines if you'd be open to it? |
I'd like to get the basic version out there first. Adding more features is a sure way to prevent this one from landing. |
These have proven over time to be much less interesting than other future extensions we might spend time on. In particular: * Give up on fallback support. Closes #76, closes #79, closes #83, closes #84. Also closes #79 by removing all the potential complexity there; we can instead discuss on whatwg/html#3871 and whatwg/html#5572. * Give up on import: URLs. Closes #71, closes #149. * Give up on built-in module remapping.
In principle this sounds reasonable to me. Would also like to hear from @jasnell, @lucacasonato, and @codehag. Do we have some kind of place where we document extensions to |
Yeah, I'm +1 on this. It'll be quite useful, I think. |
I have no strong opinion (maybe +0.1?). Not opposed, but I know some Node folks want this to be async. We are fine with it being sync (and would want it to be sync). |
I don't see an issue with introducing this. I think this would be useful to test import maps, and i imagine it will be useful in such a world were import maps are the norm? |
fa07fc6
to
f91ff0e
Compare
Closes #3871.
f91ff0e
to
6cc21d3
Compare
Awesome, thanks all for the review. I'll plan to merge this after I'm a bit more confident in the tests; I have some up initially at https://chromium-review.googlesource.com/c/chromium/src/+/3456729 but that CL is messy enough that I don't trust them yet. |
Hey, can we slow this down just a bit? I’m a bit concerned with this being sync, since in Node the resolution step is async (and needs to remain so, or it would break various loaders use cases). Taking a step back, for the non-bare case isn’t And the bare case, |
I think a 7-month incubation period is slow enough? It's ok for Node to use a different API than the web here. This is different than the URL constructor because import maps, indeed. There is no plan to make that async; we never want to introduce network calls into module resolution on the web. |
Put another way, is there some advantage in making this sync? If it’s async then we can maintain compatibility with Node or any platform that wants to allow customization in module resolution (and for that custom resolution to potentially be async). |
Aside: was whatwg/js-hosts dissolved due to the creation of the Winter CG? I was trying to make sense of the status of this in light of mozilla/standards-positions#647 and it seems like it's roughly at where we left it in February. Node.js can make this shape work, but isn't thrilled about it. Deno doesn't necessarily have a need, but if it's added it should be this shape. And browser folks being somewhat positive given that it matches how we resolve JS module specifiers. Is that accurate? |
I believe that is the case, yes.
That's accurate. |
To add to this, please read this detailed comment about the downsides of Node making |
From my perspective, that comment displays some fundamental confusion between the resolve phases (a string operation) and the fetch phases (which includes things like checking for existence, permissions, network requests, etc.) |
In Node, the resolve “phase” includes disk operations. Going back to CommonJS, |
See whatwg/html#5572 Bug: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
Hi @domenic — sorry for this being quite a delayed reply (I wasn't aware of this thread which linked to my other comment) I'd like to clarify the use-case which led me to my other comment, which should help explain the bits which you think of as confusion on my part: In NodeJS's current system, these imports could all resolve to the same file: import 'bar';
import 'bar/index'; // or something else depending on bar's package.json
import './node_modules/bar/index.js'; And It's important to know all that before loading the module, because if they resolve to the same thing, we need to use the same instance of that module (e.g. so that any user-land global state is truly global). Once we get to the point of actually loading the module, it's too late. So we need Now you may say that's a problem with NodeJS's Import maps have of course been created to fill this need in the browser (with the intent being that all this information which was previously gathered by polling the filesystem is now available declaratively in-advance), but they are at an early stage (and only implemented in one runtime), so I for one cannot yet rely on them for this use-case (even in an experimental capacity). I could also imagine a future need to lazy-load or even dynamically-generate import maps, especially in local environments like Node where the number of entries could be vast. Even in-browser I could imagine the need to fetch (sub) import maps on-demand once nesting / combining of maps gets more specced out, which would make the My preference (as a userspace developer with a vested interest in keeping parity between browsers and NodeJS) is that |
FYI, the compartments proposal at TC39 (https://github.com/tc39/proposal-compartments) also defines specifier resolving as being a synchronous operation. (grep for |
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Bug: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529}
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529}
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529}
Automatic update from web-platform-tests Implement import.meta.resolve() See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529} -- wpt-commits: edcc51d740e71ace04d1fe1a4ae107ea3e640c2a wpt-pr: 34188
Automatic update from web-platform-tests Implement import.meta.resolve() See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529} -- wpt-commits: edcc51d740e71ace04d1fe1a4ae107ea3e640c2a wpt-pr: 34188
See whatwg/html#5572. Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74 Fixed: 1296665 Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3456729 Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Hiroshige Hayashizaki <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1021529} NOKEYCHECK=True GitOrigin-RevId: b2f7928219b78bf917e8ae1355d3b43f3ca85014
Closes #3871.
This introduces a new function,
import.meta.resolve(moduleSpecifier)
, which returns the URL to which a given module specifier would resolve in the context of the current script. That is, it returns the URL that would be imported if you didimport(moduleSpecifier)
.It will throw an exception if an invalid specifier is given (including one not mapped to anywhere by an import map).
The main use case for this is to get URLs relative to the current script, or relative to a current page. For example:
or
(See WHATWG Working Mode: Changes for more details.)
/infrastructure.html ( diff )
/webappapis.html ( diff )