Description
Hi all,
We're getting lots of interest from multiple corners in providing a way to add async loading to the module support specced for browsers. I was wondering if it'd be OK if I started work on a TC39-side import() spec? It would absorb some of the responsibilities of this repo, so I wanted to check here first.
The basic ideas I have so far:
import("./specifier.js")
returns a promise for a module namespace object- import() works in both scripts and modules
- At first I thought only modules was appropriate, but it turns out that we can make this work in scripts without much extra work, and this is key for enabling lots of important interop scenarios.
- import() first calls HostFetchModule(currentScriptOrModule, specifier)
- this returns a promise for undefined, or is maybe just "spec asynchronous" in some way
- In HTML, this would be defined to first call resolve a module specifier using specifier + currentScriptOrModule, then call fetch a module script tree with the resulting URL and currentScriptOrModule's settings object.
- import() then calls HostResolveImportedModule(currentScriptOrModule, specifier)
- We need to upgrade HostResolveImportedModule to also work for scripts as the first argument, not just modules
- HTML needs to start storing a script URL inside the HTML classic script, just like it does for the HTML module script. Then we can retrieve the HTML classic script using argument.[[HostDefined]] and do the same things as today.
- import() then returns the resulting module record's [[Namespace]] object.
I think this would be a pretty small spec. It would, of course, have room for expansion in the future to support all the loader hooks, e.g. Loader could define a HostFetchModule for host environments that implement the Loader spec that goes through all the hooks, similar to what it does with HostResolveImportedModule today. But in the meantime it would give browsers and Node something they could implement.
Let me know what you think! If you're OK with me taking on this effort, I'd plan to have some spec text ready in time for the next TC39.
/cc @bmeck