Split the `internal/process/esm_loader` file which contains the singleton cascaded loader: - The the singleton cascaded loader now directly resides in `internal/modules/esm/loader`, where the constructor also lives. This file is the root of most circular dependency of ESM code, (because components of the loader need the singleton itself), so this makes the dependency more obvious. Added comments about loading it lazily to avoid circular dependency. - The getter to the cascaded loader is also turned into a method to make the side effect explicit. - The sequence of `loadESM()` and `handleMainPromise` is now merged together into `runEntryPointWithESMLoader()` in `internal/modules/run_main` because this is intended to run entry points with the ESM loader and not just any module. - Documents how top-level await is handled. PR-URL: https://github.com/nodejs/node/pull/51999 Fixes: https://github.com/nodejs/node/issues/42868 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
69 lines
2 KiB
JavaScript
69 lines
2 KiB
JavaScript
'use strict';
|
|
|
|
// Create the REPL if `-i` or `--interactive` is passed, or if
|
|
// the main module is not specified and stdin is a TTY.
|
|
|
|
const {
|
|
prepareMainThreadExecution,
|
|
markBootstrapComplete,
|
|
} = require('internal/process/pre_execution');
|
|
|
|
const {
|
|
evalScript,
|
|
} = require('internal/process/execution');
|
|
|
|
const console = require('internal/console/global');
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const { exitCodes: { kInvalidCommandLineArgument } } = internalBinding('errors');
|
|
|
|
prepareMainThreadExecution();
|
|
|
|
markBootstrapComplete();
|
|
|
|
if (process.env.NODE_REPL_EXTERNAL_MODULE) {
|
|
require('internal/modules/cjs/loader')
|
|
.Module
|
|
._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true);
|
|
} else {
|
|
// --input-type flag not supported in REPL
|
|
if (getOptionValue('--input-type')) {
|
|
// If we can't write to stderr, we'd like to make this a noop,
|
|
// so use console.error.
|
|
console.error('Cannot specify --input-type for REPL');
|
|
process.exit(kInvalidCommandLineArgument);
|
|
}
|
|
|
|
require('internal/modules/run_main').runEntryPointWithESMLoader(() => {
|
|
console.log(`Welcome to Node.js ${process.version}.\n` +
|
|
'Type ".help" for more information.');
|
|
|
|
const cliRepl = require('internal/repl');
|
|
cliRepl.createInternalRepl(process.env, (err, repl) => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
repl.on('exit', () => {
|
|
if (repl._flushing) {
|
|
repl.pause();
|
|
return repl.once('flushHistory', () => {
|
|
process.exit();
|
|
});
|
|
}
|
|
process.exit();
|
|
});
|
|
});
|
|
|
|
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
|
|
// evaluate the code in the current context.
|
|
if (getOptionValue('[has_eval_string]')) {
|
|
evalScript('[eval]',
|
|
getOptionValue('--eval'),
|
|
getOptionValue('--inspect-brk'),
|
|
getOptionValue('--print'));
|
|
}
|
|
// The TLAs in the REPL are still run as scripts, just transformed as async
|
|
// IIFEs for the REPL code itself to await on.
|
|
});
|
|
}
|