@@ -510,11 +510,19 @@ CommonJS modules loaded.
510
510
511
511
### Hooks
512
512
513
- #### <code> resolve</code> hook
513
+ #### ` resolve (specifier, context, defaultResolve) `
514
514
515
515
> Note: The loaders API is being redesigned. This hook may disappear or its
516
516
> signature may change. Do not rely on the API described below.
517
517
518
+ * ` specifier` {string}
519
+ * ` context` {Object}
520
+ * ` conditions` {string[]}
521
+ * ` parentURL` {string}
522
+ * ` defaultResolve` {Function}
523
+ * Returns: {Object}
524
+ * ` url` {string}
525
+
518
526
The ` resolve` hook returns the resolved file URL for a given module specifier
519
527
and parent URL. The module specifier is the string in an ` import ` statement or
520
528
` 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
535
543
/**
536
544
* @param {string} specifier
537
545
* @param {{
546
+ * conditions: !Array<string>,
538
547
* parentURL: !(string | undefined),
539
- * conditions: !(Array<string>),
540
548
* }} context
541
549
* @param {Function} defaultResolve
542
- * @returns {!( Promise<{ url: string }>) }
550
+ * @returns {Promise<{ url: string }>}
543
551
*/
544
552
export async function resolve (specifier , context , defaultResolve ) {
545
553
const { parentURL = null } = context;
@@ -565,30 +573,35 @@ export async function resolve(specifier, context, defaultResolve) {
565
573
}
566
574
` ` `
567
575
568
- #### <code> getFormat</code> hook
576
+ #### ` getFormat (url, context, defaultGetFormat) `
569
577
570
578
> Note: The loaders API is being redesigned. This hook may disappear or its
571
579
> signature may change. Do not rely on the API described below.
572
580
581
+ * ` url` {string}
582
+ * ` context` {Object}
583
+ * ` defaultGetFormat` {Function}
584
+ * Returns: {Object}
585
+ * ` format` {string}
586
+
573
587
The ` getFormat` hook provides a way to define a custom method of determining how
574
588
a URL should be interpreted. The ` format` returned also affects what the
575
589
acceptable forms of source values are for a module when parsing. This can be one
576
590
of the following:
577
591
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` ][] } |
586
600
587
601
Note: These types all correspond to classes defined in ECMAScript.
588
602
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 ` ][].
592
605
593
606
Note: If the source value of a text-based format (i.e., ` ' json' ` , ` ' module' ` ) is
594
607
not a string, it will be converted to a string using [` util .TextDecoder ` ][].
@@ -614,11 +627,18 @@ export async function getFormat(url, context, defaultGetFormat) {
614
627
}
615
628
` ` `
616
629
617
- #### <code> getSource</code> hook
630
+ #### ` getSource (url, context, defaultGetSource) `
618
631
619
632
> Note: The loaders API is being redesigned. This hook may disappear or its
620
633
> signature may change. Do not rely on the API described below.
621
634
635
+ * ` url` {string}
636
+ * ` context` {Object}
637
+ * ` format` {string}
638
+ * ` defaultGetSource` {Function}
639
+ * Returns: {Object}
640
+ * ` source` {string|SharedArrayBuffer|Uint8Array}
641
+
622
642
The ` getSource` hook provides a way to define a custom method for retrieving
623
643
the source code of an ES module specifier. This would allow a loader to
624
644
potentially avoid reading files from disk.
@@ -628,7 +648,7 @@ potentially avoid reading files from disk.
628
648
* @param {string} url
629
649
* @param {{ format: string }} context
630
650
* @param {Function} defaultGetSource
631
- * @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
651
+ * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
632
652
*/
633
653
export async function getSource (url , context , defaultGetSource ) {
634
654
const { format } = context;
@@ -644,7 +664,7 @@ export async function getSource(url, context, defaultGetSource) {
644
664
}
645
665
` ` `
646
666
647
- #### <code> transformSource</code> hook
667
+ #### ` transformSource (source, context, defaultTransformSource) `
648
668
649
669
` ` ` console
650
670
NODE_OPTIONS = ' --experimental-loader ./custom-loader.mjs' node x .js
@@ -653,6 +673,13 @@ NODE_OPTIONS='--experimental-loader ./custom-loader.mjs' node x.js
653
673
> Note: The loaders API is being redesigned. This hook may disappear or its
654
674
> signature may change. Do not rely on the API described below.
655
675
676
+ * ` source` {string|SharedArrayBuffer|Uint8Array}
677
+ * ` context` {Object}
678
+ * ` format` {string}
679
+ * ` url` {string}
680
+ * Returns: {Object}
681
+ * ` source` {string|SharedArrayBuffer|Uint8Array}
682
+
656
683
The ` transformSource` hook provides a way to modify the source code of a loaded
657
684
ES module file after the source string has been loaded but before Node.js has
658
685
done anything with it.
@@ -663,13 +690,13 @@ unknown-to-Node.js file extensions. See the [transpiler loader example][] below.
663
690
664
691
` ` ` js
665
692
/**
666
- * @param {!(SharedArrayBuffer | string | Uint8Array)} source
693
+ * @param {!(string | SharedArrayBuffer | Uint8Array)} source
667
694
* @param {{
668
- * url: string,
669
695
* format: string,
696
+ * url: string,
670
697
* }} context
671
698
* @param {Function} defaultTransformSource
672
- * @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
699
+ * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
673
700
*/
674
701
export async function transformSource (source , context , defaultTransformSource ) {
675
702
const { url , format } = context;
@@ -685,11 +712,13 @@ export async function transformSource(source, context, defaultTransformSource) {
685
712
}
686
713
` ` `
687
714
688
- #### <code> getGlobalPreloadCode</code> hook
715
+ #### ` getGlobalPreloadCode () `
689
716
690
717
> Note: The loaders API is being redesigned. This hook may disappear or its
691
718
> signature may change. Do not rely on the API described below.
692
719
720
+ * Returns: {string}
721
+
693
722
Sometimes it can be necessary to run some code inside of the same global scope
694
723
that the application will run in. This hook allows to return a string that will
695
724
be ran as sloppy-mode script on startup.
@@ -726,7 +755,7 @@ const require = createRequire(process.cwd() + '/<preload>');
726
755
To create a custom dynamic module that doesn't correspond to one of the
727
756
existing ` format` interpretations, the ` dynamicInstantiate` hook can be used.
728
757
This hook is called only for modules that return ` format: ' dynamic' ` from
729
- the [ ` getFormat` hook][] .
758
+ the ` getFormat` hook.
730
759
731
760
` ` ` js
732
761
/**
@@ -1254,18 +1283,17 @@ success!
1254
1283
[WHATWG JSON modules specification]: https: // html.spec.whatwg.org/#creating-a-json-module-script
1255
1284
[` data:` URLs]: https: // developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
1256
1285
[` export` ]: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
1257
- [` getFormat` hook]: #esm_code_getformat_code_hook
1258
1286
[` import()` ]: #esm_import_expressions
1259
1287
[` import.meta.url` ]: #esm_import_meta
1260
1288
[` import` ]: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
1261
1289
[` module.createRequire()` ]: modules .html #modules_module_createrequire_filename
1262
1290
[` 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
1269
1297
[` util.TextDecoder` ]: util .html #util_class_util_textdecoder
1270
1298
[cjs- module - lexer]: https: // github.com/guybedford/cjs-module-lexer/tree/0.4.0
1271
1299
[dynamic instantiate hook]: #esm_code_dynamicinstantiate_code_hook
0 commit comments