From 8a6454f02b8fc7ec164b544a147e26ca3f6fd7fd Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 12 Feb 2022 16:34:48 +0100 Subject: [PATCH] esm: use `[stdin]` as module URL when reading stdin --- lib/internal/main/eval_stdin.js | 5 ++++- lib/internal/modules/esm/loader.js | 4 ++-- lib/internal/process/execution.js | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js index d97dbece8f0f56..5c7662b0153e50 100644 --- a/lib/internal/main/eval_stdin.js +++ b/lib/internal/main/eval_stdin.js @@ -5,6 +5,9 @@ const { prepareMainThreadExecution } = require('internal/bootstrap/pre_execution'); +const { + pathToFileURL, +} = require('internal/url'); const { getOptionValue } = require('internal/options'); @@ -24,7 +27,7 @@ readStdin((code) => { const print = getOptionValue('--print'); if (getOptionValue('--input-type') === 'module') - evalModule(code, print); + evalModule(code, print, pathToFileURL(`${process.cwd()}/[stdin]`).href); else evalScript('[stdin]', code, diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index d5e0b61af6a309..7e146149d4f976 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -90,7 +90,7 @@ class ESMLoader { /** * The index for assigning unique URLs to anonymous module evaluation */ - evalIndex = 0; + #evalIndex = 0; /** * Registry of loaded modules, akin to `require.cache` @@ -202,7 +202,7 @@ class ESMLoader { async eval( source, - url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href + url = pathToFileURL(`${process.cwd()}/[eval${++this.#evalIndex}]`).href ) { const evalInstance = (url) => { const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 4a8e51f694f7e6..a94735e9bd2152 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -39,13 +39,13 @@ function tryGetCwd() { } } -function evalModule(source, print) { +function evalModule(source, print, url = undefined) { if (print) { throw new ERR_EVAL_ESM_CANNOT_PRINT(); } const { loadESM } = require('internal/process/esm_loader'); const { handleMainPromise } = require('internal/modules/run_main'); - return handleMainPromise(loadESM((loader) => loader.eval(source))); + return handleMainPromise(loadESM((loader) => loader.eval(source, url))); } function evalScript(name, body, breakFirstLine, print) {