summaryrefslogtreecommitdiff
path: root/ext/node/analyze.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-05-18 11:42:03 -0400
committerGitHub <noreply@github.com>2024-05-18 11:42:03 -0400
commita2dbcf9e0aa1f70056ccc6ecdd4c813d30af768c (patch)
treeae0c11a160b1203209b5a54d5dbb693d7dbc5135 /ext/node/analyze.rs
parentfcb6a18b2b9fe23f3c19f6039d8f270a3a1999d6 (diff)
perf: analyze cjs exports and emit typescript in parallel (#23856)
Diffstat (limited to 'ext/node/analyze.rs')
-rw-r--r--ext/node/analyze.rs12
1 files changed, 9 insertions, 3 deletions
diff --git a/ext/node/analyze.rs b/ext/node/analyze.rs
index ad38a511b..2d1c169fc 100644
--- a/ext/node/analyze.rs
+++ b/ext/node/analyze.rs
@@ -36,6 +36,7 @@ pub struct CjsAnalysisExports {
}
/// Code analyzer for CJS and ESM files.
+#[async_trait::async_trait(?Send)]
pub trait CjsCodeAnalyzer {
/// Analyzes CommonJs code for exports and reexports, which is
/// then used to determine the wrapper ESM module exports.
@@ -44,7 +45,7 @@ pub trait CjsCodeAnalyzer {
/// already has it. If the source is needed by the implementation,
/// then it can use the provided source, or otherwise load it if
/// necessary.
- fn analyze_cjs(
+ async fn analyze_cjs(
&self,
specifier: &ModuleSpecifier,
maybe_source: Option<String>,
@@ -79,7 +80,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> {
/// For all discovered reexports the analysis will be performed recursively.
///
/// If successful a source code for equivalent ES module is returned.
- pub fn translate_cjs_to_esm(
+ pub async fn translate_cjs_to_esm(
&self,
specifier: &ModuleSpecifier,
source: Option<String>,
@@ -88,7 +89,10 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> {
let mut temp_var_count = 0;
let mut handled_reexports: HashSet<ModuleSpecifier> = HashSet::default();
- let analysis = self.cjs_code_analyzer.analyze_cjs(specifier, source)?;
+ let analysis = self
+ .cjs_code_analyzer
+ .analyze_cjs(specifier, source)
+ .await?;
let analysis = match analysis {
CjsAnalysis::Esm(source) => return Ok(source),
@@ -113,6 +117,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> {
reexports_to_handle.push_back((reexport, specifier.clone()));
}
+ // todo(dsherret): we could run this analysis concurrently in a FuturesOrdered
while let Some((reexport, referrer)) = reexports_to_handle.pop_front() {
// First, resolve the reexport specifier
let reexport_specifier = self.resolve(
@@ -133,6 +138,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> {
let analysis = self
.cjs_code_analyzer
.analyze_cjs(&reexport_specifier, None)
+ .await
.with_context(|| {
format!(
"Could not load '{}' ({}) referenced from {}",