Skip to content

Commit c150976

Browse files
aduh95targos
authored andcommitted
esm: initialize import.meta on eval
PR-URL: #47551 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 3154543 commit c150976

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/internal/modules/esm/loader.js

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class DefaultModuleLoader {
119119
const { setCallbackForWrap } = require('internal/modules/esm/utils');
120120
const module = new ModuleWrap(url, undefined, source, 0, 0);
121121
setCallbackForWrap(module, {
122+
initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, { url }),
122123
importModuleDynamically: (specifier, { url }, importAssertions) => {
123124
return this.import(specifier, url, importAssertions);
124125
},

test/es-module/test-esm-import-meta-resolve.mjs

+38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Flags: --experimental-import-meta-resolve
22
import '../common/index.mjs';
33
import assert from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
46

57
const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
68
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';
@@ -30,3 +32,39 @@ assert.strictEqual(
3032
);
3133
assert.strictEqual(import.meta.resolve('baz/', fixtures),
3234
fixtures + 'node_modules/baz/');
35+
36+
{
37+
const cp = spawn(execPath, [
38+
'--experimental-import-meta-resolve',
39+
'--input-type=module',
40+
'--eval', 'console.log(typeof import.meta.resolve)',
41+
]);
42+
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
43+
}
44+
45+
{
46+
const cp = spawn(execPath, [
47+
'--experimental-import-meta-resolve',
48+
'--input-type=module',
49+
]);
50+
cp.stdin.end('console.log(typeof import.meta.resolve)');
51+
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
52+
}
53+
54+
{
55+
const cp = spawn(execPath, [
56+
'--experimental-import-meta-resolve',
57+
'--input-type=module',
58+
'--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"',
59+
]);
60+
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
61+
}
62+
63+
{
64+
const cp = spawn(execPath, [
65+
'--experimental-import-meta-resolve',
66+
'--input-type=module',
67+
]);
68+
cp.stdin.end('import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"');
69+
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
70+
}

0 commit comments

Comments
 (0)