Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add import.meta.resolve()#5572

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
domenic merged 1 commit intomainfromimport-meta-resolve
Jun 30, 2022
Merged

Add import.meta.resolve()#5572

domenic merged 1 commit intomainfromimport-meta-resolve
Jun 30, 2022

Conversation

domenic
Copy link
Member

@domenicdomenic commentedMay 21, 2020
edited
Loading

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:

// Inside https://example.com/my/deep/path/app.mjsimport.meta.resolve("./foo.txt");// -> "https://example.com/my/deep/path/foo.txt"

or

import.meta.resolve("somepackage/resource.json")// -> "https://example.com/node_modules/somepackage/lib/resource.json", based on// an import map that sends "somepackage/" to "https://example.com/node_modules/somepackage/lib/".

(SeeWHATWG Working Mode: Changes for more details.)


/infrastructure.html (diff )
/webappapis.html (diff )

stefanpenner, bathos, thescientist13, lhwdev, mathiasbynens, caridy, and dmail reacted with hooray emojibrainkim, thescientist13, and lhwdev reacted with eyes emoji
@domenicdomenic added addition/proposalNew features or enhancements needs implementer interestMoving the issue forward requires implementers to express interest needs testsMoving the issue forward requires someone to write tests topic: script labelsMay 21, 2020
@guybedford
Copy link
Contributor

This looks like a good start. A useful feature can be to permit a second argument toimport.meta.resolve that allows providing a different baseURL for the resolution function - this letsimport.meta.resolve behave both as a contextual resolver and as a more general resolver as well.

I can provide some spec along these lines if you'd be open to it?

@domenic
Copy link
MemberAuthor

I'd like to get the basic version out there first. Adding more features is a sure way to prevent this one from landing.

Base automatically changed frommaster tomainJanuary 15, 2021 07:57
domenic added a commit to WICG/import-maps that referenced this pull requestOct 14, 2021
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. Alsocloses#79 by removing all the potential complexity there; we can instead discuss onwhatwg/html#3871 andwhatwg/html#5572.* Give up on import: URLs.Closes#71,closes#149.* Give up on built-in module remapping.
@annevk
Copy link
Member

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 toimport.meta?

domenic reacted with thumbs up emoji

@domenic
Copy link
MemberAuthor

@jasnell
Copy link

Yeah, I'm +1 on this. It'll be quite useful, I think.

@lucacasonato
Copy link
Member

lucacasonato commentedFeb 10, 2022
edited
Loading

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).

@codehag
Copy link

codehag commentedFeb 11, 2022
edited
Loading

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?

@domenicdomenic removed the needs implementer interestMoving the issue forward requires implementers to express interest labelFeb 11, 2022
@domenic
Copy link
MemberAuthor

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 athttps://chromium-review.googlesource.com/c/chromium/src/+/3456729 but that CL is messy enough that I don't trust them yet.

@GeoffreyBooth
Copy link

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’timport.meta.resolve("./foo.txt") the same asnew URL("./foo.txt", import.meta.url)? What’s the advantage ofimport.meta.resolve?

And the bare case,import.meta.resolve("somepackage/resource.json"), depends on import maps? Is there any anticipation that in the future the resolution ofsomepackage might be something that happens dynamically, similar to Node’s loaders? And if so, perhapsimport.meta.resolve would need to be an async function so that this future dynamic resolution could take advantage of things like network calls?

espoal and merrywhether reacted with thumbs up emoji

@domenic
Copy link
MemberAuthor

domenic commentedFeb 11, 2022
edited
Loading

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.

@GeoffreyBooth
Copy link

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).

@annevk
Copy link
Member

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 ofmozilla/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?

@jasnell
Copy link

Aside: was whatwg/js-hosts dissolved due to the creation of the Winter CG?

I believe that is the case, yes.

Node.js can make this shape work, but isn't thrilled about it.

That's accurate.

@GeoffreyBooth
Copy link

Node.js can make this shape work, but isn’t thrilled about it.

To add to this, please readthis detailed comment about the downsides of Node makingresolve sync. This is the kind of user feedback we will be dealing with if we make this change. These all strike me as very reasonable use cases, though mostly more for command-line programs than browser web apps; and I’m not sure if there will be other ways to solve these problems without an asyncresolve.nodejs/loaders#83 is an attempt to address this, but as a new API it would be a deviation from a cross-platform standard.

@domenic
Copy link
MemberAuthor

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.)

@GeoffreyBooth
Copy link

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,require.resolve('foo') will search the hard disk for a package namedfoo, first in localnode_modules, then globally, etc.This shipped over ten years ago. So I don’t think it’s unreasonable for users to think of these things as appropriate to occur withinresolve. Decidingwhat to resolve to takes disk operations in Node; it’s not just a string operation.ESM resolution behaves largely the same way, though without automatic extension resolution.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJun 17, 2022
Seewhatwg/html#5572Bug: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
@davidje13
Copy link

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.)

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.jsonimport'./node_modules/bar/index.js';

Andimport 'bar'; from a sibling module might be referencing thesamebar, or a differentbar, depending on the file layout (specifically: whichnode_modules folder(s)bar lives in).

It's important to know all thatbefore 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 needresolve to detect that these are all the same thing and return one consistent URI for them (e.g.file:///wherever/node_modules/bar/index.js).

Now you may say that's a problem with NodeJS'simport being too permissive in the specifiers it accepts, but the ability to use named imports is quite important for sharing dependencies while avoiding version conflicts. It's also pretty well established and shared with highly popular tools like WebPack and Rollup, which makes it very popular with destined-for-browser code (pre-bundling).

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 bevast. 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 theresolve call into something more interesting.


My preference (as a userspace developer with a vested interest in keeping parity between browsers and NodeJS) is thatimport.meta.resolve be defined as asynchronous. I understand that making it asynchronous has the downside that it will not be usable in synchronous contexts, but since asynchronous APIs are already quite common (and in particular used byimport()), this doesn't seem likely to cause any problems. If it really trulymust not be asynchronous, then it should be unavailable to userspace code until import maps are more mature and widely used, because making it synchronous now will cause problems if it turns out that module resolution is not always going to be a simple string operation (even in-browser).

@lucacasonato
Copy link
Member

FYI, the compartments proposal at TC39 (https://github.com/tc39/proposal-compartments) also defines specifier resolving as being a synchronous operation. (grep forresolveHook).

@domenicdomenic removed the needs testsMoving the issue forward requires someone to write tests labelJun 30, 2022
@domenicdomenic merged commitb235571 intomainJun 30, 2022
@domenicdomenic deleted the import-meta-resolve branchJune 30, 2022 03:52
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJun 30, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Bug: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
@GeoffreyBooth

This comment was marked as off-topic.

@domenic

This comment was marked as off-topic.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJul 7, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJul 7, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261
aarongable pushed a commit to chromium/chromium that referenced this pull requestJul 7, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261Reviewed-on:https://chromium-review.googlesource.com/c/chromium/src/+/3456729Reviewed-by: Kouhei Ueno <kouhei@chromium.org>Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>Reviewed-by: Yuki Shiino <yukishiino@chromium.org>Commit-Queue: Domenic Denicola <domenic@chromium.org>Cr-Commit-Position: refs/heads/main@{#1021529}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJul 7, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261Reviewed-on:https://chromium-review.googlesource.com/c/chromium/src/+/3456729Reviewed-by: Kouhei Ueno <kouhei@chromium.org>Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>Reviewed-by: Yuki Shiino <yukishiino@chromium.org>Commit-Queue: Domenic Denicola <domenic@chromium.org>Cr-Commit-Position: refs/heads/main@{#1021529}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull requestJul 7, 2022
Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261Reviewed-on:https://chromium-review.googlesource.com/c/chromium/src/+/3456729Reviewed-by: Kouhei Ueno <kouhei@chromium.org>Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>Reviewed-by: Yuki Shiino <yukishiino@chromium.org>Commit-Queue: Domenic Denicola <domenic@chromium.org>Cr-Commit-Position: refs/heads/main@{#1021529}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull requestJul 14, 2022
Automatic update from web-platform-testsImplement import.meta.resolve()Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed:1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261Reviewed-on:https://chromium-review.googlesource.com/c/chromium/src/+/3456729Reviewed-by: Kouhei Ueno <kouhei@chromium.org>Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>Reviewed-by: Yuki Shiino <yukishiino@chromium.org>Commit-Queue: Domenic Denicola <domenic@chromium.org>Cr-Commit-Position: refs/heads/main@{#1021529}--wpt-commits: edcc51d740e71ace04d1fe1a4ae107ea3e640c2awpt-pr: 34188
aosmond pushed a commit to aosmond/gecko that referenced this pull requestJul 15, 2022
Automatic update from web-platform-testsImplement import.meta.resolve()Seewhatwg/html#5572.Intent to Ship:https://groups.google.com/a/chromium.org/g/blink-dev/c/ZVODFsnIf74Fixed: 1296665Change-Id: I63938700518941d0f65a2a1c7fd13910bd095261Reviewed-on:https://chromium-review.googlesource.com/c/chromium/src/+/3456729Reviewed-by: Kouhei Ueno <kouhei@chromium.org>Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>Reviewed-by: Yuki Shiino <yukishiino@chromium.org>Commit-Queue: Domenic Denicola <domenic@chromium.org>Cr-Commit-Position: refs/heads/main@{#1021529}--wpt-commits: edcc51d740e71ace04d1fe1a4ae107ea3e640c2awpt-pr: 34188
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@hiroshige-ghiroshige-ghiroshige-g left review comments

@annevkannevkannevk approved these changes

Assignees
No one assigned
Labels
addition/proposalNew features or enhancementstopic: script
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Proposal: import.meta.resolve (was: import.meta.resolveURL)
10 participants
@domenic@guybedford@annevk@jasnell@lucacasonato@codehag@GeoffreyBooth@DerekNonGeneric@davidje13@hiroshige-g

[8]ページ先頭

©2009-2025 Movatter.jp