summaryrefslogtreecommitdiff
path: root/cli/node.rs
diff options
context:
space:
mode:
authorsnek <snek@deno.com>2024-09-19 21:10:34 -0700
committerGitHub <noreply@github.com>2024-09-19 21:10:34 -0700
commita01dce3a25e0bf671c6c21bd6ff57861be613087 (patch)
treebecb8a7c90e5a21e83c81160eec9d91e1281bc92 /cli/node.rs
parentf1ba26661346a83b6e7fe5e7ffeed4553a9571ae (diff)
fix: cjs resolution cases (#25739)
Fixes cjs modules being loaded as esm.
Diffstat (limited to 'cli/node.rs')
-rw-r--r--cli/node.rs39
1 files changed, 36 insertions, 3 deletions
diff --git a/cli/node.rs b/cli/node.rs
index 766da1c01..a3cee8dde 100644
--- a/cli/node.rs
+++ b/cli/node.rs
@@ -16,6 +16,7 @@ use serde::Serialize;
use crate::cache::CacheDBHash;
use crate::cache::NodeAnalysisCache;
+use crate::resolver::CliNodeResolver;
use crate::util::fs::canonicalize_path_maybe_not_exists;
pub type CliNodeCodeTranslator =
@@ -54,11 +55,20 @@ pub enum CliCjsAnalysis {
pub struct CliCjsCodeAnalyzer {
cache: NodeAnalysisCache,
fs: deno_fs::FileSystemRc,
+ node_resolver: Arc<CliNodeResolver>,
}
impl CliCjsCodeAnalyzer {
- pub fn new(cache: NodeAnalysisCache, fs: deno_fs::FileSystemRc) -> Self {
- Self { cache, fs }
+ pub fn new(
+ cache: NodeAnalysisCache,
+ fs: deno_fs::FileSystemRc,
+ node_resolver: Arc<CliNodeResolver>,
+ ) -> Self {
+ Self {
+ cache,
+ fs,
+ node_resolver,
+ }
}
async fn inner_cjs_analysis(
@@ -73,7 +83,7 @@ impl CliCjsCodeAnalyzer {
return Ok(analysis);
}
- let media_type = MediaType::from_specifier(specifier);
+ let mut media_type = MediaType::from_specifier(specifier);
if media_type == MediaType::Json {
return Ok(CliCjsAnalysis::Cjs {
exports: vec![],
@@ -81,6 +91,22 @@ impl CliCjsCodeAnalyzer {
});
}
+ if media_type == MediaType::JavaScript {
+ if let Some(package_json) =
+ self.node_resolver.get_closest_package_json(specifier)?
+ {
+ match package_json.typ.as_str() {
+ "commonjs" => {
+ media_type = MediaType::Cjs;
+ }
+ "module" => {
+ media_type = MediaType::Mjs;
+ }
+ _ => {}
+ }
+ }
+ }
+
let analysis = deno_core::unsync::spawn_blocking({
let specifier = specifier.clone();
let source: Arc<str> = source.into();
@@ -99,6 +125,13 @@ impl CliCjsCodeAnalyzer {
exports: analysis.exports,
reexports: analysis.reexports,
})
+ } else if media_type == MediaType::Cjs {
+ // FIXME: `deno_ast` should internally handle MediaType::Cjs implying that
+ // the result must never be Esm
+ Ok(CliCjsAnalysis::Cjs {
+ exports: vec![],
+ reexports: vec![],
+ })
} else {
Ok(CliCjsAnalysis::Esm)
}