module.link(linker)
linker<Function>-
specifier<string> 所请求模块的说明符:import foo from 'foo'; // ^^^^^ the module specifier -
referencingModule<vm.Module> 调用link()的Module对象。 -
extra<Object> -
返回: <vm.Module> | <Promise>
-
- 返回: <Promise>
链接模块依赖。这种方法必须在评估之前调用,并且每个模块只能调用一次。
【Link module dependencies. This method must be called before evaluation, and can only be called once per module.】
该函数预计返回一个 Module 对象或一个最终解析为 Module 对象的 Promise。返回的 Module 必须满足以下两个不变量:
【The function is expected to return a Module object or a Promise that
eventually resolves to a Module object. The returned Module must satisfy the
following two invariants:】
- 它必须属于与父
Module相同的上下文。 - 它的
status不能为'errored'。
如果返回的 Module 的 status 是 'unlinked',此方法将会对返回的 Module 递归调用,并使用相同提供的 linker 函数。
【If the returned Module's status is 'unlinked', this method will be
recursively called on the returned Module with the same provided linker
function.】
link() 返回一个 Promise,当所有链接实例解析为有效的 Module 时,该 Promise 会被解决;如果链接器函数抛出异常或返回无效的 Module,则 Promise 会被拒绝。
链接器函数大致对应于 ECMAScript 规范中实现定义的 主机解析导入模块 抽象操作,但有一些关键差异:
【The linker function roughly corresponds to the implementation-defined HostResolveImportedModule abstract operation in the ECMAScript specification, with a few key differences:】
- 链接器函数可以是异步的,而 主机解析导入模块 是同步的。
在模块链接期间使用的实际 主机解析导入模块 实现会返回在链接过程中被链接的模块。由于在那时所有模块都已经完全链接,因此 主机解析导入模块 实现根据规范是完全同步的。
【The actual HostResolveImportedModule implementation used during module linking is one that returns the modules linked during linking. Since at that point all modules would have been fully linked already, the HostResolveImportedModule implementation is fully synchronous per specification.】
对应 ECMAScript 规范中 循环模块记录 的 Link() 具体方法 字段。
【Corresponds to the Link() concrete method field of Cyclic Module Records in the ECMAScript specification.】