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

module: add support for URL to import.meta.resolve #38587

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/esm.md
Expand Up @@ -280,6 +280,15 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url));
```

### `import.meta.resolve(specifier[, parent])`
<!--
added:
- v13.9.0
- v12.16.2
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/38587
description: Add support for WHATWG `URL` object to `parentURL` parameter.
-->

> Stability: 1 - Experimental

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -13,15 +13,15 @@ const {
} = primordials;

const {
ERR_INVALID_ARG_TYPE,
jasnell marked this conversation as resolved.
Show resolved Hide resolved
ERR_INVALID_ARG_VALUE,
ERR_INVALID_MODULE_SPECIFIER,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_RETURN_VALUE,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
const { URL, pathToFileURL } = require('internal/url');
const { validateString } = require('internal/validators');
const { URL, pathToFileURL, isURLInstance } = require('internal/url');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');

Expand Down Expand Up @@ -83,8 +83,8 @@ class Loader {

async resolve(specifier, parentURL) {
const isMain = parentURL === undefined;
if (!isMain)
validateString(parentURL, 'parentURL');
if (!isMain && typeof parentURL !== 'string' && !isURLInstance(parentURL))
throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL);

const resolveResponse = await this._resolve(
specifier, { parentURL, conditions: DEFAULT_CONDITIONS }, defaultResolve);
Expand Down
13 changes: 13 additions & 0 deletions test/es-module/test-esm-import-meta-resolve.mjs
Expand Up @@ -19,6 +19,19 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
await import.meta.resolve('../fixtures/empty-with-bom.txt'),
fixtures + 'empty-with-bom.txt');
assert.strictEqual(await import.meta.resolve('../fixtures/'), fixtures);
assert.strictEqual(
await import.meta.resolve('../fixtures/', import.meta.url),
fixtures);
assert.strictEqual(
await import.meta.resolve('../fixtures/', new URL(import.meta.url)),
fixtures);
await Promise.all(
[[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) =>
assert.rejects(import.meta.resolve('../fixtures/', arg), {
code: 'ERR_INVALID_ARG_TYPE',
})
)
);
assert.strictEqual(await import.meta.resolve('baz/', fixtures),
fixtures + 'node_modules/baz/');
})().then(mustCall());