module.registerHooks() 接受的钩子函数


🌐 Hook functions accepted by module.registerHooks()

module.registerHooks() 方法接受以下同步钩子函数。

🌐 The module.registerHooks() method accepts the following synchronous hook functions.

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.
} 

同步钩子在与模块加载相同的线程和相同的 字段 中运行,钩子函数中的代码可以通过全局变量或其他共享状态将值直接传递给被引用的模块。

🌐 Synchronous hooks are run in the same thread and the same realm where the modules are loaded, the code in the hook function can pass values to the modules being referenced directly via global variables or other shared states.

与异步钩子不同,同步钩子默认不会被继承到子工作线程中,尽管如果使用 --import--require 预加载的文件注册钩子,子工作线程可以通过 process.execArgv 继承预加载的脚本。详情请参见 Worker 的文档

🌐 Unlike the asynchronous hooks, the synchronous hooks 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 details.