From 40fa6e42466867fe86f29345cc1d56569bf40e1f Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 19 Apr 2023 20:55:36 +0200 Subject: [PATCH] esm: initialize `import.meta` on eval PR-URL: https://github.com/nodejs/node/pull/47551 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/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 8bff8b2420789a..c6fb40922e1a73 100644 --- a/lib/internal/modules/esm/loader.js +++ b/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/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index 843ca6d747140a..22139122eb505c 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/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$/); +}