Skip to content

Commit

Permalink
feat(es/minifier): Respect more options (#8582)
Browse files Browse the repository at this point in the history
**Description:**

This PR makes the ES minifier respect more options.

Changes:
 - `reduce_vars` now enables IIFE invoker.
 - `reduce_vars` now enables seq inliner.
 - `top_retain` is now respected by seq inliner.
 - `arguments` is now ignored by seq inliner.

---------

Co-authored-by: austaras <austaras@outlook.com>
  • Loading branch information
kdy1 and Austaras authored Feb 2, 2024
1 parent 507c469 commit fd997d3
Show file tree
Hide file tree
Showing 36 changed files with 175 additions and 208 deletions.
4 changes: 3 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ impl Optimizer<'_> {
pub(super) fn invoke_iife(&mut self, e: &mut Expr) {
trace_op!("iife: invoke_iife");

if self.options.inline == 0 {
if self.options.inline == 0
&& !(self.options.reduce_vars && self.options.reduce_fns && self.options.evaluate)
{
let skip = match e {
Expr::Call(v) => !v.callee.span().is_dummy(),
_ => true,
Expand Down
14 changes: 11 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
maybe_par,
mode::Mode,
option::{CompressOptions, MangleOptions},
program_data::ProgramData,
program_data::{ProgramData, VarUsageInfo},
util::{
contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt,
},
Expand Down Expand Up @@ -323,7 +323,7 @@ impl From<&Function> for FnMetadata {

impl Optimizer<'_> {
fn may_remove_ident(&self, id: &Ident) -> bool {
if self.ctx.is_exported {
if let Some(VarUsageInfo { exported: true, .. }) = self.data.vars.get(&id.clone().to_id()) {
return false;
}

Expand Down Expand Up @@ -2041,6 +2041,7 @@ impl VisitMut for Optimizer<'_> {

let ctx = Ctx {
top_level: false,
in_fn_like: true,
is_lhs_of_assign: false,
is_exact_lhs_of_assign: false,
..self.ctx
Expand All @@ -2062,7 +2063,14 @@ impl VisitMut for Optimizer<'_> {
}
}

e.visit_mut_children_with(self);
let ctx = Ctx {
top_level: false,
in_fn_like: true,
is_lhs_of_assign: false,
is_exact_lhs_of_assign: false,
..self.ctx
};
e.visit_mut_children_with(&mut *self.with_ctx(ctx));
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
Expand Down
23 changes: 21 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,16 @@ impl Optimizer<'_> {
options: &CompressOptions,
) -> Option<Vec<Mergable<'a>>> {
Some(match s {
Stmt::Expr(e) => vec![Mergable::Expr(&mut e.expr)],
Stmt::Expr(e) => {
if self.options.sequences()
|| self.options.collapse_vars
|| self.options.side_effects
{
vec![Mergable::Expr(&mut e.expr)]
} else {
return None;
}
}
Stmt::Decl(Decl::Var(v)) => {
if options.reduce_vars || options.collapse_vars {
v.decls.iter_mut().map(Mergable::Var).collect()
Expand Down Expand Up @@ -612,7 +621,7 @@ impl Optimizer<'_> {
where
T: ModuleItemExt,
{
if !self.options.sequences() && !self.options.collapse_vars {
if !self.options.sequences() && !self.options.collapse_vars && !self.options.reduce_vars {
log_abort!("sequences: [x] Disabled");
return;
}
Expand Down Expand Up @@ -1513,6 +1522,16 @@ impl Optimizer<'_> {
)
};

// Respect top_retain
if let Some(a_id) = a.id() {
if a_id.0 == "arguments"
|| (matches!(a, Mergable::Var(_) | Mergable::FnDecl(_))
&& !self.may_remove_ident(&Ident::from(a_id)))
{
return Ok(false);
}
}

if match &*b {
Expr::Arrow(..)
| Expr::Fn(..)
Expand Down
10 changes: 0 additions & 10 deletions crates/swc_ecma_minifier/tests/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ collapse_vars/issue_1858/input.js
collapse_vars/issue_2187_2/input.js
collapse_vars/issue_2203_2/input.js
collapse_vars/issue_2203_4/input.js
collapse_vars/issue_2298/input.js
collapse_vars/issue_2319_1/input.js
collapse_vars/issue_2319_3/input.js
collapse_vars/issue_2436_1/input.js
Expand Down Expand Up @@ -119,11 +118,8 @@ drop_unused/issue_2660_2/input.js
drop_unused/issue_2665/input.js
drop_unused/issue_2846/input.js
drop_unused/issue_t161_top_retain_10/input.js
drop_unused/issue_t161_top_retain_11/input.js
drop_unused/issue_t161_top_retain_12/input.js
drop_unused/issue_t161_top_retain_15/input.js
drop_unused/issue_t161_top_retain_8/input.js
drop_unused/issue_t161_top_retain_9/input.js
drop_unused/issue_t183/input.js
drop_unused/keep_assign/input.js
drop_unused/reassign_const/input.js
Expand Down Expand Up @@ -159,7 +155,6 @@ functions/issue_2107/input.js
functions/issue_2114_1/input.js
functions/issue_2114_2/input.js
functions/issue_2601_2/input.js
functions/issue_2604_2/input.js
functions/issue_2620_2/input.js
functions/issue_2620_3/input.js
functions/issue_2630_2/input.js
Expand Down Expand Up @@ -297,13 +292,8 @@ reduce_vars/iife/input.js
reduce_vars/iife_new/input.js
reduce_vars/inner_var_for_2/input.js
reduce_vars/inverted_var/input.js
reduce_vars/issue_1595_3/input.js
reduce_vars/issue_1670_2/input.js
reduce_vars/issue_1670_4/input.js
reduce_vars/issue_1670_5/input.js
reduce_vars/issue_1850_2/input.js
reduce_vars/issue_2485/input.js
reduce_vars/issue_2757_1/input.js
reduce_vars/issue_2799_2/input.js
reduce_vars/issue_2836/input.js
reduce_vars/issue_2860_2/input.js
Expand Down
Loading

0 comments on commit fd997d3

Please sign in to comment.