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 committed Sep 12, 2024
1 parent c765b59 commit 936d76d
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 @@ -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',
});
}

0 comments on commit 936d76d

Please sign in to comment.