Skip to content
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

perf:skip modules patch module graph by self #826

Merged
merged 9 commits into from
Jan 2, 2024
15 changes: 12 additions & 3 deletions crates/mako/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,20 @@ impl ModuleGraph {
targets
}

pub fn remove_dependencies(&mut self, module_id: &ModuleId) {
pub fn remove_dependency_module_by_source(&mut self, module_id: &ModuleId, source: &String) {
let mut edges = self.get_edges(module_id, Direction::Outgoing);

while let Some((edge_index, _)) = edges.next(&self.graph) {
self.graph.remove_edge(edge_index);
while let Some((edge_index, _node_index)) = edges.next(&self.graph) {
let dependencies = self.graph.edge_weight_mut(edge_index).unwrap();

if let Some(to_del_dep) = dependencies.iter().position(|dep| *source == dep.source) {
dependencies.take(&dependencies.iter().nth(to_del_dep).unwrap().clone());

if dependencies.is_empty() {
self.graph.remove_edge(edge_index);
}
return;
}
}
}

Expand Down
55 changes: 27 additions & 28 deletions crates/mako/src/plugins/farm_tree_shake/module/import_exports.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::plugins::farm_tree_shake::module::{is_ident_sym_equal, TreeShakeModule};
use crate::plugins::farm_tree_shake::shake::skip_module::{ReExportSource2, ReExportType2};
use crate::plugins::farm_tree_shake::shake::skip_module::{ReExportSource, ReExportType};
use crate::plugins::farm_tree_shake::shake::strip_context;
use crate::plugins::farm_tree_shake::statement_graph::{ExportSpecifierInfo, ImportSpecifierInfo};

impl TreeShakeModule {
pub fn find_export_source(&self, ident: &String) -> Option<ReExportSource2> {
pub fn find_export_source(&self, ident: &String) -> Option<ReExportSource> {
let mut local_ident = None;
let mut re_export_type = None;

Expand All @@ -15,8 +15,8 @@ impl TreeShakeModule {
match export_specifier {
ExportSpecifierInfo::All(all_exports) => {
if all_exports.iter().any(|i| is_ident_sym_equal(i, ident)) {
return Some(ReExportSource2 {
re_export_type: ReExportType2::Named(strip_context(ident)),
return Some(ReExportSource {
re_export_type: ReExportType::Named(strip_context(ident)),
source: Some(source.clone()),
});
}
Expand All @@ -31,21 +31,21 @@ impl TreeShakeModule {

if let Some(exported_name) = exported {
if is_ident_sym_equal(exported_name, ident) {
return Some(ReExportSource2 {
return Some(ReExportSource {
re_export_type: if stripped_local == "default" {
ReExportType2::Default
ReExportType::Default
} else {
ReExportType2::Named(stripped_local.clone())
ReExportType::Named(stripped_local.clone())
},
source: Some(source.clone()),
});
}
} else if is_ident_sym_equal(ident, local) {
return Some(ReExportSource2 {
return Some(ReExportSource {
re_export_type: if stripped_local == "default" {
ReExportType2::Default
ReExportType::Default
} else {
ReExportType2::Named(stripped_local.clone())
ReExportType::Named(stripped_local.clone())
},
source: Some(source.clone()),
});
Expand All @@ -60,8 +60,8 @@ impl TreeShakeModule {
ExportSpecifierInfo::Namespace(name) => {
let stripped_name = strip_context(name);
if stripped_name.eq(ident) {
return Some(ReExportSource2 {
re_export_type: ReExportType2::Namespace,
return Some(ReExportSource {
re_export_type: ReExportType::Namespace,
source: Some(source.clone()),
});
}
Expand All @@ -75,16 +75,15 @@ impl TreeShakeModule {
ExportSpecifierInfo::Named { exported, local } => {
if let Some(exported_name) = exported {
if is_ident_sym_equal(exported_name, ident) {
re_export_type = Some(ReExportType2::Named(strip_context(
exported_name,
)));
re_export_type =
Some(ReExportType::Named(strip_context(exported_name)));

local_ident = Some(local.clone());
break;
}
} else if is_ident_sym_equal(ident, local) {
re_export_type =
Some(ReExportType2::Named(strip_context(local)));
Some(ReExportType::Named(strip_context(local)));
local_ident = Some(local.clone());

break;
Expand All @@ -93,12 +92,12 @@ impl TreeShakeModule {
ExportSpecifierInfo::Default(export_default_ident) => {
if ident == "default" {
if let Some(default_ident) = export_default_ident {
re_export_type = Some(ReExportType2::Default);
re_export_type = Some(ReExportType::Default);
local_ident = Some(default_ident.clone());
break;
} else {
return Some(ReExportSource2 {
re_export_type: ReExportType2::Default,
return Some(ReExportSource {
re_export_type: ReExportType::Default,
source: None,
});
}
Expand All @@ -119,8 +118,8 @@ impl TreeShakeModule {
if let Some(import_specifier) = import_info.find_define_specifier(local) {
match import_specifier {
ImportSpecifierInfo::Namespace(_namespace) => {
return Some(ReExportSource2 {
re_export_type: ReExportType2::Namespace,
return Some(ReExportSource {
re_export_type: ReExportType::Namespace,
source: Some(import_info.source.clone()),
});
}
Expand All @@ -135,8 +134,8 @@ impl TreeShakeModule {
local.clone()
};

return Some(ReExportSource2 {
re_export_type: ReExportType2::Named(strip_context(
return Some(ReExportSource {
re_export_type: ReExportType::Named(strip_context(
&next_name,
)),
source: Some(import_info.source.clone()),
Expand All @@ -145,8 +144,8 @@ impl TreeShakeModule {
}
ImportSpecifierInfo::Default(name) => {
if local == name {
return Some(ReExportSource2 {
re_export_type: ReExportType2::Default,
return Some(ReExportSource {
re_export_type: ReExportType::Default,
source: Some(import_info.source.clone()),
});
}
Expand All @@ -156,7 +155,7 @@ impl TreeShakeModule {
}
}

re_export_type.map(|re_export_type| ReExportSource2 {
re_export_type.map(|re_export_type| ReExportSource {
re_export_type,
source: None,
})
Expand All @@ -177,9 +176,9 @@ mod tests {
use crate::ast::build_js_ast;
use crate::compiler::Context;
use crate::module::{Module, ModuleAst, ModuleInfo};
use crate::plugins::farm_tree_shake::shake::skip_module::ReExportSource2;
use crate::plugins::farm_tree_shake::shake::skip_module::ReExportSource;

impl ReExportSource2 {
impl ReExportSource {
pub fn describe(&self) -> String {
if let Some(source) = &self.source {
format!("ReExport from {} by {:?}", source, self.re_export_type)
Expand Down
Loading