module.registerHooks() 接受同步钩子


【Synchronous hooks accepted by module.registerHooks()

稳定性: 1.1 - 处于活跃开发中

module.registerHooks() 方法接受同步钩子函数。initialize() 不被支持也不必要,因为钩子实现者可以在调用 module.registerHooks() 之前直接运行初始化代码。

【The module.registerHooks() method accepts synchronous hook functions. initialize() is not supported nor necessary, as the hook implementer can simply run the initialization code directly before the call to module.registerHooks().】

function resolve(specifier, context, nextResolve) {
  // Take an `import` or `require` specifier and resolve it to a URL.
}

function load(url, context, nextLoad) {
  // Take a resolved URL and return the source code to be evaluated.
} 

同步钩子在相同的线程和加载模块的相同 字段 中运行。与异步钩子不同,它们默认不会继承到子工作线程中,不过如果钩子是通过 --import--require 预加载的文件注册的,子工作线程可以通过 process.execArgv 继承预加载的脚本。详情见 Worker 的文档

【Synchronous hooks are run in the same thread and the same realm where the modules are loaded. Unlike the asynchronous hooks they are not inherited into child worker threads by default, though if the hooks are registered using a file preloaded by --import or --require, child worker threads can inherit the preloaded scripts via process.execArgv inheritance. See the documentation of Worker for detail.】

在同步钩子中,用户可以预期 console.log() 的执行方式与他们在模块代码中预期 console.log() 的执行方式相同。

【In synchronous hooks, users can expect console.log() to complete in the same way that they expect console.log() in module code to complete.】