summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/compat/esm_resolver.rs28
-rw-r--r--cli/compat/mod.rs28
-rw-r--r--cli/compat/testdata/not_esm/main.js1
-rw-r--r--cli/compat/testdata/not_esm/node_modules/foo/index.js0
-rw-r--r--cli/compat/testdata/not_esm/node_modules/foo/package.json5
-rw-r--r--cli/compat/testdata/not_esm/package.json6
-rw-r--r--cli/main.rs6
7 files changed, 42 insertions, 32 deletions
diff --git a/cli/compat/esm_resolver.rs b/cli/compat/esm_resolver.rs
index f069428e5..0709c66a3 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<bool, AnyError> {
+ 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 997265f96..3797d7cb4 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<Url> {
}
}
-pub(crate) async fn check_if_should_use_esm_loader(
- js_runtime: &mut JsRuntime,
- main_module: &str,
-) -> Result<bool, AnyError> {
- // 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 000000000..347e20832
--- /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 000000000..e69de29bb
--- /dev/null
+++ b/cli/compat/testdata/not_esm/node_modules/foo/index.js
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 000000000..a74d52fd3
--- /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 000000000..4b03fc938
--- /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 67b59a443..d1ee4a012 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