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:
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.
Consider this file:
Two imports in the same file must always resolve to the same module, so that
console.logmust always logtrue. However, the following loader allow breaking that language invariant andconsole.logwill log false:For reference, the relevant specification is https://tc39.es/ecma262/#sec-HostLoadImportedModule:
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.