2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-07-24 15:35:13 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_ast::CjsAnalysis;
|
|
|
|
use deno_ast::MediaType;
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
2023-07-24 15:35:13 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2023-04-21 16:38:10 -04:00
|
|
|
use deno_runtime::deno_node::analyze::CjsAnalysis as ExtNodeCjsAnalysis;
|
2023-07-19 04:30:04 -04:00
|
|
|
use deno_runtime::deno_node::analyze::CjsCodeAnalyzer;
|
2023-04-21 21:02:46 -04:00
|
|
|
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2022-10-01 06:15:56 -04:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2023-04-21 21:02:46 -04:00
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists;
|
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
pub type CliNodeCodeTranslator = NodeCodeTranslator<CliCjsCodeAnalyzer>;
|
2023-04-21 21:02:46 -04:00
|
|
|
|
|
|
|
/// Resolves a specifier that is pointing into a node_modules folder.
|
|
|
|
///
|
|
|
|
/// Note: This should be called whenever getting the specifier from
|
|
|
|
/// a Module::External(module) reference because that module might
|
|
|
|
/// not be fully resolved at the time deno_graph is analyzing it
|
|
|
|
/// because the node_modules folder might not exist at that time.
|
|
|
|
pub fn resolve_specifier_into_node_modules(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> ModuleSpecifier {
|
|
|
|
specifier
|
|
|
|
.to_file_path()
|
|
|
|
.ok()
|
|
|
|
// this path might not exist at the time the graph is being created
|
|
|
|
// because the node_modules folder might not yet exist
|
|
|
|
.and_then(|path| canonicalize_path_maybe_not_exists(&path).ok())
|
|
|
|
.and_then(|path| ModuleSpecifier::from_file_path(path).ok())
|
|
|
|
.unwrap_or_else(|| specifier.clone())
|
|
|
|
}
|
2022-10-01 06:15:56 -04:00
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
pub struct CliCjsCodeAnalyzer {
|
2023-04-21 16:38:10 -04:00
|
|
|
cache: NodeAnalysisCache,
|
2023-07-24 15:35:13 -04:00
|
|
|
fs: deno_fs::FileSystemRc,
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
impl CliCjsCodeAnalyzer {
|
2023-07-24 15:35:13 -04:00
|
|
|
pub fn new(cache: NodeAnalysisCache, fs: deno_fs::FileSystemRc) -> Self {
|
|
|
|
Self { cache, fs }
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:38:10 -04:00
|
|
|
fn inner_cjs_analysis(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-04-21 16:38:10 -04:00
|
|
|
source: &str,
|
2023-04-14 16:22:33 -04:00
|
|
|
) -> Result<CjsAnalysis, AnyError> {
|
2023-04-21 16:38:10 -04:00
|
|
|
let source_hash = NodeAnalysisCache::compute_source_hash(source);
|
2023-04-14 16:22:33 -04:00
|
|
|
if let Some(analysis) = self
|
2023-04-21 16:38:10 -04:00
|
|
|
.cache
|
|
|
|
.get_cjs_analysis(specifier.as_str(), &source_hash)
|
2023-04-14 16:22:33 -04:00
|
|
|
{
|
|
|
|
return Ok(analysis);
|
|
|
|
}
|
|
|
|
|
2023-04-21 16:38:10 -04:00
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
2023-04-14 16:22:33 -04:00
|
|
|
if media_type == MediaType::Json {
|
|
|
|
return Ok(CjsAnalysis {
|
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
|
|
|
|
specifier: specifier.to_string(),
|
2023-04-21 16:38:10 -04:00
|
|
|
text_info: deno_ast::SourceTextInfo::new(source.into()),
|
2023-04-14 16:22:33 -04:00
|
|
|
media_type,
|
|
|
|
capture_tokens: true,
|
|
|
|
scope_analysis: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})?;
|
|
|
|
let analysis = parsed_source.analyze_cjs();
|
|
|
|
self
|
2023-04-21 16:38:10 -04:00
|
|
|
.cache
|
|
|
|
.set_cjs_analysis(specifier.as_str(), &source_hash, &analysis);
|
2023-04-14 16:22:33 -04:00
|
|
|
|
|
|
|
Ok(analysis)
|
|
|
|
}
|
2023-04-21 16:38:10 -04:00
|
|
|
}
|
2023-04-14 16:22:33 -04:00
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
impl CjsCodeAnalyzer for CliCjsCodeAnalyzer {
|
2023-04-21 16:38:10 -04:00
|
|
|
fn analyze_cjs(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self,
|
2023-04-21 16:38:10 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-24 15:35:13 -04:00
|
|
|
source: Option<&str>,
|
2023-04-21 16:38:10 -04:00
|
|
|
) -> Result<ExtNodeCjsAnalysis, AnyError> {
|
2023-07-24 15:35:13 -04:00
|
|
|
let source = match source {
|
|
|
|
Some(source) => Cow::Borrowed(source),
|
2023-08-08 16:28:18 -04:00
|
|
|
None => Cow::Owned(
|
|
|
|
self
|
|
|
|
.fs
|
|
|
|
.read_text_file_sync(&specifier.to_file_path().unwrap())?,
|
|
|
|
),
|
2023-07-24 15:35:13 -04:00
|
|
|
};
|
|
|
|
let analysis = self.inner_cjs_analysis(specifier, &source)?;
|
2023-04-21 16:38:10 -04:00
|
|
|
Ok(ExtNodeCjsAnalysis {
|
|
|
|
exports: analysis.exports,
|
|
|
|
reexports: analysis.reexports,
|
|
|
|
})
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|