w3ctag / design-reviews

W3C specs and API reviews
Creative Commons Zero v1.0 Universal
326 stars 55 forks source link

import.meta.resolve() #746

Closed domenic closed 1 year ago

domenic commented 2 years ago

Wotcher TAG!

I'm requesting a TAG review of import.meta.resolve().

import.meta.resolve(specifier) returns the URL to which specifier would resolve in the context of the current script. That is, it returns the URL that would be imported if you did import(specifier).

This is a quite small feature (a single function, with only a few lines of spec text which exposes an already-existing spec algorithm).

Further details:

We'd prefer the TAG provide feedback as (please delete all but the desired option):

💬 leave review feedback as a comment in this issue and @-notify @domenic

plinss commented 2 years ago

We took a look at this today and are generally favorable. Some questions though regarding the sync/async decision, what happens if the author calls this while there's a pending load for an import map? Does it throw or return the wrong value? If the author needs to wait until the import map(s) are fully loaded, how do they do that?

domenic commented 2 years ago

Right now import maps are always inline and cannot be loaded. It's unclear whether that will change in the future; implementer interest is low, although I personally think it'd be nice.

In such a future, it would return the current value. It would not throw. It's not clear whether the current value would be considered "wrong"; if you do import.meta.resolve(x); import(x); while an import map is being loaded, the import() statement will import the same URL returned by import.meta.resolve(x) (i.e., the current value).

If an author needs to wait for import maps in such a future, it depends on how we expose external import map loading, but probably the simplest way would be to wait for that import map <script> element's load event.

LeaVerou commented 1 year ago

We looked at this again today during a breakout. Even without import map loading, authors can always dynamically add import maps by creating new <script> elements and appending them to the document. So even if the API were async, it could not possibly account for all future import maps. Therefore we're fine with a sync API, that is easier to reason about as resolving the specifier based on whatever the current state of import maps is, and we're going to close this. Thank you for flying TAG!