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
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
use std::borrow::Cow;
|
2024-05-18 11:42:03 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_ast::MediaType;
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
2024-10-14 20:48:39 -04:00
|
|
|
use deno_graph::ParsedSourceStore;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_path_util::url_from_file_path;
|
|
|
|
use deno_path_util::url_to_file_path;
|
2023-07-24 15:35:13 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2024-07-25 19:08:14 -04:00
|
|
|
use deno_runtime::deno_node::DenoFsNodeResolverEnv;
|
|
|
|
use node_resolver::analyze::CjsAnalysis as ExtNodeCjsAnalysis;
|
|
|
|
use node_resolver::analyze::CjsAnalysisExports;
|
|
|
|
use node_resolver::analyze::CjsCodeAnalyzer;
|
|
|
|
use node_resolver::analyze::NodeCodeTranslator;
|
2024-03-21 14:35:51 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2024-05-29 14:38:18 -04:00
|
|
|
use crate::cache::CacheDBHash;
|
2022-10-01 06:15:56 -04:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2024-10-14 20:48:39 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::resolver::CjsTracker;
|
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
2023-04-21 21:02:46 -04:00
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
pub type CliNodeCodeTranslator =
|
|
|
|
NodeCodeTranslator<CliCjsCodeAnalyzer, DenoFsNodeResolverEnv>;
|
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,
|
2024-11-01 12:27:00 -04:00
|
|
|
fs: &dyn deno_fs::FileSystem,
|
2023-04-21 21:02:46 -04:00
|
|
|
) -> ModuleSpecifier {
|
2024-11-01 12:27:00 -04:00
|
|
|
url_to_file_path(specifier)
|
2023-04-21 21:02:46 -04:00
|
|
|
.ok()
|
|
|
|
// this path might not exist at the time the graph is being created
|
|
|
|
// because the node_modules folder might not yet exist
|
2024-11-01 12:27:00 -04:00
|
|
|
.and_then(|path| canonicalize_path_maybe_not_exists_with_fs(&path, fs).ok())
|
|
|
|
.and_then(|path| url_from_file_path(&path).ok())
|
2023-04-21 21:02:46 -04:00
|
|
|
.unwrap_or_else(|| specifier.clone())
|
|
|
|
}
|
2022-10-01 06:15:56 -04:00
|
|
|
|
2024-03-21 14:35:51 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum CliCjsAnalysis {
|
|
|
|
/// The module was found to be an ES module.
|
|
|
|
Esm,
|
|
|
|
/// The module was CJS.
|
|
|
|
Cjs {
|
|
|
|
exports: Vec<String>,
|
|
|
|
reexports: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
pub struct CliCjsCodeAnalyzer {
|
2023-04-21 16:38:10 -04:00
|
|
|
cache: NodeAnalysisCache,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
2023-07-24 15:35:13 -04:00
|
|
|
fs: deno_fs::FileSystemRc,
|
2024-10-14 20:48:39 -04:00
|
|
|
parsed_source_cache: Option<Arc<ParsedSourceCache>>,
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
impl CliCjsCodeAnalyzer {
|
2024-09-20 00:10:34 -04:00
|
|
|
pub fn new(
|
|
|
|
cache: NodeAnalysisCache,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
2024-09-20 00:10:34 -04:00
|
|
|
fs: deno_fs::FileSystemRc,
|
2024-10-14 20:48:39 -04:00
|
|
|
parsed_source_cache: Option<Arc<ParsedSourceCache>>,
|
2024-09-20 00:10:34 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
cache,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker,
|
2024-09-20 00:10:34 -04:00
|
|
|
fs,
|
2024-10-14 20:48:39 -04:00
|
|
|
parsed_source_cache,
|
2024-09-20 00:10:34 -04:00
|
|
|
}
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
async 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,
|
2024-03-21 14:35:51 -04:00
|
|
|
) -> Result<CliCjsAnalysis, AnyError> {
|
2024-05-29 14:38:18 -04:00
|
|
|
let source_hash = CacheDBHash::from_source(source);
|
|
|
|
if let Some(analysis) =
|
|
|
|
self.cache.get_cjs_analysis(specifier.as_str(), source_hash)
|
2023-04-14 16:22:33 -04:00
|
|
|
{
|
|
|
|
return Ok(analysis);
|
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
2023-04-14 16:22:33 -04:00
|
|
|
if media_type == MediaType::Json {
|
2024-03-21 14:35:51 -04:00
|
|
|
return Ok(CliCjsAnalysis::Cjs {
|
2023-04-14 16:22:33 -04:00
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
let cjs_tracker = self.cjs_tracker.clone();
|
2024-11-13 10:10:09 -05:00
|
|
|
let is_maybe_cjs = cjs_tracker.is_maybe_cjs(specifier, media_type)?;
|
2024-11-01 12:27:00 -04:00
|
|
|
let analysis = if is_maybe_cjs {
|
|
|
|
let maybe_parsed_source = self
|
|
|
|
.parsed_source_cache
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|c| c.remove_parsed_source(specifier));
|
|
|
|
|
|
|
|
deno_core::unsync::spawn_blocking({
|
|
|
|
let specifier = specifier.clone();
|
|
|
|
let source: Arc<str> = source.into();
|
|
|
|
move || -> Result<_, AnyError> {
|
|
|
|
let parsed_source =
|
|
|
|
maybe_parsed_source.map(Ok).unwrap_or_else(|| {
|
|
|
|
deno_ast::parse_program(deno_ast::ParseParams {
|
|
|
|
specifier,
|
|
|
|
text: source,
|
|
|
|
media_type,
|
|
|
|
capture_tokens: true,
|
|
|
|
scope_analysis: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
let is_script = parsed_source.compute_is_script();
|
|
|
|
let is_cjs = cjs_tracker.is_cjs_with_known_is_script(
|
|
|
|
parsed_source.specifier(),
|
|
|
|
media_type,
|
|
|
|
is_script,
|
2024-11-13 10:10:09 -05:00
|
|
|
)?;
|
2024-11-01 12:27:00 -04:00
|
|
|
if is_cjs {
|
|
|
|
let analysis = parsed_source.analyze_cjs();
|
|
|
|
Ok(CliCjsAnalysis::Cjs {
|
|
|
|
exports: analysis.exports,
|
|
|
|
reexports: analysis.reexports,
|
2024-10-14 20:48:39 -04:00
|
|
|
})
|
2024-11-01 12:27:00 -04:00
|
|
|
} else {
|
|
|
|
Ok(CliCjsAnalysis::Esm)
|
|
|
|
}
|
2024-05-18 11:42:03 -04:00
|
|
|
}
|
2024-11-01 12:27:00 -04:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()?
|
|
|
|
} else {
|
|
|
|
CliCjsAnalysis::Esm
|
|
|
|
};
|
2024-05-18 11:42:03 -04:00
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
self
|
2023-04-21 16:38:10 -04:00
|
|
|
.cache
|
2024-05-29 14:38:18 -04:00
|
|
|
.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
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-07-19 04:30:04 -04:00
|
|
|
impl CjsCodeAnalyzer for CliCjsCodeAnalyzer {
|
2024-11-01 12:27:00 -04:00
|
|
|
async fn analyze_cjs<'a>(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self,
|
2023-04-21 16:38:10 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-11-01 12:27:00 -04:00
|
|
|
source: Option<Cow<'a, str>>,
|
|
|
|
) -> Result<ExtNodeCjsAnalysis<'a>, AnyError> {
|
2023-07-24 15:35:13 -04:00
|
|
|
let source = match source {
|
2024-03-21 14:35:51 -04:00
|
|
|
Some(source) => source,
|
2024-05-18 11:42:03 -04:00
|
|
|
None => {
|
2024-09-19 21:37:36 -04:00
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
|
|
|
if let Ok(source_from_file) =
|
|
|
|
self.fs.read_text_file_lossy_async(path, None).await
|
|
|
|
{
|
2024-11-01 12:27:00 -04:00
|
|
|
Cow::Owned(source_from_file)
|
2024-09-19 21:37:36 -04:00
|
|
|
} else {
|
|
|
|
return Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
}));
|
|
|
|
}
|
2024-05-18 11:42:03 -04:00
|
|
|
}
|
2023-07-24 15:35:13 -04:00
|
|
|
};
|
2024-05-18 11:42:03 -04:00
|
|
|
let analysis = self.inner_cjs_analysis(specifier, &source).await?;
|
2024-03-21 14:35:51 -04:00
|
|
|
match analysis {
|
|
|
|
CliCjsAnalysis::Esm => Ok(ExtNodeCjsAnalysis::Esm(source)),
|
|
|
|
CliCjsAnalysis::Cjs { exports, reexports } => {
|
|
|
|
Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports,
|
|
|
|
reexports,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|