summaryrefslogtreecommitdiff
path: root/lib/repl.js
diff options
context:
space:
mode:
authorBradley Farias <bfarias@godaddy.com>2019-09-04 11:19:14 -0500
committerBradley Farias <bfarias@godaddy.com>2019-09-06 11:47:32 -0500
commitcdebd3246102be2a79546806161d121e5d3e3294 (patch)
tree76510b04d658449de4774c7cb848c6753ce87265 /lib/repl.js
parent63b056d8d4f0696254cd5fc40a69aee0157fc410 (diff)
downloadandroid-node-v8-cdebd3246102be2a79546806161d121e5d3e3294.tar.gz
android-node-v8-cdebd3246102be2a79546806161d121e5d3e3294.tar.bz2
android-node-v8-cdebd3246102be2a79546806161d121e5d3e3294.zip
esm: make dynamic import work in the REPL
PR-URL: https://github.com/nodejs/node/pull/29437 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: David Carlier <devnexen@gmail.com>
Diffstat (limited to 'lib/repl.js')
-rw-r--r--lib/repl.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/repl.js b/lib/repl.js
index d1b9a91df6..4dabe2110d 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -279,6 +279,12 @@ function REPLServer(prompt,
}
function defaultEval(code, context, file, cb) {
+ const { getOptionValue } = require('internal/options');
+ const experimentalModules = getOptionValue('--experimental-modules');
+ const asyncESM = experimentalModules ?
+ require('internal/process/esm_loader') :
+ null;
+
let result, script, wrappedErr;
let err = null;
let wrappedCmd = false;
@@ -312,6 +318,12 @@ function REPLServer(prompt,
if (code === '\n')
return cb(null);
+ let pwd;
+ try {
+ const { pathToFileURL } = require('url');
+ pwd = pathToFileURL(process.cwd()).href;
+ } catch {
+ }
while (true) {
try {
if (!/^\s*$/.test(code) &&
@@ -322,7 +334,12 @@ function REPLServer(prompt,
}
script = vm.createScript(code, {
filename: file,
- displayErrors: true
+ displayErrors: true,
+ importModuleDynamically: experimentalModules ?
+ async (specifier) => {
+ return (await asyncESM.loaderPromise).import(specifier, pwd);
+ } :
+ undefined
});
} catch (e) {
debug('parse error %j', code, e);