-
Notifications
You must be signed in to change notification settings - Fork 11
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
Support import assertions #37
Comments
Let’s add anoptions bag to Let’s expressly not thread |
cc @littledan |
I would note in the design rationales that we would not be able to enforce import assertions in a virtualized module loader if we did not encapsulate the linker. |
Following the #71 design, I think import assertions fit nicely as new parameters of
The import assertions proposal has two goals:
while for (2) we can "trust" hosts to not make assertions affect module loading/execution (otherwise they are not ECMAScript-compliant), I think for loaders we must enforce it explicitly. The
class Module {
#importHook; #importMeta; #supportedAssertions; #applyAssertions;
#imports_cache = new Map();
#load_deps_link_and_evaluate() { /* magic */ }
async #import(specifier, { assert: inputAssertions = {} } = {}) {
const resolvedAssertions = Object.create(null);
for (const [key, value] of Object.entries(inputAssertions)) {
if (supportedAssertions.includes(key)) resolvedAssertions[key] = value;
}
let module = this.#imports_cache.get(specifier);
if (!module) {
module = await importHook(specifier, importMeta);
this.#imports_cache.set(specifier, module);
}
applyAssertions(module, resolvedAssertions);
return module.#load_deps_link_and_evaluate();
}
} For example, if you wanted to implement a virtual host that only supports JavaScript and JSON modules, and that forces you to use async function loadHook(specifier, importMeta) {
const source = /* magic loading */;
const newImportMeta = /* magic logic */;
return new Module(source, loadHook, newImportMeta, supportedAssertions, applyAssertions);
}
const supportedAssertions = ["type"];
function applyAssertions(module, assertions) {
if (assertions.type === "json") {
if (module.source instanceof JsonModuleSource) return;
throw new TypeError("It's not a JSON module");
}
if (!assertions.type || assertions.type === "javascript") {
if (module.source instanceof JavaScriptModuleSource) return;
throw new TypeError("It's not a JavaScript module");
}
throw new TypeError("Unsupported type assertion: " + assertions.type);
}
// bootstrap
const entrypoint = new Module(entrypointSource, loadHook, entrypointImportMeta, supportedAssertions, applyAssertions);
await import(entrypoint); |
tc39/proposal-import-attributes#103
The text was updated successfully, but these errors were encountered: