Skip to content

Commit 7a98961

Browse files
Derek LewisMylesBorins
Derek Lewis
authored andcommittedNov 16, 2020
esm: fix hook mistypes and links to types
Prior to this commit, the custom loader hooks were: * missing the Node.js API ref docs types * missing the function signature from their sections * linking directly to the specification (not customary) * had an inconsistent non-nullable JSDoc promise return * had JSDoc object properties that weren't alpha-sorted * designated set of non-nullable types when single was fine Notes: https://www.typescriptlang.org/play/index.html?strictNullChecks=true&useJavaScript=true#code/PQKhCgAIUgBAHAhgJ0QW0gbwM4BdkCWAdgOYC+k28ApgMYEBmB1yUMCK6WmbkftAeyIATArgJDsALkgBCAILJUATwA8eQqQB8AGl58kyakVwBVAEoAZGbIAUG4iUgAfSAFcR1JkWrCAlHrQkGQUgibUAB64vByoGJgAYh604kIUwl6IbgA2uObU2ALZAG7UMUa4bshE2FgACsgCaATY1KqY7sjZMg6kwVpkbMDgkfACyLiQiNjKRLSQDMmpRJBGhSXU9jT0TCw6kGG4kbj7GQxZufnrpX5YUAeSkx2GxmZWkAC8kEQ52cGfD3CUQA3PdGJBbABZRC4AAWADpUCImrZblpIAAGeEAVluHWAwEgAGUmtRAaJlvD7nwCZAEuNKKTIAzENk-lQ6IxmMhsKcBIy0GTaG48E1INkBCQCPMGAy1kVio4qXwaYT5NkAO6IZS1CpVFaIFYCABGACs6JMBAxIHCybLkPEqt1IOp8I4BsqVXrqncVSqnTIXiYLJZIAB+al+77UDWQENbTm7ZD7INvSx+eGwozWqSRv0+WPxjk7bkZrNeQIqsigviDPjgqEwhFI4Qo26qTE4vGQWnyIgCW3IcliCRET2qyAAdVhxgOrOyjkgAAMzhc8gUFdQl-tbVNkCQ3IKTLVaIbIMayWgBKJdsJ4ZAAJIrOEtSO00+tSBiADktUQwlEPpDQHGch2KVk3DtBk0BhWhYUXMIKVHVlIFGcZcGwcdVmoSofVXHJ12uTZiy5PZfSjeFKMOY5Kz9RCR0kGQAG1KPhaiojYoQkMkfZv2AwcAFp6OWb8AF1aOCPwa2Ce5aQAES8FgbX5AA5a9qHhU1antKY2WZQdKG2UieU9b0Vnwy4Nw2BMSzI9iTkgCzCM3KTwDIIA https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540warning_level%2520VERBOSE%250A%252F%252F%2520%2540jscomp_error%2520strictCheckTypes%250A%252F%252F%2520%2540language_out%2520ECMASCRIPT_NEXT%250A%252F%252F%2520%2540checks_only%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250A%252F**%250A%2520*%2520%2540param%2520%257Bstring%257D%2520specifier%250A%2520*%2520%2540param%2520%257B%257B%250A%2520*%2520%2520%2520conditions%253A%2520!Array%253Cstring%253E%252C%250A%2520*%2520%2520%2520parentURL%253A%2520!(string%2520%257C%2520undefined)%252C%250A%2520*%2520%257D%257D%2520context%250A%2520*%2520%2540param%2520%257BFunction%257D%2520defaultResolve%250A%2520*%2520%2540returns%2520%257BPromise%253C%257B%2520url%253A%2520string%2520%257D%253E%257D%250A%2520*%252F%250Aexport%2520async%2520function%2520resolve(specifier%252C%2520context%252C%2520defaultResolve)%2520%257B%250A%2520%2520const%2520%257B%2520parentURL%2520%253D%2520null%2520%257D%2520%253D%2520context%253B%250A%2520%2520if%2520(Math.random()%2520%253E%25200.5)%2520%257B%2520%252F%252F%2520Some%2520condition.%250A%2520%2520%2520%2520%252F%252F%2520For%2520some%2520or%2520all%2520specifiers%252C%2520do%2520some%2520custom%2520logic%2520for%2520resolving.%250A%2520%2520%2520%2520%252F%252F%2520Always%2520return%2520an%2520object%2520of%2520the%2520form%2520%257Burl%253A%2520%253Cstring%253E%257D.%250A%2520%2520%2520%2520return%2520%257B%250A%2520%2520%2520%2520%2520%2520url%253A%2520parentURL%2520%253F%250A%2520%2520%2520%2520%2520%2520%2520%2520new%2520URL(specifier%252C%2520parentURL).href%2520%253A%250A%2520%2520%2520%2520%2520%2520%2520%2520new%2520URL(specifier).href%252C%250A%2520%2520%2520%2520%257D%253B%250A%2520%2520%257D%250A%2520%2520if%2520(Math.random()%2520%253C%25200.5)%2520%257B%2520%252F%252F%2520Another%2520condition.%250A%2520%2520%2520%2520%252F%252F%2520When%2520calling%2520%2560defaultResolve%2560%252C%2520the%2520arguments%2520can%2520be%2520modified.%2520In%2520this%250A%2520%2520%2520%2520%252F%252F%2520case%2520it's%2520adding%2520another%2520value%2520for%2520matching%2520conditional%2520exports.%250A%2520%2520%2520%2520return%2520defaultResolve(specifier%252C%2520%257B%250A%2520%2520%2520%2520%2520%2520...context%252C%250A%2520%2520%2520%2520%2520%2520conditions%253A%2520%255B...context.conditions%252C%2520'another-condition'%255D%252C%250A%2520%2520%2520%2520%257D)%253B%250A%2520%2520%257D%250A%2520%2520%252F%252F%2520Defer%2520to%2520Node.js%2520for%2520all%2520other%2520specifiers.%250A%2520%2520return%2520defaultResolve(specifier%252C%2520context%252C%2520defaultResolve)%253B%250A%257D PR-URL: #34240 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 354f358 commit 7a98961

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed
 

‎doc/api/esm.md

+58-30
Original file line numberDiff line numberDiff line change
@@ -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+
518526
The `resolve` hook returns the resolved file URL for a given module specifier
519527
and 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
*/
544552
export 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+
573587
The `getFormat` hook provides a way to define a custom method of determining how
574588
a URL should be interpreted. The `format` returned also affects what the
575589
acceptable forms of source values are for a module when parsing. This can be one
576590
of 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
587601
Note: 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
593606
Note: If the source value of a text-based format (i.e., `'json'`, `'module'`) is
594607
not 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+
622642
The `getSource` hook provides a way to define a custom method for retrieving
623643
the source code of an ES module specifier. This would allow a loader to
624644
potentially 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
*/
633653
export 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
650670
NODE_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+
656683
The `transformSource` hook provides a way to modify the source code of a loaded
657684
ES module file after the source string has been loaded but before Node.js has
658685
done 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
*/
674701
export 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+
693722
Sometimes it can be necessary to run some code inside of the same global scope
694723
that the application will run in. This hook allows to return a string that will
695724
be ran as sloppy-mode script on startup.
@@ -726,7 +755,7 @@ const require = createRequire(process.cwd() + '/<preload>');
726755
To create a custom dynamic module that doesn't correspond to one of the
727756
existing `format` interpretations, the `dynamicInstantiate` hook can be used.
728757
This 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

Comments
 (0)
Please sign in to comment.