summaryrefslogtreecommitdiff
path: root/cli/compat/mod.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-11-24 16:55:10 +0100
committerGitHub <noreply@github.com>2021-11-24 16:55:10 +0100
commit1117d2db3965fc44f174be3fd029293b7e2b952e (patch)
tree3a50bffc376d1d86cfa09e726e500560f9f3c67c /cli/compat/mod.rs
parent1a51f2392db731fe0c1aa0ccd00da6769d254e66 (diff)
compat: support compat mode in REPL (#12882)
This commit introduces "ProcState::maybe_resolver" field, which stores a single instance of resolver for the whole lifetime of the process, instead of creating these resolvers for each creation of module graph. As a result, this resolver can be used in fallback case where graph is not constructed (REPL, loading modules using "require") unifying resolution logic.
Diffstat (limited to 'cli/compat/mod.rs')
-rw-r--r--cli/compat/mod.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/cli/compat/mod.rs b/cli/compat/mod.rs
index 01d84d65a..41443968f 100644
--- a/cli/compat/mod.rs
+++ b/cli/compat/mod.rs
@@ -104,6 +104,24 @@ pub(crate) fn load_cjs_module(
Ok(())
}
+pub(crate) fn add_global_require(
+ js_runtime: &mut JsRuntime,
+ main_module: &str,
+) -> Result<(), AnyError> {
+ let source_code = &format!(
+ r#"(async function setupGlobalRequire(main) {{
+ const Module = await import("{}");
+ const require = Module.createRequire(main);
+ globalThis.require = require;
+ }})('{}');"#,
+ MODULE_URL_STR.as_str(),
+ escape_for_single_quote_string(main_module),
+ );
+
+ js_runtime.execute_script(&located_script_name!(), source_code)?;
+ Ok(())
+}
+
fn escape_for_single_quote_string(text: &str) -> String {
text.replace(r"\", r"\\").replace("'", r"\'")
}