当带有
cachedData
选项的 new SourceTextModule() 时出现类型错误。
它说
TypeError: A dynamic import callback was not specified.
但是
importModuleDynamically
选项显然是存在的。
function importModuleDynamically(spec: string, ref: any, attributes: any) {
return linker(spec, ref, {attributes})
}
async function SrcModule(url: string, code: string) {
let cachedData = v8Cache.get(url)
let mod = new vm.SourceTextModule(code, {
cachedData,
identifier: url,
initializeImportMeta,
importModuleDynamically
})
if (!cachedData) {
cachedData = mod.createCachedData()
cachedData.code = code
v8Cache.set(url, cachedData)
}
await mod.link(linker)
await mod.evaluate()
return mod
}
一旦设置了
cachedData
,就代表import()
不能再使用了?
在进程重新启动之前,
identifier
不能重复。
通过在末尾附加哈希来解决。