module.link(linker)
linker
<Function>-
specifier
<string> 请求模块的说明符:import foo from 'foo'; // ^^^^^ 模块说明符
-
referencingModule
<vm.Module>Module
对象link()
被调用。 -
extra
<Object>assert
<Object> 来自断言的数据:
根据 ECMA-262,主机应该忽略他们不支持的断言,而不是,例如,如果存在不受支持的断言则触发错误。import foo from 'foo' assert { name: 'value' }; // ^^^^^^^^^^^^^^^^^ 断言
-
返回: <vm.Module> | <Promise>
-
- 返回: <Promise>
链接模块依赖项。 此方法必须在求值前调用,并且每个模块只能调用一次。
该函数应返回 Module
对象或最终解析为 Module
对象的 Promise
。
返回的 Module
必须满足以下两个不变量:
- 它必须与父
Module
属于相同的上下文。 - 它的
status
不能是'errored'
。
如果返回的 Module
的 status
是 'unlinked'
,则将在返回的 Module
上递归调用此方法,并使用相同提供的 linker
函数。
link()
返回 Promise
,当所有链接实例都解析为有效的 Module
时,它将被解析,或者如果链接器函数抛出异常或返回无效的 Module
,则被拒绝。
链接器函数大致对应于 ECMAScript 规范中实现定义的 HostResolveImportedModule 抽象操作,有几个关键区别:
- 当 HostResolveImportedModule 是同步的时,允许链接器函数是异步的。
在模块链接期间使用的实际 HostResolveImportedModule 实现是一种返回链接期间链接的模块的实现。 因为那时所有模块都已经完全链接了,HostResolveImportedModule 实现是完全同步的每个规范。
对应 ECMAScript 规范中循环模块记录的 Link() 具体方法字段。
linker
<Function>-
specifier
<string> The specifier of the requested module:import foo from 'foo'; // ^^^^^ the module specifier
-
referencingModule
<vm.Module> TheModule
objectlink()
is called on. -
extra
<Object>assert
<Object> The data from the assertion:
Per ECMA-262, hosts are expected to ignore assertions that they do not support, as opposed to, for example, triggering an error if an unsupported assertion is present.import foo from 'foo' assert { name: 'value' }; // ^^^^^^^^^^^^^^^^^ the assertion
-
Returns: <vm.Module> | <Promise>
-
- Returns: <Promise>
Link module dependencies. This method must be called before evaluation, and can only be called once per 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:
- It must belong to the same context as the parent
Module
. - Its
status
must not be'errored'
.
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()
returns a Promise
that will either get resolved when all linking
instances resolve to a valid Module
, or rejected if the linker function either
throws an exception or returns an invalid Module
.
The linker function roughly corresponds to the implementation-defined HostResolveImportedModule abstract operation in the ECMAScript specification, with a few key differences:
- The linker function is allowed to be asynchronous while HostResolveImportedModule is synchronous.
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.
Corresponds to the Link() concrete method field of Cyclic Module Records in the ECMAScript specification.