Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm,doc: fix custom loader hook snippet typings #33563

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 34 additions & 39 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,25 +1160,26 @@ condition list **must** be passed through to the `defaultResolve` function.
```js
/**
* @param {string} specifier
* @param {object} context
* @param {string} context.parentURL
* @param {string[]} context.conditions
* @param {function} defaultResolve
* @returns {object} response
* @returns {string} response.url
* @param {{
* parentURL: !(string | undefined),
* conditions: !(Array<string>),
* }} context
* @param {Function} defaultResolve
* @returns {!(Promise<{ url: string }>)}
*/
export async function resolve(specifier, context, defaultResolve) {
const { parentURL = null } = context;
if (someCondition) {
if (Math.random() > 0.5) { // Some condition.
// For some or all specifiers, do some custom logic for resolving.
// Always return an object of the form {url: <string>}
// Always return an object of the form {url: <string>}.
return {
url: (parentURL) ?
new URL(specifier, parentURL).href : new URL(specifier).href
url: parentURL ?
new URL(specifier, parentURL).href :
new URL(specifier).href,
};
}
if (anotherCondition) {
// When calling the defaultResolve, the arguments can be modified. In this
if (Math.random() < 0.5) { // Another condition.
// When calling `defaultResolve`, the arguments can be modified. In this
// case it's adding another value for matching conditional exports.
return defaultResolve(specifier, {
...context,
Expand Down Expand Up @@ -1220,18 +1221,17 @@ not a string, it will be converted to a string using [`util.TextDecoder`][].
```js
/**
* @param {string} url
* @param {object} context (currently empty)
* @param {function} defaultGetFormat
* @returns {object} response
* @returns {string} response.format
* @param {Object} context (currently empty)
* @param {Function} defaultGetFormat
* @returns {Promise<{ format: string }>}
*/
export async function getFormat(url, context, defaultGetFormat) {
if (someCondition) {
if (Math.random() > 0.5) { // Some condition.
// For some or all URLs, do some custom logic for determining format.
// Always return an object of the form {format: <string>}, where the
// format is one of the strings in the table above.
return {
format: 'module'
format: 'module',
};
}
// Defer to Node.js for all other URLs.
Expand All @@ -1251,19 +1251,17 @@ potentially avoid reading files from disk.
```js
/**
* @param {string} url
* @param {object} context
* @param {string} context.format
* @param {function} defaultGetSource
* @returns {object} response
* @returns {string|buffer} response.source
* @param {{ format: string }} context
* @param {Function} defaultGetSource
* @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
*/
export async function getSource(url, context, defaultGetSource) {
const { format } = context;
if (someCondition) {
if (Math.random() > 0.5) { // Some condition.
// For some or all URLs, do some custom logic for retrieving the source.
// Always return an object of the form {source: <string|buffer>}.
return {
source: '...'
source: '...',
};
}
// Defer to Node.js for all other URLs.
Expand All @@ -1286,28 +1284,25 @@ unknown-to-Node.js file extensions. See the [transpiler loader example][] below.

```js
/**
* @param {string|buffer} source
* @param {object} context
* @param {string} context.url
* @param {string} context.format
* @param {function} defaultTransformSource
* @returns {object} response
* @returns {string|buffer} response.source
* @param {!(SharedArrayBuffer | string | Uint8Array)} source
* @param {{
* url: string,
* format: string,
* }} context
* @param {Function} defaultTransformSource
* @returns {Promise<{ source: !(SharedArrayBuffer | string | Uint8Array) }>}
*/
export async function transformSource(source,
context,
defaultTransformSource) {
export async function transformSource(source, context, defaultTransformSource) {
const { url, format } = context;
if (someCondition) {
if (Math.random() > 0.5) { // Some condition.
// For some or all URLs, do some custom logic for modifying the source.
// Always return an object of the form {source: <string|buffer>}.
return {
source: '...'
source: '...',
};
}
// Defer to Node.js for all other sources.
return defaultTransformSource(
source, context, defaultTransformSource);
return defaultTransformSource(source, context, defaultTransformSource);
}
```

Expand Down