vm.SourceTextModule
実はnode v9.6.0からvm.SourceTextModuleというのが足されていて、cjs問わずコードの中でesmの文字列からimportできる サンドボックス使いたい場合も親のmodule.Module使っちゃうのでスッ…と通り抜けちゃう
これよく考えたらvm.Scriptで良かったのか
とりあえず使う
code:index.ts
import vm from 'vm'
const bar = new vm.SourceTextModule(export default "hello", { context: vm.createContext() })
await bar.link((spec, ref) => {
console.log(spec, ref)
throw new Error("unresolved")
})
await bar.evaluate()
console.log(bar.namespace) // "hello"
vmのcontextにはrequire/Buffer他が欠如しているのでリスク承知で雑に足さないと動かないものがある
型情報はあとから出します
結局electronはNODE_OPTIONS禁止なので、改造electron作るかflag外れたあとelectronあげるしか使う方法がない