Skip to content

Commit e04c622

Browse files
committed
split it into 3 utils
1 parent 74dca81 commit e04c622

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

doc/api/module.md

+48-10
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ resolution and loading behavior. See [Customization hooks][].
216216
217217
This feature requires `--allow-worker` if used with the [Permission Model][].
218218
219-
### `module.resolveLoadAndCache(specifier[, parentURL[, importAttributes[, conditions]]])`
219+
### `module.resolve(specifier, parentURL[, importAttributes]])`
220220
221221
<!-- YAML
222222
added: REPLACEME
@@ -227,21 +227,59 @@ added: REPLACEME
227227
* `specifier` {string|URL} Module to resolve and load; this should be
228228
the same string that would be passed to `import()`, except that if it is
229229
relative, it is resolved relative to `parentURL`.
230-
* `parentURL` {string|URL|undefined} If you want to resolve `specifier` relative to a base
231-
URL, such as `import.meta.url`, you can pass that URL here. If not provided,
232-
the `resolve` step will be skipped. **Default:** {undefined}.
233-
* `importAttributes` {Object}
230+
* `parentURL` {string|URL|undefined} The base URL to resolve `specifier` from,
231+
such as `import.meta.url`.
232+
* `importAttributes` {Object} **Default:** an empty object.
233+
* Returns: {Promise} fulfills with a string containing the full URL of the
234+
potential module corresponding to the given specifier.
235+
236+
Analogous to `import.meta.resolve`, but asynchronous, accessible from CommonJS
237+
modules, and with additional parameters.
238+
239+
### `module.load(url[, importAttributes[, conditions]]])`
240+
241+
<!-- YAML
242+
added: REPLACEME
243+
-->
244+
245+
> Stability: 1 - Experimental
246+
247+
* `url` {string|URL|undefined} The URL of the module to load.
248+
* `importAttributes` {Object} **Default:** an empty object.
249+
* `conditions` {Array}
250+
* Returns: {Promise} fulfills with an object with the following properties:
251+
* `url` {string} The absolute URL for that module
252+
* `format` {string} The format this module will be parsed as.
253+
* `source` {null|TypedArray}
254+
255+
This API tells you how the passed URL would be loaded by the module loader if
256+
it was imported in the current process – or, if `conditions` is provided, how
257+
would it be loaded in a process with such configuration.
258+
259+
### `module.resolveLoadAndCache(specifier, parentURL[, importAttributes[, conditions]]])`
260+
261+
<!-- YAML
262+
added: REPLACEME
263+
-->
264+
265+
> Stability: 1 - Experimental
266+
267+
* `specifier` {string|URL} Module to resolve and load; this should be
268+
the same string that would be passed to `import()`, except that if it is
269+
relative, it is resolved relative to `parentURL`.
270+
* `parentURL` {string|URL|undefined} The base URL to resolve `specifier` from,
271+
such as `import.meta.url`.
272+
* `importAttributes` {Object} **Default:** an empty object.
234273
* `conditions` {Array}
235274
* Returns: {Promise} fulfills with an object with the following properties:
236275
* `url` {string} The absolute URL for that module
237276
* `format` {string} The format this module will be parsed as.
238277
* `source` {null|TypedArray}
239278
240-
This API tells you how a specific URL will be loaded by the module loader if
241-
it was imported from the `parentURL` in the current process. If the module was
242-
already imported before `resolveLoadAndCache` is called, the cached version is
243-
returned; if not, it will populate the cache so future calls to
244-
`resolveLoadAndCache` or `import` do not re-do the work.
279+
This API tells you how the passed specifier would be loaded by the module loader if
280+
it was imported from the `parentURL` in the current process – or, if
281+
`conditions` is provided, how would it be loaded in a process with such
282+
configuration.
245283
246284
### `module.registerHooks(options)`
247285

lib/internal/modules/helpers.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,28 @@ ObjectFreeze(compileCacheStatus);
391391
const constants = { __proto__: null, compileCacheStatus };
392392
ObjectFreeze(constants);
393393

394-
async function resolveLoadAndCache(specifier, base = undefined, importAttributes = kEmptyObject, conditions) {
394+
async function resolve(specifier, base, importAttributes = kEmptyObject) {
395395
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
396-
let url, resolveFormat;
397-
if (base == null) {
398-
url = `${specifier}`;
399-
} else {
400-
({ url, format: resolveFormat } = await cascadedLoader.resolve(`${specifier}`, `${base}`, importAttributes));
401-
}
396+
const { url, format: resolveFormat } = await cascadedLoader.resolve(`${specifier}`, `${base}`, importAttributes);
397+
return url;
398+
}
399+
async function load(url, importAttributes = kEmptyObject, conditions) {
400+
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
401+
const { format, source } = await cascadedLoader.load(url, {
402+
__proto__: null,
403+
importAttributes,
404+
conditions,
405+
});
406+
return {
407+
__proto__: null,
408+
format,
409+
source,
410+
url,
411+
};
412+
}
413+
async function resolveAndLoad(specifier, base = undefined, importAttributes = kEmptyObject, conditions) {
414+
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
415+
const { url, format: resolveFormat } = await cascadedLoader.resolve(`${specifier}`, `${base}`, importAttributes);
402416
const { format, source } = await cascadedLoader.load(url, {
403417
__proto__: null,
404418
importAttributes,
@@ -435,7 +449,9 @@ module.exports = {
435449
loadBuiltinModule,
436450
makeRequireFunction,
437451
normalizeReferrerURL,
438-
resolveLoadAndCache,
452+
resolve,
453+
load,
454+
resolveAndLoad,
439455
stringify,
440456
stripBOM,
441457
toRealPath,

lib/module.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const {
1515
enableCompileCache,
1616
flushCompileCache,
1717
getCompileCacheDir,
18-
resolveLoadAndCache,
18+
resolve,
19+
load,
20+
resolveAndLoad,
1921
} = require('internal/modules/helpers');
2022
const {
2123
findPackageJSON,
@@ -29,7 +31,9 @@ Module.findSourceMap = findSourceMap;
2931
Module.flushCompileCache = flushCompileCache;
3032
Module.getCompileCacheDir = getCompileCacheDir;
3133
Module.register = register;
32-
Module.resolveLoadAndCache = resolveLoadAndCache;
34+
Module.resolve= resolveLoadAndCache;
35+
Module.load = resolveLoadAndCache;
36+
Module.resolveAndLoad= resolveLoadAndCache;
3337
Module.SourceMap = SourceMap;
3438
Module.stripTypeScriptTypes = stripTypeScriptTypes;
3539

0 commit comments

Comments
 (0)