Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: use [stdin] as module URL when reading stdin #41946

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/main/eval_stdin.js
Expand Up @@ -5,6 +5,9 @@
const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
const {
pathToFileURL,
} = require('internal/url');

const { getOptionValue } = require('internal/options');

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -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`
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/process/execution.js
Expand Up @@ -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) {
Expand Down