module.registerHooks() 接受的同步钩子
¥Synchronous hooks accepted by module.registerHooks()
¥Stability: 1.1 - Active development
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.
}
同步钩子在加载模块的同一线程和同一 realm 中运行。与异步钩子不同,它们默认不会被继承到子工作线程中,但如果使用 --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.