-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
Link modules with paths in module.link(linker) #35848
Comments
You have to set up your own system to load the source text modules. |
I think it would be easier to pass back the standard import() function in the linker function, like: module.link( spec => import( spec ) ) |
that's not really how that's meant to work though. If you want to import a module that node has imported, synthetic modules would be the correct choice. If you're just trying to match how node's resolution works, you will have to do that yourself and plug it into source text modules. |
Here is an example, and it works fine now:
I think: In the future, it would be better if the module.link() could eat :-) the Module Class what ES6 import() returns
|
@lacmuch It seems like maybe you just want this: function importSource(source) {
return import(`data:text/javascript,${source}`);
} |
I want to use module.createCachedData(); |
do you just want node to cache the startup of your app? |
I want to use it for code protection... but there is an other issue: ...and i dont want to compile the node_modules folder :-) |
@lacmuch Just wanted to say thanks for documenting this; it helped me today. PS. In case it helps anyone else, I’m iterating over the module keys and copying them over in my tests to ensure they work with any module: await module.link(async (specifier, referencingModule) => {
return new Promise(async (resolve, reject) => {
const module = await import(specifier)
const exportNames = Object.keys(module)
const syntheticModule = new vm.SyntheticModule(
exportNames,
function () {
exportNames.forEach(key => {
this.setExport(key, module[key])
})
}, { context })
resolve(syntheticModule)
})
}) |
Hmm, the thing is, when using "await import" this way means it will do the import within the context of the link implementator, not within the context of possibly another package using the implementator (meaning it should look into that node_modules folder, of said package instead that of the linker) I'm using this for a code generator, that generates some JS code on the fly matching definitions in a file, and the code generator is in another package than the code taking use of this. (https://gitlab.com/timberdoodle/tim I generate code for immutables) In CommonJS world, I solved this by passing the callees module object as paramater to the generator, so it call that "module.require" from the callees package context to get additional packages from there... but how do I this when doing modules? Yes completely reimplementing node.js module loading is an option, but thats IMO asking for incompatiblity problems and nasty supprises down the road. What would be needed would the node.js linking function as parameter, as object to extend or so, the linker if it would natively link that file in that directory if node would do it by itself (where my generator would e.g. decide to link something itself (say the specifier starts with "tim:") or forward it do the default linker otherwise. |
I guess this #31234 is asking for the same thing. |
Found it, this way you can load modules from another path.
Wasn't obvious in any way tough. |
https://nodejs.org/api/vm.html#vm_module_link_linker
Now we can only link "standard" modules with functions combined with SyntheticModules, like this:
The text was updated successfully, but these errors were encountered: