2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-20 15:31:33 +00:00
|
|
|
|
2024-11-01 16:27:00 +00:00
|
|
|
use std::borrow::Cow;
|
2024-05-18 15:42:03 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-04-14 20:22:33 +00:00
|
|
|
use deno_ast::MediaType;
|
2022-08-20 15:31:33 +00:00
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
2024-10-15 00:48:39 +00:00
|
|
|
use deno_graph::ParsedSourceStore;
|
2023-07-24 19:35:13 +00:00
|
|
|
use deno_runtime::deno_fs;
|
2024-07-25 23:08:14 +00: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 18:35:51 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-08-20 15:31:33 +00:00
|
|
|
|
2024-05-29 18:38:18 +00:00
|
|
|
use crate::cache::CacheDBHash;
|
2022-10-01 10:15:56 +00:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2024-10-15 00:48:39 +00:00
|
|
|
use crate::cache::ParsedSourceCache;
|
2024-11-01 16:27:00 +00:00
|
|
|
use crate::resolver::CjsTracker;
|
2023-04-22 01:02:46 +00:00
|
|
|
|
2024-07-25 23:08:14 +00:00
|
|
|
pub type CliNodeCodeTranslator =
|
|
|
|
NodeCodeTranslator<CliCjsCodeAnalyzer, DenoFsNodeResolverEnv>;
|
2023-04-22 01:02:46 +00: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 16:27:00 +00:00
|
|
|
fs: &dyn deno_fs::FileSystem,
|
2023-04-22 01:02:46 +00:00
|
|
|
) -> ModuleSpecifier {
|
2024-11-14 20:24:25 +00:00
|
|
|
node_resolver::resolve_specifier_into_node_modules(specifier, &|path| {
|
|
|
|
fs.realpath_sync(path).map_err(|err| err.into_io_error())
|
|
|
|
})
|
2023-04-22 01:02:46 +00:00
|
|
|
}
|
2022-10-01 10:15:56 +00:00
|
|
|
|
2024-03-21 18:35:51 +00: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 08:30:04 +00:00
|
|
|
pub struct CliCjsCodeAnalyzer {
|
2023-04-21 20:38:10 +00:00
|
|
|
cache: NodeAnalysisCache,
|
2024-11-01 16:27:00 +00:00
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
2023-07-24 19:35:13 +00:00
|
|
|
fs: deno_fs::FileSystemRc,
|
2024-10-15 00:48:39 +00:00
|
|
|
parsed_source_cache: Option<Arc<ParsedSourceCache>>,
|
2023-04-14 20:22:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 08:30:04 +00:00
|
|
|
impl CliCjsCodeAnalyzer {
|
2024-09-20 04:10:34 +00:00
|
|
|
pub fn new(
|
|
|
|
cache: NodeAnalysisCache,
|
2024-11-01 16:27:00 +00:00
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
2024-09-20 04:10:34 +00:00
|
|
|
fs: deno_fs::FileSystemRc,
|
2024-10-15 00:48:39 +00:00
|
|
|
parsed_source_cache: Option<Arc<ParsedSourceCache>>,
|
2024-09-20 04:10:34 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
cache,
|
2024-11-01 16:27:00 +00:00
|
|
|
cjs_tracker,
|
2024-09-20 04:10:34 +00:00
|
|
|
fs,
|
2024-10-15 00:48:39 +00:00
|
|
|
parsed_source_cache,
|
2024-09-20 04:10:34 +00:00
|
|
|
}
|
2023-04-14 20:22:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 15:42:03 +00:00
|
|
|
async fn inner_cjs_analysis(
|
2023-04-14 20:22:33 +00:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-04-21 20:38:10 +00:00
|
|
|
source: &str,
|
2024-03-21 18:35:51 +00:00
|
|
|
) -> Result<CliCjsAnalysis, AnyError> {
|
2024-05-29 18:38:18 +00: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 20:22:33 +00:00
|
|
|
{
|
|
|
|
return Ok(analysis);
|
|
|
|
}
|
|
|
|
|
2024-11-01 16:27:00 +00:00
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
2023-04-14 20:22:33 +00:00
|
|
|
if media_type == MediaType::Json {
|
2024-03-21 18:35:51 +00:00
|
|
|
return Ok(CliCjsAnalysis::Cjs {
|
2023-04-14 20:22:33 +00:00
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-01 16:27:00 +00:00
|
|
|
let cjs_tracker = self.cjs_tracker.clone();
|
2024-11-13 15:10:09 +00:00
|
|
|
let is_maybe_cjs = cjs_tracker.is_maybe_cjs(specifier, media_type)?;
|
2024-11-01 16:27:00 +00: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 15:10:09 +00:00
|
|
|
)?;
|
2024-11-01 16:27:00 +00:00
|
|
|
if is_cjs {
|
|
|
|
let analysis = parsed_source.analyze_cjs();
|
|
|
|
Ok(CliCjsAnalysis::Cjs {
|
|
|
|
exports: analysis.exports,
|
|
|
|
reexports: analysis.reexports,
|
2024-10-15 00:48:39 +00:00
|
|
|
})
|
2024-11-01 16:27:00 +00:00
|
|
|
} else {
|
|
|
|
Ok(CliCjsAnalysis::Esm)
|
|
|
|
}
|
2024-05-18 15:42:03 +00:00
|
|
|
}
|
2024-11-01 16:27:00 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()?
|
|
|
|
} else {
|
|
|
|
CliCjsAnalysis::Esm
|
|
|
|
};
|
2024-05-18 15:42:03 +00:00
|
|
|
|
2023-04-14 20:22:33 +00:00
|
|
|
self
|
2023-04-21 20:38:10 +00:00
|
|
|
.cache
|
2024-05-29 18:38:18 +00:00
|
|
|
.set_cjs_analysis(specifier.as_str(), source_hash, &analysis);
|
2023-04-14 20:22:33 +00:00
|
|
|
|
|
|
|
Ok(analysis)
|
|
|
|
}
|
2023-04-21 20:38:10 +00:00
|
|
|
}
|
2023-04-14 20:22:33 +00:00
|
|
|
|
2024-05-18 15:42:03 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-07-19 08:30:04 +00:00
|
|
|
impl CjsCodeAnalyzer for CliCjsCodeAnalyzer {
|
2024-11-01 16:27:00 +00:00
|
|
|
async fn analyze_cjs<'a>(
|
2023-04-14 20:22:33 +00:00
|
|
|
&self,
|
2023-04-21 20:38:10 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-11-01 16:27:00 +00:00
|
|
|
source: Option<Cow<'a, str>>,
|
|
|
|
) -> Result<ExtNodeCjsAnalysis<'a>, AnyError> {
|
2023-07-24 19:35:13 +00:00
|
|
|
let source = match source {
|
2024-03-21 18:35:51 +00:00
|
|
|
Some(source) => source,
|
2024-05-18 15:42:03 +00:00
|
|
|
None => {
|
2024-09-20 01:37:36 +00: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 16:27:00 +00:00
|
|
|
Cow::Owned(source_from_file)
|
2024-09-20 01:37:36 +00:00
|
|
|
} else {
|
|
|
|
return Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports: vec![],
|
|
|
|
reexports: vec![],
|
|
|
|
}));
|
|
|
|
}
|
2024-05-18 15:42:03 +00:00
|
|
|
}
|
2023-07-24 19:35:13 +00:00
|
|
|
};
|
2024-05-18 15:42:03 +00:00
|
|
|
let analysis = self.inner_cjs_analysis(specifier, &source).await?;
|
2024-03-21 18:35:51 +00:00
|
|
|
match analysis {
|
|
|
|
CliCjsAnalysis::Esm => Ok(ExtNodeCjsAnalysis::Esm(source)),
|
|
|
|
CliCjsAnalysis::Cjs { exports, reexports } => {
|
|
|
|
Ok(ExtNodeCjsAnalysis::Cjs(CjsAnalysisExports {
|
|
|
|
exports,
|
|
|
|
reexports,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 20:22:33 +00:00
|
|
|
}
|
2022-08-20 15:31:33 +00:00
|
|
|
}
|