Skip to content

Commit

Permalink
esm: fix support for URL instances in import.meta.resolve
Browse files Browse the repository at this point in the history
PR-URL: #54690
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Oct 2, 2024
1 parent d6a2317 commit 0dd4639
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,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 @@ -516,6 +517,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',
});
}

0 comments on commit 0dd4639

Please sign in to comment.