vm.SourceTextModule 与缓存数据

问题描述 投票:0回答:1

当带有

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()
不能再使用了?

node.js v8
1个回答
0
投票

在进程重新启动之前,

identifier
不能重复。

通过在末尾附加哈希来解决。

© www.soinside.com 2019 - 2024. All rights reserved.