diff options
Diffstat (limited to 'cli/node/mod.rs')
-rw-r--r-- | cli/node/mod.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 3ea263b04..2e431eaa0 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -5,6 +5,7 @@ use std::collections::VecDeque; use std::path::Path; use std::path::PathBuf; +use crate::cache::NodeAnalysisCache; use crate::deno_std::CURRENT_STD_URL; use deno_ast::CjsAnalysis; use deno_ast::MediaType; @@ -734,12 +735,20 @@ pub fn translate_cjs_to_esm( code: String, media_type: MediaType, npm_resolver: &NpmPackageResolver, + node_analysis_cache: &NodeAnalysisCache, ) -> Result<String, AnyError> { fn perform_cjs_analysis( + analysis_cache: &NodeAnalysisCache, specifier: &str, media_type: MediaType, code: String, ) -> Result<CjsAnalysis, AnyError> { + let source_hash = NodeAnalysisCache::compute_source_hash(&code); + if let Some(analysis) = + analysis_cache.get_cjs_analysis(specifier, &source_hash) + { + return Ok(analysis); + } let parsed_source = deno_ast::parse_script(deno_ast::ParseParams { specifier: specifier.to_string(), text_info: deno_ast::SourceTextInfo::new(code.into()), @@ -748,7 +757,10 @@ pub fn translate_cjs_to_esm( scope_analysis: false, maybe_syntax: None, })?; - Ok(parsed_source.analyze_cjs()) + let analysis = parsed_source.analyze_cjs(); + analysis_cache.set_cjs_analysis(specifier, &source_hash, &analysis); + + Ok(analysis) } let mut temp_var_count = 0; @@ -758,7 +770,12 @@ pub fn translate_cjs_to_esm( r#"const require = Deno[Deno.internal].require.Module.createRequire(import.meta.url);"#.to_string(), ]; - let analysis = perform_cjs_analysis(specifier.as_str(), media_type, code)?; + let analysis = perform_cjs_analysis( + node_analysis_cache, + specifier.as_str(), + media_type, + code, + )?; let mut all_exports = analysis .exports @@ -804,6 +821,7 @@ pub fn translate_cjs_to_esm( { let analysis = perform_cjs_analysis( + node_analysis_cache, reexport_specifier.as_str(), reexport_file.media_type, reexport_file.source.to_string(), |