-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Import map read/write API #12217
Description
What problem are you trying to solve and what solutions exist today?
Currently, reading and writing import maps on the page requires DOM methods, which is an unfortunate coupling, since fundamentally import maps have nothing to do with HTML.
Additionally, DOM methods only allow authors to read individual import maps. Although there is an algorithm to merge import maps and produce the import map of the page itself, authors have no way to access the result, short of re-implementing it.
There is import.meta.resolve() but that requires a module scope. Additionally, it does not actually get you an import map but a finished resolution.
How would you solve it?
Some sort of API to get:
- Get existing import maps
- Get computed global import map
- Add new import map
Possibly:
- An
ImportMapinterface to represent import maps after normalization - An
ImportMaps extends ObservableArray<ImportMap>interface (rel: Make ObservableArray "subclassable" webidl#1342) that adds acomputedproperty that returns the computed import map of the array as a frozen, memoizedImportMapobject - A global
importMapsproperty that is anImportMapsinstance. Later, other resources could have similar arrays hanging on different objects, e.g.import.meta.importMaps,document.importMapsetc.
These are both very thin wrappers over the current DOM-based API and easily polyfillable, but can be adopted by non-DOM environments such as JS runtimes and workers. Being synchronous prevents some of the issues with import ... with { type: "importmap" }
TBD:
- Does adding an import map via this API add a new DOM element? I vote no, since the whole point is decoupling from the DOM. DOM would become one input mechanism, but not the only one.
- Assuming the answer to the above is no, how do DOM import maps combine with import maps specified via this API? Presumably once an import map is parsed and merged into the global import map, it gets added to the array regardless of where it comes from.