doc,module: clarify hook chain execution sequence

PR-URL: https://github.com/nodejs/node/pull/51884
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Jacob Smith 2024-03-02 15:33:25 +01:00 committed by GitHub
parent 5c97f411d8
commit 2e2a848c58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -274,8 +274,8 @@ It's possible to call `register` more than once:
// entrypoint.mjs // entrypoint.mjs
import { register } from 'node:module'; import { register } from 'node:module';
register('./first.mjs', import.meta.url); register('./foo.mjs', import.meta.url);
register('./second.mjs', import.meta.url); register('./bar.mjs', import.meta.url);
await import('./my-app.mjs'); await import('./my-app.mjs');
``` ```
@ -285,20 +285,23 @@ const { register } = require('node:module');
const { pathToFileURL } = require('node:url'); const { pathToFileURL } = require('node:url');
const parentURL = pathToFileURL(__filename); const parentURL = pathToFileURL(__filename);
register('./first.mjs', parentURL); register('./foo.mjs', parentURL);
register('./second.mjs', parentURL); register('./bar.mjs', parentURL);
import('./my-app.mjs'); import('./my-app.mjs');
``` ```
In this example, the registered hooks will form chains. If both `first.mjs` and In this example, the registered hooks will form chains. These chains run
`second.mjs` define a `resolve` hook, both will be called, in the order they last-in, first out (LIFO). If both `foo.mjs` and `bar.mjs` define a `resolve`
were registered. The same applies to all the other hooks. hook, they will be called like so (note the right-to-left):
node's default ← `./foo.mjs``./bar.mjs`
(starting with `./bar.mjs`, then `./foo.mjs`, then the Node.js default).
The same applies to all the other hooks.
The registered hooks also affect `register` itself. In this example, The registered hooks also affect `register` itself. In this example,
`second.mjs` will be resolved and loaded per the hooks registered by `bar.mjs` will be resolved and loaded via the hooks registered by `foo.mjs`
`first.mjs`. This allows for things like writing hooks in non-JavaScript (because `foo`'s hooks will have already been added to the chain). This allows
languages, so long as an earlier registered loader is one that transpiles into for things like writing hooks in non-JavaScript languages, so long as
JavaScript. earlier registered hooks transpile into JavaScript.
The `register` method cannot be called from within the module that defines the The `register` method cannot be called from within the module that defines the
hooks. hooks.
@ -373,11 +376,11 @@ export async function load(url, context, nextLoad) {
} }
``` ```
Hooks are part of a chain, even if that chain consists of only one custom Hooks are part of a [chain][], even if that chain consists of only one
(user-provided) hook and the default hook, which is always present. Hook custom (user-provided) hook and the default hook, which is always present. Hook
functions nest: each one must always return a plain object, and chaining happens functions nest: each one must always return a plain object, and chaining happens
as a result of each function calling `next<hookName>()`, which is a reference to as a result of each function calling `next<hookName>()`, which is a reference to
the subsequent loader's hook. the subsequent loader's hook (in LIFO order).
A hook that returns a value lacking a required property triggers an exception. A A hook that returns a value lacking a required property triggers an exception. A
hook that returns without calling `next<hookName>()` _and_ without returning hook that returns without calling `next<hookName>()` _and_ without returning
@ -1060,6 +1063,7 @@ returned object contains the following keys:
[`register`]: #moduleregisterspecifier-parenturl-options [`register`]: #moduleregisterspecifier-parenturl-options
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String [`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
[`util.TextDecoder`]: util.md#class-utiltextdecoder [`util.TextDecoder`]: util.md#class-utiltextdecoder
[chain]: #chaining
[hooks]: #customization-hooks [hooks]: #customization-hooks
[load hook]: #loadurl-context-nextload [load hook]: #loadurl-context-nextload
[module wrapper]: modules.md#the-module-wrapper [module wrapper]: modules.md#the-module-wrapper