diff options
Diffstat (limited to 'ext/node/analyze.rs')
-rw-r--r-- | ext/node/analyze.rs | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/ext/node/analyze.rs b/ext/node/analyze.rs index 2a0324ccf..0a0226625 100644 --- a/ext/node/analyze.rs +++ b/ext/node/analyze.rs @@ -5,6 +5,7 @@ use std::collections::VecDeque; use std::path::Path; use std::path::PathBuf; +use deno_core::anyhow; use deno_core::anyhow::Context; use deno_core::ModuleSpecifier; use once_cell::sync::Lazy; @@ -21,7 +22,15 @@ use crate::PackageJson; use crate::PathClean; #[derive(Debug, Clone)] -pub struct CjsAnalysis { +pub enum CjsAnalysis { + /// File was found to be an ES module and the translator should + /// load the code as ESM. + Esm(String), + Cjs(CjsAnalysisExports), +} + +#[derive(Debug, Clone)] +pub struct CjsAnalysisExports { pub exports: Vec<String>, pub reexports: Vec<String>, } @@ -38,7 +47,7 @@ pub trait CjsCodeAnalyzer { fn analyze_cjs( &self, specifier: &ModuleSpecifier, - maybe_source: Option<&str>, + maybe_source: Option<String>, ) -> Result<CjsAnalysis, AnyError>; } @@ -73,7 +82,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> { pub fn translate_cjs_to_esm( &self, specifier: &ModuleSpecifier, - source: Option<&str>, + source: Option<String>, permissions: &dyn NodePermissions, ) -> Result<String, AnyError> { let mut temp_var_count = 0; @@ -81,6 +90,11 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> { let analysis = self.cjs_code_analyzer.analyze_cjs(specifier, source)?; + let analysis = match analysis { + CjsAnalysis::Esm(source) => return Ok(source), + CjsAnalysis::Cjs(analysis) => analysis, + }; + let mut source = vec![ r#"import {createRequire as __internalCreateRequire} from "node:module"; const require = __internalCreateRequire(import.meta.url);"# @@ -127,6 +141,17 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer> NodeCodeTranslator<TCjsCodeAnalyzer> { reexport, reexport_specifier, referrer ) })?; + let analysis = match analysis { + CjsAnalysis::Esm(_) => { + // todo(dsherret): support this once supporting requiring ES modules + return Err(anyhow::anyhow!( + "Cannot require ES module '{}' from '{}'", + reexport_specifier, + specifier + )); + } + CjsAnalysis::Cjs(analysis) => analysis, + }; for reexport in analysis.reexports { reexports_to_handle.push_back((reexport, reexport_specifier.clone())); |