perf(minifier): avoid unecessary import/export merging for cjs/scripts (#27291)

- disable processing of import/export for files that can't contain it
- if `target_specifiers` are fully consumed we do not have to attempt to drop them
This commit is contained in:
Armano authored and GitHub committed 2026-10-06 09:42:23 +01:00
1 parent 7e8e97c35b
commit 180d4e7280
2 files changed
+7 -7

No files matched your search

@@ -230,10 +230,6 @@ impl<'a> PeepholeOptimizations {
continue;
};
{
let source_stmt = stmts.get(statement_index).unwrap();
ctx.drop_statement(source_stmt);
}
let Statement::ImportDeclaration(source_import) =
stmts.get_mut(statement_index).unwrap()
else {
@@ -272,7 +268,8 @@ impl<'a> PeepholeOptimizations {
// Remove imports in reverse order so removing a later import does not
// shift the indices of any merge that is still waiting to be applied.
for &source_index in merges.iter().rev() {
stmts.remove(source_index);
let source_stmt = stmts.remove(source_index);
ctx.drop_statement(&source_stmt);
}
}
+5 -2
View File
@@ -390,8 +390,11 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
}
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
Self::merge_imports(&mut program.body, ctx);
Self::merge_import_export(&mut program.body, ctx);
// skip merging import / export for cjs and scripts as they can't contain import stmt
if !ctx.source_type().is_script() && !ctx.source_type().is_commonjs() {
Self::merge_imports(&mut program.body, ctx);
Self::merge_import_export(&mut program.body, ctx);
}
// Private member usage is collected only in full optimization mode.
debug_assert!(ctx.is_tree_shake_only() || ctx.state.private_member_usage.is_at_root());
}