-
Notifications
You must be signed in to change notification settings - Fork 108
feat: ensure esm imports exists when mode is production #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00769e6
feat: ensure esm imports exists when mode is production
Jinbao1001 d72597f
chore: impl
Jinbao1001 e826a0b
fix: some error
Jinbao1001 12adc52
feat: add exp config
Jinbao1001 05f3267
fix: export default specifier
Jinbao1001 9c51b22
chore: default to false
Jinbao1001 baf20e4
fix: module info system
Jinbao1001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
避免使用
panic!,改用适当的错误处理机制在生产环境中使用
panic!可能导致程序崩溃,影响用户体验。建议使用错误返回值或日志记录来处理错误,以提供更好的稳定性和可维护性。可以考虑修改为:
if should_panic { - panic!("dependency check error!"); + return Err(anyhow::anyhow!("Dependency check error")); };并在函数签名中调整返回类型: