Skip to content

Commit c150976

Browse files
aduh95targos
authored andcommitted
esm: initialize import.meta on eval
PR-URL: #47551 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 3154543 commit c150976

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎lib/internal/modules/esm/loader.js‎

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

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

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

57
constdirname=import.meta.url.slice(0,import.meta.url.lastIndexOf('/')+1);
68
constfixtures=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+
constcp=spawn(execPath,[
38+
'--experimental-import-meta-resolve',
39+
'--input-type=module',
40+
'--eval','console.log(typeof import.meta.resolve)',
41+
]);
42+
assert.match((awaitcp.stdout.toArray()).toString(),/^function\r?\n$/);
43+
}
44+
45+
{
46+
constcp=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((awaitcp.stdout.toArray()).toString(),/^function\r?\n$/);
52+
}
53+
54+
{
55+
constcp=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((awaitcp.stdout.toArray()).toString(),/^node:os\r?\n$/);
61+
}
62+
63+
{
64+
constcp=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((awaitcp.stdout.toArray()).toString(),/^node:os\r?\n$/);
70+
}

0 commit comments

Comments
 (0)