Skip to content

Commit 93af04a

Browse files
aduh95targos
authored andcommitted
module: add support for URL to import.meta.resolve
PR-URL: #38587 Refs: #38585 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ef72490 commit 93af04a

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/api/esm.md

+4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url));
281281
added:
282282
- v13.9.0
283283
- v12.16.2
284+
changes:
285+
- version: REPLACEME
286+
pr-url: https://github.com/nodejs/node/pull/38587
287+
description: Add support for WHATWG `URL` object to `parentURL` parameter.
284288
-->
285289
286290
> Stability: 1 - Experimental

lib/internal/modules/esm/loader.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const {
1313
} = primordials;
1414

1515
const {
16+
ERR_INVALID_ARG_TYPE,
1617
ERR_INVALID_ARG_VALUE,
1718
ERR_INVALID_MODULE_SPECIFIER,
1819
ERR_INVALID_RETURN_PROPERTY,
1920
ERR_INVALID_RETURN_PROPERTY_VALUE,
2021
ERR_INVALID_RETURN_VALUE,
2122
ERR_UNKNOWN_MODULE_FORMAT
2223
} = require('internal/errors').codes;
23-
const { URL, pathToFileURL } = require('internal/url');
24-
const { validateString } = require('internal/validators');
24+
const { URL, pathToFileURL, isURLInstance } = require('internal/url');
2525
const ModuleMap = require('internal/modules/esm/module_map');
2626
const ModuleJob = require('internal/modules/esm/module_job');
2727

@@ -83,8 +83,8 @@ class Loader {
8383

8484
async resolve(specifier, parentURL) {
8585
const isMain = parentURL === undefined;
86-
if (!isMain)
87-
validateString(parentURL, 'parentURL');
86+
if (!isMain && typeof parentURL !== 'string' && !isURLInstance(parentURL))
87+
throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL);
8888

8989
const resolveResponse = await this._resolve(
9090
specifier, { parentURL, conditions: DEFAULT_CONDITIONS }, defaultResolve);

test/es-module/test-esm-import-meta-resolve.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
1919
await import.meta.resolve('../fixtures/empty-with-bom.txt'),
2020
fixtures + 'empty-with-bom.txt');
2121
assert.strictEqual(await import.meta.resolve('../fixtures/'), fixtures);
22+
assert.strictEqual(
23+
await import.meta.resolve('../fixtures/', import.meta.url),
24+
fixtures);
25+
assert.strictEqual(
26+
await import.meta.resolve('../fixtures/', new URL(import.meta.url)),
27+
fixtures);
28+
await Promise.all(
29+
[[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) =>
30+
assert.rejects(import.meta.resolve('../fixtures/', arg), {
31+
code: 'ERR_INVALID_ARG_TYPE',
32+
})
33+
)
34+
);
2235
assert.strictEqual(await import.meta.resolve('baz/', fixtures),
2336
fixtures + 'node_modules/baz/');
2437
})().then(mustCall());

0 commit comments

Comments
 (0)