Skip to content

Commit ee7cae2

Browse files
committed
repl: support --loader option in builtin REPL
Fixes: nodejs#33435 PR-URL: nodejs#33437 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 2f67e99 commit ee7cae2

File tree

8 files changed

+58
-46
lines changed

8 files changed

+58
-46
lines changed

lib/internal/main/repl.js

Lines changed: 26 additions & 23 deletions
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

Lines changed: 2 additions & 13 deletions
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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const {
55
} = require('internal/errors').codes;
66
const assert = require('internal/assert');
77
const { Loader } = require('internal/modules/esm/loader');
8+
const {
9+
hasUncaughtExceptionCaptureCallback,
10+
} = require('internal/process/execution');
811
const { pathToFileURL } = require('internal/url');
912
const {
1013
getModuleFromWrap,
@@ -45,7 +48,6 @@ let ESMLoader = new Loader();
4548
exports.ESMLoader = ESMLoader;
4649

4750
let calledInitialize = false;
48-
exports.initializeLoader = initializeLoader;
4951
async function initializeLoader() {
5052
assert(calledInitialize === false);
5153
process.emitWarning(
@@ -73,3 +75,19 @@ async function initializeLoader() {
7375
return exports.ESMLoader = ESMLoader;
7476
})();
7577
}
78+
79+
exports.loadESM = async function loadESM(callback) {
80+
try {
81+
await initializeLoader();
82+
await callback(ESMLoader);
83+
} catch (err) {
84+
if (hasUncaughtExceptionCaptureCallback()) {
85+
process._fatalException(err);
86+
return;
87+
}
88+
internalBinding('errors').triggerUncaughtException(
89+
err,
90+
true /* fromPromise */
91+
);
92+
}
93+
};

test/message/esm_display_syntax_error_import.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-ex
66
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
77
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
88
at async Loader.import (internal/modules/esm/loader.js:*:*)
9+
at async Object.loadESM (internal/process/esm_loader.js:*:*)

test/message/esm_display_syntax_error_import_module.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ SyntaxError: The requested module './module-named-exports.mjs' does not provide
66
at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*)
77
at async ModuleJob.run (internal/modules/esm/module_job.js:*:*)
88
at async Loader.import (internal/modules/esm/loader.js:*:*)
9+
at async Object.loadESM (internal/process/esm_loader.js:*:*)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
22
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
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 *
@@ -11,8 +11,8 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from *
1111
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
1212
at Loader.import (internal/modules/esm/loader.js:*:*)
1313
at internal/process/esm_loader.js:*:*
14-
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
15-
at runMainESM (internal/modules/run_main.js:*:*)
16-
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) {
14+
at initializeLoader (internal/process/esm_loader.js:*:*)
15+
at Object.loadESM (internal/process/esm_loader.js:*:*)
16+
at runMainESM (internal/modules/run_main.js:*:*) {
1717
code: 'ERR_MODULE_NOT_FOUND'
1818
}

test/message/esm_loader_not_found_cjs_hint_bare.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2-
internal/modules/run_main.js:*
2+
internal/process/esm_loader.js:*
33
internalBinding('errors').triggerUncaughtException(
44
^
55

test/message/esm_loader_not_found_cjs_hint_relative.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
22
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
3-
internal/modules/run_main.js:*
3+
internal/process/esm_loader.js:*
44
internalBinding('errors').triggerUncaughtException(
55
^
66

@@ -13,8 +13,8 @@ Did you mean to import ./test/common/fixtures.js?
1313
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
1414
at Loader.import (internal/modules/esm/loader.js:*:*)
1515
at internal/process/esm_loader.js:*:*
16-
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
17-
at runMainESM (internal/modules/run_main.js:*:*)
18-
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) {
16+
at initializeLoader (internal/process/esm_loader.js:*:*)
17+
at Object.loadESM (internal/process/esm_loader.js:*:*)
18+
at runMainESM (internal/modules/run_main.js:*:*) {
1919
code: 'ERR_MODULE_NOT_FOUND'
2020
}

0 commit comments

Comments
 (0)