From b92019a8475a16213028ab73b15be5a3f7c56e0a Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 1 Nov 2021 14:46:07 -0400 Subject: [PATCH] port check_if_should_use_esm_loader to rust (#12562) --- cli/compat/esm_resolver.rs | 28 +++++++++++++++++++ cli/compat/mod.rs | 28 +------------------ cli/compat/testdata/not_esm/main.js | 1 + .../not_esm/node_modules/foo/index.js | 0 .../not_esm/node_modules/foo/package.json | 5 ++++ cli/compat/testdata/not_esm/package.json | 6 ++++ cli/main.rs | 6 +--- 7 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 cli/compat/testdata/not_esm/main.js create mode 100644 cli/compat/testdata/not_esm/node_modules/foo/index.js create mode 100644 cli/compat/testdata/not_esm/node_modules/foo/package.json create mode 100644 cli/compat/testdata/not_esm/package.json diff --git a/cli/compat/esm_resolver.rs b/cli/compat/esm_resolver.rs index f069428e58..0709c66a3b 100644 --- a/cli/compat/esm_resolver.rs +++ b/cli/compat/esm_resolver.rs @@ -926,6 +926,20 @@ struct PackageConfig { typ: String, } +pub fn check_if_should_use_esm_loader( + main_module: &ModuleSpecifier, +) -> Result { + let s = main_module.as_str(); + if s.ends_with(".mjs") { + return Ok(true); + } + if s.ends_with(".cjs") { + return Ok(false); + } + let package_config = get_package_scope_config(main_module)?; + Ok(package_config.typ == "module") +} + fn get_package_config( path: PathBuf, specifier: &str, @@ -1219,4 +1233,18 @@ mod tests { assert!(is_relative_specifier("./foo.js")); assert!(!is_relative_specifier("https://deno.land/std/node/http.ts")); } + + #[test] + fn test_check_if_should_use_esm_loader() { + let basic = testdir("basic"); + let main = Url::from_file_path(basic.join("main.js")).unwrap(); + assert!(check_if_should_use_esm_loader(&main).unwrap()); + + let cjs = Url::from_file_path(basic.join("main.cjs")).unwrap(); + assert!(!check_if_should_use_esm_loader(&cjs).unwrap()); + + let not_esm = testdir("not_esm"); + let main = Url::from_file_path(not_esm.join("main.js")).unwrap(); + assert!(!check_if_should_use_esm_loader(&main).unwrap()); + } } diff --git a/cli/compat/mod.rs b/cli/compat/mod.rs index 997265f96c..3797d7cb4f 100644 --- a/cli/compat/mod.rs +++ b/cli/compat/mod.rs @@ -8,6 +8,7 @@ use deno_core::located_script_name; use deno_core::url::Url; use deno_core::JsRuntime; +pub use esm_resolver::check_if_should_use_esm_loader; pub(crate) use esm_resolver::NodeEsmResolver; // TODO(bartlomieju): this needs to be bumped manually for @@ -86,33 +87,6 @@ fn try_resolve_builtin_module(specifier: &str) -> Option { } } -pub(crate) async fn check_if_should_use_esm_loader( - js_runtime: &mut JsRuntime, - main_module: &str, -) -> Result { - // Decide if we're running with Node ESM loader or CJS loader. - let source_code = &format!( - r#"(async function checkIfEsm(main) {{ - const {{ resolveMainPath, shouldUseESMLoader }} = await import("{}"); - const resolvedMain = resolveMainPath(main); - const useESMLoader = shouldUseESMLoader(resolvedMain); - return useESMLoader; - }})('{}');"#, - MODULE_URL_STR.as_str(), - escape_for_single_quote_string(main_module), - ); - let result = - js_runtime.execute_script(&located_script_name!(), source_code)?; - let use_esm_loader_global = js_runtime.resolve_value(result).await?; - let use_esm_loader = { - let scope = &mut js_runtime.handle_scope(); - let use_esm_loader_local = use_esm_loader_global.open(scope); - use_esm_loader_local.boolean_value(scope) - }; - - Ok(use_esm_loader) -} - pub(crate) fn load_cjs_module( js_runtime: &mut JsRuntime, main_module: &str, diff --git a/cli/compat/testdata/not_esm/main.js b/cli/compat/testdata/not_esm/main.js new file mode 100644 index 0000000000..347e208321 --- /dev/null +++ b/cli/compat/testdata/not_esm/main.js @@ -0,0 +1 @@ +require("foo"); diff --git a/cli/compat/testdata/not_esm/node_modules/foo/index.js b/cli/compat/testdata/not_esm/node_modules/foo/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cli/compat/testdata/not_esm/node_modules/foo/package.json b/cli/compat/testdata/not_esm/node_modules/foo/package.json new file mode 100644 index 0000000000..a74d52fd35 --- /dev/null +++ b/cli/compat/testdata/not_esm/node_modules/foo/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "type": "module", + "exports": "./index.js" +} diff --git a/cli/compat/testdata/not_esm/package.json b/cli/compat/testdata/not_esm/package.json new file mode 100644 index 0000000000..4b03fc9384 --- /dev/null +++ b/cli/compat/testdata/not_esm/package.json @@ -0,0 +1,6 @@ +{ + "name": "bar", + "dependencies": { + "foo": "1.0.0" + } +} diff --git a/cli/main.rs b/cli/main.rs index 67b59a4434..d1ee4a0127 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -1153,11 +1153,7 @@ async fn run_command( // this file. worker.execute_side_module(&compat::MODULE_URL).await?; - let use_esm_loader = compat::check_if_should_use_esm_loader( - &mut worker.js_runtime, - &main_module.to_file_path().unwrap().display().to_string(), - ) - .await?; + let use_esm_loader = compat::check_if_should_use_esm_loader(&main_module)?; if use_esm_loader { // ES module execution in Node compatiblity mode