mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(minifier): improve folding block stmts (#6793)
This commit is contained in:
parent
7c51b2fd9d
commit
b4bc300ebf
2 changed files with 64 additions and 12 deletions
|
|
@ -146,7 +146,12 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if block.body.len() == 0
|
if block.body.len() == 0
|
||||||
&& (ctx.parent().is_block_statement() || ctx.parent().is_program())
|
&& (ctx.parent().is_while_statement()
|
||||||
|
|| ctx.parent().is_for_statement()
|
||||||
|
|| ctx.parent().is_for_in_statement()
|
||||||
|
|| ctx.parent().is_for_of_statement()
|
||||||
|
|| ctx.parent().is_block_statement()
|
||||||
|
|| ctx.parent().is_program())
|
||||||
{
|
{
|
||||||
// Remove the block if it is empty and the parent is a block statement.
|
// Remove the block if it is empty and the parent is a block statement.
|
||||||
*stmt = ctx.ast.statement_empty(SPAN);
|
*stmt = ctx.ast.statement_empty(SPAN);
|
||||||
|
|
@ -282,6 +287,53 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
Expression::ObjectExpression(object_expr) => {
|
Expression::ObjectExpression(object_expr) => {
|
||||||
Self::try_fold_object_expression(object_expr, ctx)
|
Self::try_fold_object_expression(object_expr, ctx)
|
||||||
}
|
}
|
||||||
|
Expression::TemplateLiteral(template_lit) => {
|
||||||
|
template_lit.expressions.retain(
|
||||||
|
oxc_ecmascript::side_effects::MayHaveSideEffects::may_have_side_effects,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut expressions = ctx.ast.move_vec(&mut template_lit.expressions);
|
||||||
|
|
||||||
|
if expressions.len() == 0 {
|
||||||
|
return Some(ctx.ast.statement_empty(SPAN));
|
||||||
|
} else if expressions.len() == 1 {
|
||||||
|
return Some(
|
||||||
|
ctx.ast.statement_expression(
|
||||||
|
template_lit.span,
|
||||||
|
expressions.pop().unwrap(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(ctx.ast.statement_expression(
|
||||||
|
template_lit.span,
|
||||||
|
ctx.ast.expression_from_sequence(
|
||||||
|
ctx.ast.sequence_expression(template_lit.span, expressions),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Expression::BinaryExpression(bin_expr) => {
|
||||||
|
let left = bin_expr.left.may_have_side_effects();
|
||||||
|
let right = bin_expr.left.may_have_side_effects();
|
||||||
|
|
||||||
|
if left && right {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if !left && !right {
|
||||||
|
return Some(ctx.ast.statement_empty(SPAN));
|
||||||
|
}
|
||||||
|
return Some(ctx.ast.statement_expression(
|
||||||
|
bin_expr.span,
|
||||||
|
ctx.ast.move_expression(if left {
|
||||||
|
&mut bin_expr.right
|
||||||
|
} else {
|
||||||
|
&mut bin_expr.left
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Expression::FunctionExpression(function_expr) if function_expr.id.is_none() => {
|
||||||
|
Some(ctx.ast.statement_empty(SPAN))
|
||||||
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -503,22 +555,22 @@ mod test {
|
||||||
fold("{if(false)if(false)if(false)foo(); {bar()}}", "bar()");
|
fold("{if(false)if(false)if(false)foo(); {bar()}}", "bar()");
|
||||||
|
|
||||||
fold("{'hi'}", "");
|
fold("{'hi'}", "");
|
||||||
// fold("{x==3}", "");
|
fold("{x==3}", "");
|
||||||
// fold("{`hello ${foo}`}", "");
|
fold("{`hello ${foo}`}", "");
|
||||||
// fold("{ (function(){x++}) }", "");
|
fold("{ (function(){x++}) }", "");
|
||||||
// fold_same("function f(){return;}");
|
fold_same("function f(){return;}");
|
||||||
// fold("function f(){return 3;}", "function f(){return 3}");
|
fold("function f(){return 3;}", "function f(){return 3}");
|
||||||
// fold_same("function f(){if(x)return; x=3; return; }");
|
// fold_same("function f(){if(x)return; x=3; return; }");
|
||||||
// fold("{x=3;;;y=2;;;}", "x=3;y=2");
|
// fold("{x=3;;;y=2;;;}", "x=3;y=2");
|
||||||
|
|
||||||
// Cases to test for empty block.
|
// Cases to test for empty block.
|
||||||
// fold("while(x()){x}", "while(x());");
|
// fold("while(x()){x}", "while(x());");
|
||||||
// fold("while(x()){x()}", "while(x())x()");
|
fold("while(x()){x()}", "while(x())x()");
|
||||||
// fold("for(x=0;x<100;x++){x}", "for(x=0;x<100;x++);");
|
// fold("for(x=0;x<100;x++){x}", "for(x=0;x<100;x++);");
|
||||||
// fold("for(x in y){x}", "for(x in y);");
|
// fold("for(x in y){x}", "for(x in y);");
|
||||||
// fold("for (x of y) {x}", "for(x of y);");
|
// fold("for (x of y) {x}", "for(x of y);");
|
||||||
// fold_same("for (let x = 1; x <10; x++ ) {}");
|
fold("for (let x = 1; x <10; x++ ) {}", "for (let x = 1; x <10; x++ );");
|
||||||
// fold_same("for (var x = 1; x <10; x++ ) {}");
|
fold("for (var x = 1; x <10; x++ ) {}", "for (var x = 1; x <10; x++ );");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,11 @@ Original | Minified | esbuild | Gzip | esbuild
|
||||||
|
|
||||||
173.90 kB | 61.67 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js
|
173.90 kB | 61.67 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js
|
||||||
|
|
||||||
287.63 kB | 92.70 kB | 90.07 kB | 32.27 kB | 31.95 kB | jquery.js
|
287.63 kB | 92.70 kB | 90.07 kB | 32.26 kB | 31.95 kB | jquery.js
|
||||||
|
|
||||||
342.15 kB | 121.90 kB | 118.14 kB | 44.59 kB | 44.37 kB | vue.js
|
342.15 kB | 121.90 kB | 118.14 kB | 44.59 kB | 44.37 kB | vue.js
|
||||||
|
|
||||||
544.10 kB | 73.49 kB | 72.48 kB | 26.13 kB | 26.20 kB | lodash.js
|
544.10 kB | 73.48 kB | 72.48 kB | 26.12 kB | 26.20 kB | lodash.js
|
||||||
|
|
||||||
555.77 kB | 276.49 kB | 270.13 kB | 91.15 kB | 90.80 kB | d3.js
|
555.77 kB | 276.49 kB | 270.13 kB | 91.15 kB | 90.80 kB | d3.js
|
||||||
|
|
||||||
|
|
@ -16,7 +16,7 @@ Original | Minified | esbuild | Gzip | esbuild
|
||||||
|
|
||||||
1.25 MB | 662.86 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js
|
1.25 MB | 662.86 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js
|
||||||
|
|
||||||
2.14 MB | 741.57 kB | 724.14 kB | 181.47 kB | 181.07 kB | victory.js
|
2.14 MB | 741.57 kB | 724.14 kB | 181.45 kB | 181.07 kB | victory.js
|
||||||
|
|
||||||
3.20 MB | 1.02 MB | 1.01 MB | 332.01 kB | 331.56 kB | echarts.js
|
3.20 MB | 1.02 MB | 1.01 MB | 332.01 kB | 331.56 kB | echarts.js
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue