Skip to content

Commit 90da415

Browse files
JakobJingleheimerrichardlau
authored andcommitted
doc,module: clarify hook chain execution sequence
PR-URL: #51884 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 3f85c7a commit 90da415

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

doc/api/module.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ It's possible to call `register` more than once:
268268
// entrypoint.mjs
269269
import { register } from 'node:module';
270270

271-
register('./first.mjs', import.meta.url);
272-
register('./second.mjs', import.meta.url);
271+
register('./foo.mjs', import.meta.url);
272+
register('./bar.mjs', import.meta.url);
273273
await import('./my-app.mjs');
274274
```
275275
@@ -279,20 +279,23 @@ const { register } = require('node:module');
279279
const { pathToFileURL } = require('node:url');
280280

281281
const parentURL = pathToFileURL(__filename);
282-
register('./first.mjs', parentURL);
283-
register('./second.mjs', parentURL);
282+
register('./foo.mjs', parentURL);
283+
register('./bar.mjs', parentURL);
284284
import('./my-app.mjs');
285285
```
286286
287-
In this example, the registered hooks will form chains. If both `first.mjs` and
288-
`second.mjs` define a `resolve` hook, both will be called, in the order they
289-
were registered. The same applies to all the other hooks.
287+
In this example, the registered hooks will form chains. These chains run
288+
last-in, first out (LIFO). If both `foo.mjs` and `bar.mjs` define a `resolve`
289+
hook, they will be called like so (note the right-to-left):
290+
node's default ← `./foo.mjs``./bar.mjs`
291+
(starting with `./bar.mjs`, then `./foo.mjs`, then the Node.js default).
292+
The same applies to all the other hooks.
290293
291294
The registered hooks also affect `register` itself. In this example,
292-
`second.mjs` will be resolved and loaded per the hooks registered by
293-
`first.mjs`. This allows for things like writing hooks in non-JavaScript
294-
languages, so long as an earlier registered loader is one that transpiles into
295-
JavaScript.
295+
`bar.mjs` will be resolved and loaded via the hooks registered by `foo.mjs`
296+
(because `foo`'s hooks will have already been added to the chain). This allows
297+
for things like writing hooks in non-JavaScript languages, so long as
298+
earlier registered hooks transpile into JavaScript.
296299
297300
The `register` method cannot be called from within the module that defines the
298301
hooks.
@@ -367,11 +370,11 @@ export async function load(url, context, nextLoad) {
367370
}
368371
```
369372
370-
Hooks are part of a chain, even if that chain consists of only one custom
371-
(user-provided) hook and the default hook, which is always present. Hook
373+
Hooks are part of a [chain][], even if that chain consists of only one
374+
custom (user-provided) hook and the default hook, which is always present. Hook
372375
functions nest: each one must always return a plain object, and chaining happens
373376
as a result of each function calling `next<hookName>()`, which is a reference to
374-
the subsequent loader's hook.
377+
the subsequent loader's hook (in LIFO order).
375378
376379
A hook that returns a value lacking a required property triggers an exception. A
377380
hook that returns without calling `next<hookName>()` _and_ without returning
@@ -1124,6 +1127,7 @@ returned object contains the following keys:
11241127
[`register`]: #moduleregisterspecifier-parenturl-options
11251128
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
11261129
[`util.TextDecoder`]: util.md#class-utiltextdecoder
1130+
[chain]: #chaining
11271131
[hooks]: #customization-hooks
11281132
[load hook]: #loadurl-context-nextload
11291133
[module wrapper]: modules.md#the-module-wrapper

0 commit comments

Comments
 (0)