Skip to content

Commit 7ca70e4

Browse files
targoscjihrig
authored andcommitted
repl: support --loader option in builtin REPL
Fixes: #33435 PR-URL: #33437 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9fa53b3 commit 7ca70e4

8 files changed

+56
-44
lines changed

lib/internal/main/repl.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
prepareMainThreadExecution
88
} = require('internal/bootstrap/pre_execution');
99

10+
const esmLoader = require('internal/process/esm_loader');
1011
const {
1112
evalScript
1213
} = require('internal/process/execution');
@@ -32,31 +33,33 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) {
3233
process.exit(1);
3334
}
3435

35-
console.log(`Welcome to Node.js ${process.version}.\n` +
36-
'Type ".help" for more information.');
36+
esmLoader.loadESM(() => {
37+
console.log(`Welcome to Node.js ${process.version}.\n` +
38+
'Type ".help" for more information.');
3739

38-
const cliRepl = require('internal/repl');
39-
cliRepl.createInternalRepl(process.env, (err, repl) => {
40-
if (err) {
41-
throw err;
42-
}
43-
repl.on('exit', () => {
44-
if (repl._flushing) {
45-
repl.pause();
46-
return repl.once('flushHistory', () => {
47-
process.exit();
48-
});
40+
const cliRepl = require('internal/repl');
41+
cliRepl.createInternalRepl(process.env, (err, repl) => {
42+
if (err) {
43+
throw err;
4944
}
50-
process.exit();
45+
repl.on('exit', () => {
46+
if (repl._flushing) {
47+
repl.pause();
48+
return repl.once('flushHistory', () => {
49+
process.exit();
50+
});
51+
}
52+
process.exit();
53+
});
5154
});
52-
});
5355

54-
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
55-
// evaluate the code in the current context.
56-
if (getOptionValue('[has_eval_string]')) {
57-
evalScript('[eval]',
58-
getOptionValue('--eval'),
59-
getOptionValue('--inspect-brk'),
60-
getOptionValue('--print'));
61-
}
56+
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
57+
// evaluate the code in the current context.
58+
if (getOptionValue('[has_eval_string]')) {
59+
evalScript('[eval]',
60+
getOptionValue('--eval'),
61+
getOptionValue('--inspect-brk'),
62+
getOptionValue('--print'));
63+
}
64+
});
6265
}

lib/internal/modules/run_main.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,10 @@ function shouldUseESMLoader(mainPath) {
4040
function runMainESM(mainPath) {
4141
const esmLoader = require('internal/process/esm_loader');
4242
const { pathToFileURL } = require('internal/url');
43-
const { hasUncaughtExceptionCaptureCallback } =
44-
require('internal/process/execution');
45-
return esmLoader.initializeLoader().then(() => {
43+
esmLoader.loadESM((ESMLoader) => {
4644
const main = path.isAbsolute(mainPath) ?
4745
pathToFileURL(mainPath).href : mainPath;
48-
return esmLoader.ESMLoader.import(main);
49-
}).catch((e) => {
50-
if (hasUncaughtExceptionCaptureCallback()) {
51-
process._fatalException(e);
52-
return;
53-
}
54-
internalBinding('errors').triggerUncaughtException(
55-
e,
56-
true /* fromPromise */
57-
);
46+
return ESMLoader.import(main);
5847
});
5948
}
6049

lib/internal/process/esm_loader.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const {
44
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
55
} = require('internal/errors').codes;
66
const { Loader } = require('internal/modules/esm/loader');
7+
const {
8+
hasUncaughtExceptionCaptureCallback,
9+
} = require('internal/process/execution');
710
const { pathToFileURL } = require('internal/url');
811
const {
912
getModuleFromWrap,
@@ -34,7 +37,6 @@ exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
3437
let ESMLoader = new Loader();
3538
exports.ESMLoader = ESMLoader;
3639

37-
exports.initializeLoader = initializeLoader;
3840
async function initializeLoader() {
3941
const { getOptionValue } = require('internal/options');
4042
const userLoader = getOptionValue('--experimental-loader');
@@ -59,3 +61,19 @@ async function initializeLoader() {
5961
return exports.ESMLoader = ESMLoader;
6062
})();
6163
}
64+
65+
exports.loadESM = async function loadESM(callback) {
66+
try {
67+
await initializeLoader();
68+
await callback(ESMLoader);
69+
} catch (err) {
70+
if (hasUncaughtExceptionCaptureCallback()) {
71+
process._fatalException(err);
72+
return;
73+
}
74+
internalBinding('errors').triggerUncaughtException(
75+
err,
76+
true /* fromPromise */
77+
);
78+
}
79+
};

test/message/esm_display_syntax_error_import.out

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-ex
55
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
66
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
77
at async Loader.import (internal/modules/esm/loader.js:*:*)
8+
at async Object.loadESM (internal/process/esm_loader.js:*:*)

test/message/esm_display_syntax_error_import_module.out

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ SyntaxError: The requested module './module-named-exports.mjs' does not provide
55
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
66
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
77
at async Loader.import (internal/modules/esm/loader.js:*:*)
8+
at async Object.loadESM (internal/process/esm_loader.js:*:*)

test/message/esm_loader_not_found.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
22
(Use `node --trace-warnings ...` to show where the warning was created)
3-
internal/modules/run_main.js:*
3+
internal/process/esm_loader.js:*
44
internalBinding('errors').triggerUncaughtException(
55
^
66
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 *
1212
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
1313
at Loader.import (internal/modules/esm/loader.js:*:*)
1414
at internal/process/esm_loader.js:*:*
15-
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
16-
at runMainESM (internal/modules/run_main.js:*:*) {
15+
at initializeLoader (internal/process/esm_loader.js:*:*)
16+
at Object.loadESM (internal/process/esm_loader.js:*:*) {
1717
code: 'ERR_MODULE_NOT_FOUND'
1818
}

test/message/esm_loader_not_found_cjs_hint_bare.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
internal/modules/run_main.js:*
1+
internal/process/esm_loader.js:*
22
internalBinding('errors').triggerUncaughtException(
33
^
44

test/message/esm_loader_not_found_cjs_hint_relative.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
22
(Use `node --trace-warnings ...` to show where the warning was created)
3-
internal/modules/run_main.js:*
3+
internal/process/esm_loader.js:*
44
internalBinding('errors').triggerUncaughtException(
55
^
66

@@ -14,7 +14,7 @@ Did you mean to import ./test/common/fixtures.js?
1414
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
1515
at Loader.import (internal/modules/esm/loader.js:*:*)
1616
at internal/process/esm_loader.js:*:*
17-
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
18-
at runMainESM (internal/modules/run_main.js:*:*) {
17+
at initializeLoader (internal/process/esm_loader.js:*:*)
18+
at Object.loadESM (internal/process/esm_loader.js:*:*) {
1919
code: 'ERR_MODULE_NOT_FOUND'
2020
}

0 commit comments

Comments
 (0)