-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ensure esm imports exists when mode is production (#1709)
* feat: ensure esm imports exists when mode is production
- Loading branch information
1 parent
a89b63d
commit ea532ba
Showing
15 changed files
with
333 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
mod collect_exports; | ||
mod collect_imports; | ||
|
||
use std::collections::{HashMap, HashSet}; | ||
use std::sync::{Arc, RwLockReadGuard}; | ||
|
||
use anyhow::Result; | ||
use collect_exports::CollectExports; | ||
use collect_imports::CollectImports; | ||
use swc_core::ecma::visit::VisitWith; | ||
use tracing::error; | ||
|
||
use crate::compiler::{Compiler, Context}; | ||
use crate::module::{ModuleId, ModuleSystem}; | ||
use crate::module_graph::ModuleGraph; | ||
use crate::plugin::Plugin; | ||
|
||
pub struct ImportsChecker {} | ||
|
||
fn pick_no_export_specifiers_with_imports_info( | ||
module_id: &ModuleId, | ||
module_graph: &RwLockReadGuard<ModuleGraph>, | ||
specifiers: &mut HashSet<String>, | ||
) { | ||
if !specifiers.is_empty() { | ||
let dep_module = module_graph.get_module(module_id).unwrap(); | ||
if let Some(info) = &dep_module.info { | ||
match info.module_system { | ||
ModuleSystem::ESModule => { | ||
let mut exports_star_sources: Vec<String> = vec![]; | ||
let ast = &info.ast.as_script().unwrap().ast; | ||
ast.visit_with(&mut CollectExports { | ||
specifiers, | ||
exports_star_sources: &mut exports_star_sources, | ||
}); | ||
exports_star_sources.into_iter().for_each(|source| { | ||
if let Some(id) = | ||
module_graph.get_dependency_module_by_source(module_id, &source) | ||
{ | ||
pick_no_export_specifiers_with_imports_info( | ||
id, | ||
module_graph, | ||
specifiers, | ||
); | ||
} | ||
}) | ||
} | ||
ModuleSystem::CommonJS | ModuleSystem::Custom => { | ||
specifiers.clear(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
impl Plugin for ImportsChecker { | ||
fn name(&self) -> &str { | ||
"imports_checker" | ||
} | ||
fn after_build(&self, context: &Arc<Context>, _compiler: &Compiler) -> Result<()> { | ||
let mut modules_imports_map: HashMap<&ModuleId, HashMap<String, HashSet<String>>> = | ||
HashMap::new(); | ||
|
||
let module_graph = context.module_graph.read().unwrap(); | ||
let modules = module_graph.modules(); | ||
|
||
for m in modules { | ||
if let Some(info) = &m.info { | ||
if !info.file.is_under_node_modules | ||
&& matches!(info.module_system, ModuleSystem::ESModule) | ||
{ | ||
// 收集 imports | ||
let ast = &info.ast.as_script().unwrap().ast; | ||
let mut import_specifiers: HashMap<String, HashSet<String>> = HashMap::new(); | ||
|
||
ast.visit_with(&mut CollectImports { | ||
imports_specifiers_with_source: &mut import_specifiers, | ||
}); | ||
modules_imports_map.insert(&m.id, import_specifiers); | ||
} | ||
} | ||
} | ||
// 收集 exports | ||
modules_imports_map | ||
.iter_mut() | ||
.for_each(|(module_id, import_specifiers)| { | ||
import_specifiers | ||
.iter_mut() | ||
.for_each(|(source, specifiers)| { | ||
if let Some(dep_module_id) = | ||
module_graph.get_dependency_module_by_source(module_id, source) | ||
{ | ||
pick_no_export_specifiers_with_imports_info( | ||
dep_module_id, | ||
&module_graph, | ||
specifiers, | ||
); | ||
} | ||
}) | ||
}); | ||
let mut should_panic = false; | ||
modules_imports_map | ||
.into_iter() | ||
.for_each(|(module_id, import_specifiers)| { | ||
import_specifiers | ||
.into_iter() | ||
.filter(|(_, specifiers)| !specifiers.is_empty()) | ||
.for_each(|(source, specifiers)| { | ||
should_panic = true; | ||
specifiers.iter().for_each(|specifier| { | ||
error!( | ||
"'{}' is undefined: import from '{}' in '{}'", | ||
specifier, source, module_id.id | ||
); | ||
}) | ||
}); | ||
}); | ||
if should_panic { | ||
panic!("dependency check error!"); | ||
}; | ||
Ok(()) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
crates/mako/src/plugins/imports_checker/collect_exports.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::collections::HashSet; | ||
|
||
use swc_core::ecma::ast::*; | ||
use swc_core::ecma::visit::Visit; | ||
|
||
pub struct CollectExports<'a> { | ||
pub specifiers: &'a mut HashSet<String>, | ||
pub exports_star_sources: &'a mut Vec<String>, | ||
} | ||
|
||
impl<'a> Visit for CollectExports<'a> { | ||
fn visit_module_decl(&mut self, node: &ModuleDecl) { | ||
match &node { | ||
// export const a = 1 | ||
ModuleDecl::ExportDecl(ExportDecl { decl, .. }) => match decl { | ||
Decl::Fn(FnDecl { ident, .. }) => { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} | ||
Decl::Class(ClassDecl { ident, .. }) => { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} | ||
Decl::Var(box VarDecl { decls, .. }) => decls.iter().for_each(|decl| { | ||
if let Pat::Ident(ident) = &decl.name { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} | ||
}), | ||
_ => {} | ||
}, | ||
// export default function | ||
ModuleDecl::ExportDefaultDecl(_) => { | ||
self.specifiers.remove(&"default".to_string()); | ||
} | ||
// export default 1 | ||
ModuleDecl::ExportDefaultExpr(_) => { | ||
self.specifiers.remove(&"default".to_string()); | ||
} | ||
// export * from 'b' | ||
ModuleDecl::ExportAll(all) => { | ||
let source = all.src.value.to_string(); | ||
self.exports_star_sources.push(source); | ||
} | ||
// export {a, b} || export {default as c} from 'd' || export a from 'b' | ||
ModuleDecl::ExportNamed(named) => { | ||
named | ||
.specifiers | ||
.iter() | ||
.for_each(|specifier| match &specifier { | ||
ExportSpecifier::Named(named) => { | ||
if let Some(ModuleExportName::Ident(ident)) = &named.exported { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} else if let ModuleExportName::Ident(ident) = &named.orig { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} | ||
} | ||
ExportSpecifier::Namespace(name_spacing) => { | ||
if let ModuleExportName::Ident(ident) = &name_spacing.name { | ||
self.specifiers.remove(&ident.sym.to_string()); | ||
} | ||
} | ||
ExportSpecifier::Default(default) => { | ||
self.specifiers.remove(&default.exported.sym.to_string()); | ||
} | ||
}) | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
Oops, something went wrong.