From 740eae8df08ffff45e2e3bdfaf16d258abaadc35 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 01:54:36 +0200 Subject: [PATCH] esm: initialize `import.meta` on eval PR-URL: https://github.com/nodejs/node/pull/47551 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Reviewed-By: Guy Bedford Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith --- .../lib/internal/modules/esm/loader.js | 1 + .../test-esm-import-meta-resolve.mjs | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/graal-nodejs/lib/internal/modules/esm/loader.js b/graal-nodejs/lib/internal/modules/esm/loader.js index 8bff8b24207..c6fb40922e1 100644 --- a/graal-nodejs/lib/internal/modules/esm/loader.js +++ b/graal-nodejs/lib/internal/modules/esm/loader.js @@ -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); }, diff --git a/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs b/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs index 843ca6d7471..22139122eb5 100644 --- a/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs +++ b/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs @@ -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/'; @@ -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$/); +}