Skip to content

Commit 4ebb88f

Browse files
aduh95jasnell
authored andcommitted
module: add support for URL to import.meta.resolve
PR-URL: #38587 Refs: #38585 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8886b63 commit 4ebb88f

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/api/esm.md

+9
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url));
280280
```
281281
282282
### `import.meta.resolve(specifier[, parent])`
283+
<!--
284+
added:
285+
- v13.9.0
286+
- v12.16.2
287+
changes:
288+
- version: REPLACEME
289+
pr-url: https://github.com/nodejs/node/pull/38587
290+
description: Add support for WHATWG `URL` object to `parentURL` parameter.
291+
-->
283292
284293
> Stability: 1 - Experimental
285294

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)