Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 20e0ba3

Browse filesBrowse files
JakobJingleheimertargos
authored andcommitted
doc,module: clarify hook chain execution sequence
PR-URL: #51884 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 9d2c039 commit 20e0ba3
Copy full SHA for 20e0ba3

File tree

Expand file treeCollapse file tree

1 file changed

+18
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-14
lines changed
Open diff view settings
Collapse file

‎doc/api/module.md‎

Copy file name to clipboardExpand all lines: doc/api/module.md
+18-14Lines changed: 18 additions & 14 deletions
  • Display the source diff
  • Display the rich diff
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
@@ -1049,6 +1052,7 @@ returned object contains the following keys:
10491052
[`register`]: #moduleregisterspecifier-parenturl-options
10501053
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
10511054
[`util.TextDecoder`]: util.md#class-utiltextdecoder
1055+
[chain]: #chaining
10521056
[hooks]: #customization-hooks
10531057
[load hook]: #loadurl-context-nextload
10541058
[module wrapper]: modules.md#the-module-wrapper

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.