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: fix support for URL instances in import.meta.resolve #54690

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class ModuleLoader {
* @returns {{ format: string, url: URL['href'] }}
*/
resolve(originalSpecifier, parentURL, importAttributes) {
originalSpecifier = `${originalSpecifier}`;
if (this.#customizations) {
return this.#customizations.resolve(originalSpecifier, parentURL, importAttributes);
}
Expand All @@ -530,6 +531,7 @@ class ModuleLoader {
* `import.meta.resolve` which must happen synchronously.
*/
resolveSync(originalSpecifier, parentURL, importAttributes) {
originalSpecifier = `${originalSpecifier}`;
if (this.#customizations) {
return this.#customizations.resolveSync(originalSpecifier, parentURL, importAttributes);
}
Expand Down
19 changes: 18 additions & 1 deletion test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Flags: --experimental-import-meta-resolve
import '../common/index.mjs';
import { spawnPromisified } from '../common/index.mjs';
import assert from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
Expand Down Expand Up @@ -90,3 +90,20 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
cp.stdin.end('import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"');
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
}

{
const result = await spawnPromisified(execPath, [
'--no-warnings',
'--input-type=module',
'--import', 'data:text/javascript,import{register}from"node:module";register("data:text/javascript,")',
'--eval',
'console.log(import.meta.resolve(new URL("http://example.com")))',
]);

assert.deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'http://example.com/\n',
});
}
Loading