@@ -510,11 +510,19 @@ CommonJS modules loaded.
510510
511511### Hooks
512512
513- #### <code> resolve</code> hook
513+ #### ` resolve (specifier, context, defaultResolve) `
514514
515515> Note: The loaders API is being redesigned. This hook may disappear or its
516516> signature may change. Do not rely on the API described below.
517517
518+ * ` specifier` {string}
519+ * ` context` {Object}
520+ * ` conditions` {string[]}
521+ * ` parentURL` {string}
522+ * ` defaultResolve` {Function}
523+ * Returns: {Object}
524+ * ` url` {string}
525+
518526The ` resolve` hook returns the resolved file URL for a given module specifier
519527and parent URL. The module specifier is the string in an ` import ` statement or
520528` import()` expression, and the parent URL is the URL of the module that imported
@@ -535,11 +543,11 @@ Node.js module specifier resolution behavior_ when calling `defaultResolve`, the
535543/**
536544 * @param {string} specifier
537545 * @param {{
546+ * conditions: !Array<string>,
538547 * parentURL: !(string | undefined),
539- * conditions: !(Array<string>),
540548 * }} context
541549 * @param {Function} defaultResolve
542- * @returns {!( Promise<{ url: string }>) }
550+ * @returns {Promise<{ url: string }>}
543551 */
544552export async function resolve (specifier , context , defaultResolve ) {
545553 const { parentURL = null } = context;
@@ -565,30 +573,35 @@ export async function resolve(specifier, context, defaultResolve) {
565573}
566574` ` `
567575
568- #### <code> getFormat</code> hook
576+ #### ` getFormat (url, context, defaultGetFormat) `
569577
570578> Note: The loaders API is being redesigned. This hook may disappear or its
571579> signature may change. Do not rely on the API described below.
572580
581+ * ` url` {string}
582+ * ` context` {Object}
583+ * ` defaultGetFormat` {Function}
584+ * Returns: {Object}
585+ * ` format` {string}
586+
573587The ` getFormat` hook provides a way to define a custom method of determining how
574588a URL should be interpreted. The ` format` returned also affects what the
575589acceptable forms of source values are for a module when parsing. This can be one
576590of the following:
577591
578- | ` format` | Description | Acceptable Types For ` source` Returned by ` getSource` or ` transformSource` |
579- | --- | --- | --- |
580- | ` ' builtin' ` | Load a Node.js builtin module | Not applicable |
581- | ` ' commonjs ' ` | Load a Node.js CommonJS module | Not applicable |
582- | ` ' dynamic ' ` | Use a [dynamic instantiate hook][] | Not applicable |
583- | ` ' json' ` | Load a JSON file | { [ArrayBuffer ][], [string ][], [TypedArray][] } |
584- | ` ' module' ` | Load an ES module | { [ArrayBuffer ][], [string ][], [TypedArray][] } |
585- | ` ' wasm' ` | Load a WebAssembly module | { [ArrayBuffer][], [string][], [ TypedArray][] } |
592+ | ` format` | Description | Acceptable Types For ` source` Returned by ` getSource` or ` transformSource` |
593+ | ------------ | ------------------------------ | ----------------------------------------------------------------------- --- |
594+ | ` ' builtin' ` | Load a Node.js builtin module | Not applicable |
595+ | ` ' dynamic ' ` | Use a [dynamic instantiate hook][] | Not applicable |
596+ | ` ' commonjs ' ` | Load a Node.js CommonJS module | Not applicable |
597+ | ` ' json' ` | Load a JSON file | { [` string ` ][], [` ArrayBuffer ` ][], [` TypedArray` ][] } |
598+ | ` ' module' ` | Load an ES module | { [` string ` ][], [` ArrayBuffer ` ][], [` TypedArray` ][] } |
599+ | ` ' wasm' ` | Load a WebAssembly module | { [` ArrayBuffer ` ][], [` TypedArray` ][] } |
586600
587601Note: These types all correspond to classes defined in ECMAScript.
588602
589- * The specific [ArrayBuffer][] object is a [SharedArrayBuffer][].
590- * The specific [string][] object is not the class constructor, but an instance.
591- * The specific [TypedArray][] object is a [Uint8Array][].
603+ * The specific [` ArrayBuffer ` ][] object is a [` SharedArrayBuffer ` ][].
604+ * The specific [` TypedArray` ][] object is a [` Uint8Array ` ][].
592605
593606Note: If the source value of a text-based format (i.e., ` ' json' ` , ` ' module' ` ) is
594607not a string, it will be converted to a string using [` util .TextDecoder ` ][].
@@ -614,11 +627,18 @@ export async function getFormat(url, context, defaultGetFormat) {
614627}
615628` ` `
616629
617- #### <code> getSource</code> hook
630+ #### ` getSource (url, context, defaultGetSource) `
618631
619632> Note: The loaders API is being redesigned. This hook may disappear or its
620633> signature may change. Do not rely on the API described below.
621634
635+ * ` url` {string}
636+ * ` context` {Object}
637+ * ` format` {string}
638+ * ` defaultGetSource` {Function}
639+ * Returns: {Object}
640+ * ` source` {string|SharedArrayBuffer|Uint8Array}
641+
622642The ` getSource` hook provides a way to define a custom method for retrieving
623643the source code of an ES module specifier. This would allow a loader to
624644potentially avoid reading files from disk.
@@ -628,7 +648,7 @@ potentially avoid reading files from disk.
628648 * @param {string} url
629649 * @param {{ format: string }} context
630650 * @param {Function} defaultGetSource
631- * @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
651+ * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
632652 */
633653export async function getSource (url , context , defaultGetSource ) {
634654 const { format } = context;
@@ -644,7 +664,7 @@ export async function getSource(url, context, defaultGetSource) {
644664}
645665` ` `
646666
647- #### <code> transformSource</code> hook
667+ #### ` transformSource (source, context, defaultTransformSource) `
648668
649669` ` ` console
650670NODE_OPTIONS = ' --experimental-loader ./custom-loader.mjs' node x .js
@@ -653,6 +673,13 @@ NODE_OPTIONS='--experimental-loader ./custom-loader.mjs' node x.js
653673> Note: The loaders API is being redesigned. This hook may disappear or its
654674> signature may change. Do not rely on the API described below.
655675
676+ * ` source` {string|SharedArrayBuffer|Uint8Array}
677+ * ` context` {Object}
678+ * ` format` {string}
679+ * ` url` {string}
680+ * Returns: {Object}
681+ * ` source` {string|SharedArrayBuffer|Uint8Array}
682+
656683The ` transformSource` hook provides a way to modify the source code of a loaded
657684ES module file after the source string has been loaded but before Node.js has
658685done anything with it.
@@ -663,13 +690,13 @@ unknown-to-Node.js file extensions. See the [transpiler loader example][] below.
663690
664691` ` ` js
665692/**
666- * @param {!(SharedArrayBuffer | string | Uint8Array)} source
693+ * @param {!(string | SharedArrayBuffer | Uint8Array)} source
667694 * @param {{
668- * url: string,
669695 * format: string,
696+ * url: string,
670697 * }} context
671698 * @param {Function} defaultTransformSource
672- * @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
699+ * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
673700 */
674701export async function transformSource (source , context , defaultTransformSource ) {
675702 const { url , format } = context;
@@ -685,11 +712,13 @@ export async function transformSource(source, context, defaultTransformSource) {
685712}
686713` ` `
687714
688- #### <code> getGlobalPreloadCode</code> hook
715+ #### ` getGlobalPreloadCode () `
689716
690717> Note: The loaders API is being redesigned. This hook may disappear or its
691718> signature may change. Do not rely on the API described below.
692719
720+ * Returns: {string}
721+
693722Sometimes it can be necessary to run some code inside of the same global scope
694723that the application will run in. This hook allows to return a string that will
695724be ran as sloppy-mode script on startup.
@@ -726,7 +755,7 @@ const require = createRequire(process.cwd() + '/<preload>');
726755To create a custom dynamic module that doesn't correspond to one of the
727756existing ` format` interpretations, the ` dynamicInstantiate` hook can be used.
728757This hook is called only for modules that return ` format: ' dynamic' ` from
729- the [ ` getFormat` hook][] .
758+ the ` getFormat` hook.
730759
731760` ` ` js
732761/**
@@ -1254,18 +1283,17 @@ success!
12541283[WHATWG JSON modules specification]: https: // html.spec.whatwg.org/#creating-a-json-module-script
12551284[` data:` URLs]: https: // developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
12561285[` export` ]: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
1257- [` getFormat` hook]: #esm_code_getformat_code_hook
12581286[` import()` ]: #esm_import_expressions
12591287[` import.meta.url` ]: #esm_import_meta
12601288[` import` ]: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
12611289[` module.createRequire()` ]: modules .html #modules_module_createrequire_filename
12621290[` module.syncBuiltinESMExports()` ]: modules .html #modules_module_syncbuiltinesmexports
1263- [` transformSource` hook]: #esm_code_transformsource_code_hook
1264- [ArrayBuffer ]: https: // www.ecma-international .org/ecma-262/6.0/#sec-arraybuffer-constructor
1265- [SharedArrayBuffer ]: https: // tc39.es/ecma262/#sec-sharedarraybuffer-constructor
1266- [string]: https: // www.ecma-international .org/ecma-262/6.0/#sec-string-constructor
1267- [TypedArray]: https: // www.ecma-international .org/ecma-262/6.0/#sec-typedarray-objects
1268- [Uint8Array ]: https: // www.ecma-international .org/ecma-262/6.0/#sec-uint8array
1291+ [` transformSource` hook]: #esm_transformsource_source_context_defaulttransformsource
1292+ [` ArrayBuffer` ]: https: // developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
1293+ [` SharedArrayBuffer` ]: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
1294+ [` string` ]: https: // developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
1295+ [` TypedArray` ]: https: // developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
1296+ [` Uint8Array` ]: https: // developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
12691297[` util.TextDecoder` ]: util .html #util_class_util_textdecoder
12701298[cjs- module - lexer]: https: // github.com/guybedford/cjs-module-lexer/tree/0.4.0
12711299[dynamic instantiate hook]: #esm_code_dynamicinstantiate_code_hook
0 commit comments