You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was recently updating this service code to use import instead of require since web browsers support that better; and development is driving me to update. Part of the service system shares its API across a websocket, and to enable using import in code, I had to update from using eval() or Function() or even AsyncFunction(), and use vm.SourceTextModule, which gives me the ability during await module.link(() => {}); (the callback to link is modules it further needs, allowing me to request them appropriately), but there are some things which are available from the local environment instead, which has actually already been loaded or could simply be loaded with 'import()'
somethiing like...
module.link((spec)=>{returnimport(spec)});
I can pass modules that have been imported already as part of the context object specified to SourceTextModule(code,{context:{path:path,url:url,...})... but modules that haven't already been loaded I would have to instead make a Synthetic module...
function wrapObject(obj){
if( obj instanceof vm.Module ) return obj;
if( obj instanceof Promise ) return obj.then( wrapObject );
const keys = Object.keys(obj);
// 'sm' is 'SyntheticModule'
const sm = new vm.SyntheticModule(keys, ()=>{
for( let key of keys )
sm.setExport(key,obj[key]);
}, {context:defaultContext} );
return sm.link( ()=> {} ).then( ()=>sm.evaluate().then( (result)=>sm ) );
}
linkCallback(spec,ref) { return wrapObject( import(spec ) ) }
This was the previously proposed method I found on nodejs/node#27387 'Modules: Specify or infer module type for new vm.Script'; although this method fails for modules that have a export called import or default
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
vm
I was recently updating this service code to use
import
instead ofrequire
since web browsers support that better; and development is driving me to update. Part of the service system shares its API across a websocket, and to enable usingimport
in code, I had to update from usingeval()
orFunction()
or evenAsyncFunction()
, and usevm.SourceTextModule
, which gives me the ability duringawait module.link(() => {});
(the callback to link is modules it further needs, allowing me to request them appropriately), but there are some things which are available from the local environment instead, which has actually already been loaded or could simply be loaded with 'import()'somethiing like...
I can pass modules that have been imported already as part of the context object specified to
SourceTextModule(code,{context:{path:path,url:url,...})
... but modules that haven't already been loaded I would have to instead make a Synthetic module...This was the previously proposed method I found on nodejs/node#27387 'Modules: Specify or infer module type for
new vm.Script
'; although this method fails for modules that have a export calledimport
ordefault
I'm just wondering, since this is 'experimental' why passing existing modules ( ie from just 'import') is so very difficult...
The text was updated successfully, but these errors were encountered: