Skip to content

Commit

Permalink
esm: initialize import.meta on eval
Browse files Browse the repository at this point in the history
PR-URL: #47551
Backport-PR-URL: #50669
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
  • Loading branch information
aduh95 authored and targos committed Nov 23, 2023
1 parent 39fbce7 commit 5de37a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class DefaultModuleLoader {
const { setCallbackForWrap } = require('internal/modules/esm/utils');
const module = new ModuleWrap(url, undefined, source, 0, 0);
setCallbackForWrap(module, {
initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, { url }),
importModuleDynamically: (specifier, { url }, importAssertions) => {
return this.import(specifier, url, importAssertions);
},
Expand Down
38 changes: 38 additions & 0 deletions test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Flags: --experimental-import-meta-resolve
import '../common/index.mjs';
import assert from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';
Expand Down Expand Up @@ -30,3 +32,39 @@ assert.strictEqual(
);
assert.strictEqual(import.meta.resolve('baz/', fixtures),
fixtures + 'node_modules/baz/');

{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
'--eval', 'console.log(typeof import.meta.resolve)',
]);
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
}

{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
]);
cp.stdin.end('console.log(typeof import.meta.resolve)');
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
}

{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
'--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"',
]);
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
}

{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
]);
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$/);
}

0 comments on commit 5de37a1

Please sign in to comment.