1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

feat(compat) preload Node.js built-in modules in global vars REPL (#13127)

This commit adds preloading of built-in Node.js modules in
the REPL if running with "deno repl --compat --unstable".
This commit is contained in:
VishnuJin 2022-01-04 00:40:17 +05:30 committed by GitHub
parent 340764adec
commit d9b130410b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -140,3 +140,18 @@ pub(crate) fn add_global_require(
fn escape_for_single_quote_string(text: &str) -> String {
text.replace(r"\", r"\\").replace("'", r"\'")
}
pub fn setup_builtin_modules(
js_runtime: &mut JsRuntime,
) -> Result<(), AnyError> {
let mut script = String::new();
for module in SUPPORTED_MODULES {
// skipping the modules that contains '/' as they are not available in NodeJS repl as well
if !module.contains('/') {
script = format!("{}const {} = require('{}');\n", script, module, module);
}
}
js_runtime.execute_script("setup_node_builtins.js", &script)?;
Ok(())
}

View file

@ -902,6 +902,8 @@ async fn repl_command(
if flags.compat {
worker.execute_side_module(&compat::GLOBAL_URL).await?;
compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?;
worker.run_event_loop(false).await?;
compat::setup_builtin_modules(&mut worker.js_runtime)?;
}
worker.run_event_loop(false).await?;

View file

@ -132,3 +132,15 @@ fn node_compat_url() {
assert!(!err.is_empty());
assert!(err.contains("file:///non_existent/node/global.ts"));
}
#[test]
fn native_modules_as_global_vars() {
let (out, _err) = util::run_and_collect_output_with_args(
true,
vec!["repl", "--compat", "--unstable", "--quiet"],
Some(vec!["if(cluster && v8 && sys) { true } else { false }"]),
None,
false,
);
assert!(out.contains("true"));
}