Open aiselp opened 1 month ago
Some of the arguments may not work by design. Why do you create a new require
?
Even removing the runtime.globalObject.set("require", require)
doesn't work
In new vm.SourceTextModule(code[, options])
,
options.initializeImportMeta
supports passing in a function to set Import.meta
. I need to set Import.meta
only through this method
ESM is handled by Javet module resolver. I don't think the whole thing would work.
I think this is feasible
const { Script, constants } = require('node:vm');
const script = new Script(
'import("./module.mjs")',
{ importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER });
script.runInNewContext()
In this code, import()
is completely handled by nodejs. If module.mjs contains the import
keyword, its parsing is also handled by nodejs.
This shows that the nodejs esm parser is still valid
import()
is completely implemented in JS. It has nothing to do with ESM. It's called dynamic import.
I plan to write a loader using the node:vm module to fully control module parsing and loading, but I found that the value of SourceTextModule is
undefined
, and I tried using the--experimental-vm-modules
argument but it didn't work.I don't know if the way to pass in the argument is correct.