Open
Description
Consider this file:
import * as staticImport from "a";
const dynamicImport = await import("a");
console.log(staticImport === dynamicImport);
Two imports in the same file must always resolve to the same module, so that console.log
must always log true
. However, the following loader allow breaking that language invariant and console.log
will log false:
import { register } from "node:module"
register(import.meta.url);
let counter = 0;
export function resolve(specifier, context, nextResolve) {
if (specifier === "a") {
return nextResolve(counter++ ? "./a.js" : "./b.js", context);
}
return nextResolve(specifier, context);
}
For reference, the relevant specification is https://tc39.es/ecma262/#sec-HostLoadImportedModule:
- If this operation is called multiple times with the same (referrer, specifier) pair and it performs FinishLoadingImportedModule(referrer, specifier, payload, result) where result is a normal completion, then it must perform FinishLoadingImportedModule(referrer, specifier, payload, result) with the same result each time.
Where referrer is the importer module, specifier is the string passed to import ("a"
in both cases in this example), and "the result is a normal completion" means that loading the module does not throw.
Metadata
Metadata
Assignees
Labels
No labels