Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b7be751

Browse filesBrowse files
targosaddaleax
authored andcommitted
repl: support --loader option in builtin REPL
Fixes: #33435 PR-URL: #33437 Backport-PR-URL: #35394 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 1a8669d commit b7be751
Copy full SHA for b7be751

File tree

Expand file treeCollapse file tree

8 files changed

+65
-51
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+65
-51
lines changed
Open diff view settings
Collapse file

‎lib/internal/main/repl.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/repl.js
+27-24Lines changed: 27 additions & 24 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-
});
53-
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-
}
55+
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+
}, false);
6265
}
Collapse file

‎lib/internal/modules/run_main.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/run_main.js
+2-13Lines 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

Collapse file

‎lib/internal/process/esm_loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/esm_loader.js
+25-5Lines changed: 25 additions & 5 deletions
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,12 +48,13 @@ let ESMLoader = new Loader();
4548
exports.ESMLoader = ESMLoader;
4649

4750
let calledInitialize = false;
48-
exports.initializeLoader = initializeLoader;
49-
async function initializeLoader() {
51+
async function initializeLoader(emitWarning) {
5052
assert(calledInitialize === false);
51-
process.emitWarning(
52-
'The ESM module loader is experimental.',
53-
'ExperimentalWarning', undefined);
53+
if (emitWarning) {
54+
process.emitWarning(
55+
'The ESM module loader is experimental.',
56+
'ExperimentalWarning', undefined);
57+
}
5458
calledInitialize = true;
5559
if (!userLoader)
5660
return;
@@ -73,3 +77,19 @@ async function initializeLoader() {
7377
return exports.ESMLoader = ESMLoader;
7478
})();
7579
}
80+
81+
exports.loadESM = async function loadESM(callback, emitWarning = true) {
82+
try {
83+
await initializeLoader(emitWarning);
84+
await callback(ESMLoader);
85+
} catch (err) {
86+
if (hasUncaughtExceptionCaptureCallback()) {
87+
process._fatalException(err);
88+
return;
89+
}
90+
internalBinding('errors').triggerUncaughtException(
91+
err,
92+
true /* fromPromise */
93+
);
94+
}
95+
};
Collapse file

‎test/message/esm_display_syntax_error_import.out‎

Copy file name to clipboardExpand all lines: test/message/esm_display_syntax_error_import.out
+1Lines 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:*:*)
Collapse file

‎test/message/esm_display_syntax_error_import_module.out‎

Copy file name to clipboardExpand all lines: test/message/esm_display_syntax_error_import_module.out
+1Lines 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:*:*)
Collapse file
+4-4Lines 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
}
Collapse file

‎test/message/esm_loader_not_found_cjs_hint_bare.out‎

Copy file name to clipboardExpand all lines: test/message/esm_loader_not_found_cjs_hint_bare.out
+1-1Lines 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

Collapse file

‎test/message/esm_loader_not_found_cjs_hint_relative.out‎

Copy file name to clipboardExpand all lines: test/message/esm_loader_not_found_cjs_hint_relative.out
+4-4Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.