mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
repl: support --loader option in builtin REPL
Fixes: https://github.com/nodejs/node/issues/33435 PR-URL: https://github.com/nodejs/node/pull/33437 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
8 files changed
+56
-44
No files matched your search
+26
-23
@@ -7,6 +7,7 @@ const {
|
||||
prepareMainThreadExecution
|
||||
} = require('internal/bootstrap/pre_execution');
|
||||
|
||||
const esmLoader = require('internal/process/esm_loader');
|
||||
const {
|
||||
evalScript
|
||||
} = require('internal/process/execution');
|
||||
@@ -32,31 +33,33 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Welcome to Node.js ${process.version}.\n` +
|
||||
'Type ".help" for more information.');
|
||||
esmLoader.loadESM(() => {
|
||||
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();
|
||||
});
|
||||
const cliRepl = require('internal/repl');
|
||||
cliRepl.createInternalRepl(process.env, (err, repl) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
process.exit();
|
||||
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'));
|
||||
}
|
||||
// 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'));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -40,21 +40,10 @@ function shouldUseESMLoader(mainPath) {
|
||||
function runMainESM(mainPath) {
|
||||
const esmLoader = require('internal/process/esm_loader');
|
||||
const { pathToFileURL } = require('internal/url');
|
||||
const { hasUncaughtExceptionCaptureCallback } =
|
||||
require('internal/process/execution');
|
||||
return esmLoader.initializeLoader().then(() => {
|
||||
esmLoader.loadESM((ESMLoader) => {
|
||||
const main = path.isAbsolute(mainPath) ?
|
||||
pathToFileURL(mainPath).href : mainPath;
|
||||
return esmLoader.ESMLoader.import(main);
|
||||
}).catch((e) => {
|
||||
if (hasUncaughtExceptionCaptureCallback()) {
|
||||
process._fatalException(e);
|
||||
return;
|
||||
}
|
||||
internalBinding('errors').triggerUncaughtException(
|
||||
e,
|
||||
true /* fromPromise */
|
||||
);
|
||||
return ESMLoader.import(main);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ const {
|
||||
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
|
||||
} = require('internal/errors').codes;
|
||||
const { Loader } = require('internal/modules/esm/loader');
|
||||
const {
|
||||
hasUncaughtExceptionCaptureCallback,
|
||||
} = require('internal/process/execution');
|
||||
const { pathToFileURL } = require('internal/url');
|
||||
const {
|
||||
getModuleFromWrap,
|
||||
@@ -34,7 +37,6 @@ exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
|
||||
let ESMLoader = new Loader();
|
||||
exports.ESMLoader = ESMLoader;
|
||||
|
||||
exports.initializeLoader = initializeLoader;
|
||||
async function initializeLoader() {
|
||||
const { getOptionValue } = require('internal/options');
|
||||
const userLoader = getOptionValue('--experimental-loader');
|
||||
@@ -59,3 +61,19 @@ async function initializeLoader() {
|
||||
return exports.ESMLoader = ESMLoader;
|
||||
})();
|
||||
}
|
||||
|
||||
exports.loadESM = async function loadESM(callback) {
|
||||
try {
|
||||
await initializeLoader();
|
||||
await callback(ESMLoader);
|
||||
} catch (err) {
|
||||
if (hasUncaughtExceptionCaptureCallback()) {
|
||||
process._fatalException(err);
|
||||
return;
|
||||
}
|
||||
internalBinding('errors').triggerUncaughtException(
|
||||
err,
|
||||
true /* fromPromise */
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -5,3 +5,4 @@ SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-ex
|
||||
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
|
||||
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
|
||||
at async Loader.import (internal/modules/esm/loader.js:*:*)
|
||||
at async Object.loadESM (internal/process/esm_loader.js:*:*)
|
||||
@@ -5,3 +5,4 @@ SyntaxError: The requested module './module-named-exports.mjs' does not provide
|
||||
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
|
||||
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
|
||||
at async Loader.import (internal/modules/esm/loader.js:*:*)
|
||||
at async Object.loadESM (internal/process/esm_loader.js:*:*)
|
||||
@@ -1,6 +1,6 @@
|
||||
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
|
||||
(Use `node --trace-warnings ...` to show where the warning was created)
|
||||
internal/modules/run_main.js:*
|
||||
internal/process/esm_loader.js:*
|
||||
internalBinding('errors').triggerUncaughtException(
|
||||
^
|
||||
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from *
|
||||
@@ -12,7 +12,7 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from *
|
||||
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
|
||||
at Loader.import (internal/modules/esm/loader.js:*:*)
|
||||
at internal/process/esm_loader.js:*:*
|
||||
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
|
||||
at runMainESM (internal/modules/run_main.js:*:*) {
|
||||
at initializeLoader (internal/process/esm_loader.js:*:*)
|
||||
at Object.loadESM (internal/process/esm_loader.js:*:*) {
|
||||
code: 'ERR_MODULE_NOT_FOUND'
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
internal/modules/run_main.js:*
|
||||
internal/process/esm_loader.js:*
|
||||
internalBinding('errors').triggerUncaughtException(
|
||||
^
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
|
||||
(Use `node --trace-warnings ...` to show where the warning was created)
|
||||
internal/modules/run_main.js:*
|
||||
internal/process/esm_loader.js:*
|
||||
internalBinding('errors').triggerUncaughtException(
|
||||
^
|
||||
|
||||
@@ -14,7 +14,7 @@ Did you mean to import ./test/common/fixtures.js?
|
||||
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
|
||||
at Loader.import (internal/modules/esm/loader.js:*:*)
|
||||
at internal/process/esm_loader.js:*:*
|
||||
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
|
||||
at runMainESM (internal/modules/run_main.js:*:*) {
|
||||
at initializeLoader (internal/process/esm_loader.js:*:*)
|
||||
at Object.loadESM (internal/process/esm_loader.js:*:*) {
|
||||
code: 'ERR_MODULE_NOT_FOUND'
|
||||
}
|
||||
Reference in new issue
Block a user