Skip to content

Commit

Permalink
refact: tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Mar 18, 2024
1 parent 506a462 commit 910d408
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 282 deletions.
100 changes: 51 additions & 49 deletions crates/mako/src/plugins/farm_tree_shake/remove_useless_stmts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,66 +29,68 @@ pub fn remove_useless_stmts(
let mut used_export_from_infos = vec![];

// remove unused specifiers in export statement and import statement
for (stmt_id, used_defined_idents) in &used_stmts {
let module_item = &mut swc_module.body[*stmt_id];

let StatementInfo {
import_info,
export_info,
..
} = analyze_imports_and_exports(
stmt_id,
module_item,
Some(used_defined_idents.clone()),
tree_shake_module.unresolved_ctxt,
);

if let Some(import_info) = import_info {
used_import_infos.push(import_info.clone());

let mut remover = UselessImportStmtsRemover { import_info };

module_item.visit_mut_with(&mut remover);
}
used_stmts
.iter()
.for_each(|(stmt_id, used_defined_idents)| {
let module_item = &mut swc_module.body[*stmt_id];

if let Some(mut export_info) = export_info {
// ignore export {}
if export_info.specifiers.is_empty() && export_info.source.is_none() {
continue;
}
let StatementInfo {
import_info,
export_info,
..
} = analyze_imports_and_exports(
stmt_id,
module_item,
Some(used_defined_idents.clone()),
tree_shake_module.unresolved_ctxt,
);

if export_info.source.is_some() {
// export {} from "x"
if export_info.specifiers.is_empty() {
used_export_from_infos.push(export_info.clone());
let mut remover = UselessExportStmtRemover { export_info };
if let Some(import_info) = import_info {
used_import_infos.push(import_info.clone());

module_item.visit_mut_with(&mut remover);
} else {
// export * from "x"
if matches!(export_info.specifiers[0], UsedExportSpecInfo::All(_)) {
export_info.specifiers[0] = UsedExportSpecInfo::All(
used_defined_idents.clone().into_iter().collect(),
);
let mut remover = UselessImportStmtsRemover { import_info };

used_export_from_infos.push(export_info.clone());
} else {
// export {a,b } from "x"
used_export_from_infos.push(export_info.clone());
module_item.visit_mut_with(&mut remover);
}

if let Some(mut export_info) = export_info {
// ignore export {}
if export_info.specifiers.is_empty() && export_info.source.is_none() {
return;
}

if export_info.source.is_some() {
// export {} from "x"
if export_info.specifiers.is_empty() {
used_export_from_infos.push(export_info.clone());
let mut remover = UselessExportStmtRemover { export_info };

module_item.visit_mut_with(&mut remover);
} else {
// export * from "x"
if matches!(export_info.specifiers[0], UsedExportSpecInfo::All(_)) {
export_info.specifiers[0] = UsedExportSpecInfo::All(
used_defined_idents.clone().into_iter().collect(),
);

used_export_from_infos.push(export_info.clone());
} else {
// export {a,b } from "x"
used_export_from_infos.push(export_info.clone());

let mut remover = UselessExportStmtRemover { export_info };

module_item.visit_mut_with(&mut remover);
}
}
}
} else {
// export { a ,b } or export default a;
let mut remover = UselessExportStmtRemover { export_info };
} else {
// export { a ,b } or export default a;
let mut remover = UselessExportStmtRemover { export_info };

module_item.visit_mut_with(&mut remover);
module_item.visit_mut_with(&mut remover);
}
}
}
}
});

let mut stmts_to_remove = vec![];
// TODO recognize the self-executed statements and preserve all the related statements
Expand Down
Loading

0 comments on commit 910d408

Please sign in to comment.