| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -268,8 +268,8 @@ It's possible to call `register` more than once: | |||
| 268 | 268 | // entrypoint.mjs | |
| 269 | 269 | import { register } from 'node:module'; | |
| 270 | 270 | ||
| 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); | ||
| 273 | 273 | await import('./my-app.mjs'); | |
| 274 | 274 | ``` | |
| 275 | 275 | ||
@@ -279,20 +279,23 @@ const { register } = require('node:module'); | |||
| 279 | 279 | const { pathToFileURL } = require('node:url'); | |
| 280 | 280 | ||
| 281 | 281 | const parentURL = pathToFileURL(__filename); | |
| 282 | - register('./first.mjs', parentURL); | ||
| 283 | - register('./second.mjs', parentURL); | ||
| 282 | + register('./foo.mjs', parentURL); | ||
| 283 | + register('./bar.mjs', parentURL); | ||
| 284 | 284 | import('./my-app.mjs'); | |
| 285 | 285 | ``` | |
| 286 | 286 | ||
| 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. | ||
| 290 | 293 | ||
| 291 | 294 | 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. | ||
| 296 | 299 | ||
| 297 | 300 | The `register` method cannot be called from within the module that defines the | |
| 298 | 301 | hooks. | |
@@ -367,11 +370,11 @@ export async function load(url, context, nextLoad) { | |||
| 367 | 370 | } | |
| 368 | 371 | ``` | |
| 369 | 372 | ||
| 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 | ||
| 372 | 375 | functions nest: each one must always return a plain object, and chaining happens | |
| 373 | 376 | 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). | ||
| 375 | 378 | ||
| 376 | 379 | A hook that returns a value lacking a required property triggers an exception. A | |
| 377 | 380 | hook that returns without calling `next<hookName>()` _and_ without returning | |
@@ -1049,6 +1052,7 @@ returned object contains the following keys: | |||
| 1049 | 1052 | [`register`]: #moduleregisterspecifier-parenturl-options | |
| 1050 | 1053 | [`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | |
| 1051 | 1054 | [`util.TextDecoder`]: util.md#class-utiltextdecoder | |
| 1055 | + [chain]: #chaining | ||
| 1052 | 1056 | [hooks]: #customization-hooks | |
| 1053 | 1057 | [load hook]: #loadurl-context-nextload | |
| 1054 | 1058 | [module wrapper]: modules.md#the-module-wrapper | |
| Back | FazBrowse Home | New Git URL |
0 commit comments