refactor(es/minifier): Cleanup (#4489)

This commit is contained in:
Donny/강동윤 2022-04-30 16:00:21 +09:00 committed by GitHub
parent b26ac6105e
commit 50b20496cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 90 deletions

View File

@ -41,8 +41,8 @@ echo "----- ⚠️ Removing cache"
(cd $dir && rm -rf .next)
echo "----- ⚠️ Replacing swc binary"
cp packages/next-swc/native/*.node $dir/node_modules/@next/swc-*/
ls -al $dir/node_modules/@next/swc-*/
mv packages/next-swc/native/*.node $dir/node_modules/@next/swc-*/
ls -alh $dir/node_modules/@next/swc-*/
# Build and start
echo "----- ⚠️ Building the app using next"

View File

@ -24,7 +24,7 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, Vis
use swc_timer::timer;
use tracing::{debug, error};
pub(crate) use self::pure::pure_optimizer;
pub(crate) use self::pure::{pure_optimizer, PureOptimizerConfig};
use self::{hoist_decls::DeclHoisterConfig, optimize::optimizer};
use crate::{
analyzer::{analyze, UsageAnalyzer},
@ -319,9 +319,11 @@ where
self.options,
None,
self.marks,
M::force_str_for_tpl(),
self.pass > 1,
self.pass >= 20,
PureOptimizerConfig {
enable_join_vars: self.pass > 1,
force_str_for_tpl: M::force_str_for_tpl(),
debug_infinite_loop: self.pass >= 20,
},
);
n.apply(&mut visitor);

View File

@ -1,6 +1,3 @@
#![allow(dead_code)]
#![allow(unused_imports)]
use std::{iter::once, mem::take};
use retain_mut::RetainMut;
@ -98,6 +95,7 @@ struct Ctx {
skip_standalone: bool,
/// `true` if the [VarDecl] has const annotation.
#[allow(dead_code)]
has_const_ann: bool,
in_bool_ctx: bool,
@ -149,13 +147,12 @@ struct Ctx {
in_obj_of_non_computed_member: bool,
#[allow(dead_code)]
in_tpl_expr: bool,
/// True while handling callee, except an arrow expression in callee.
is_this_aware_callee: bool,
can_inline_arguments: bool,
is_nested_if_return_merging: bool,
dont_invoke_iife: bool,
@ -1458,10 +1455,7 @@ where
fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
let prepend = self.prepend_stmts.take();
let ctx = Ctx {
can_inline_arguments: true,
..self.ctx
};
let ctx = Ctx { ..self.ctx };
n.visit_mut_children_with(&mut *self.with_ctx(ctx));
@ -2010,7 +2004,6 @@ where
skip_standalone: self.ctx.skip_standalone || is_standalone,
in_fn_like: true,
scope: n.span.ctxt,
can_inline_arguments: true,
top_level: false,
..self.ctx
@ -2033,10 +2026,7 @@ where
}
{
let ctx = Ctx {
can_inline_arguments: true,
..self.ctx
};
let ctx = Ctx { ..self.ctx };
self.with_ctx(ctx).optimize_usage_of_arguments(n);
}

View File

@ -1,10 +1,9 @@
use std::ops::{Deref, DerefMut};
use rustc_hash::FxHashMap;
use swc_atoms::JsWord;
use swc_common::{Span, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{ident::IdentLike, prop_name_eq, ExprExt, Id};
use swc_ecma_utils::{ident::IdentLike, ExprExt, Id};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use tracing::debug;
@ -15,48 +14,6 @@ impl<'b, M> Optimizer<'b, M>
where
M: Mode,
{
pub(super) fn access_property<'e>(
&mut self,
expr: &'e mut Expr,
prop: &JsWord,
) -> Option<&'e mut Expr> {
if let Expr::Object(obj) = expr {
for obj_prop in obj.props.iter_mut() {
match obj_prop {
PropOrSpread::Spread(_) => {}
PropOrSpread::Prop(p) => match &mut **p {
Prop::Shorthand(_) => {}
Prop::KeyValue(p) => {
if prop_name_eq(&p.key, prop) {
return Some(&mut *p.value);
}
}
Prop::Assign(_) => {}
Prop::Getter(_) => {}
Prop::Setter(_) => {}
Prop::Method(_) => {}
},
}
}
}
None
}
pub(super) fn access_property_with_prop_name<'e>(
&mut self,
expr: &'e mut Expr,
prop: &PropName,
) -> Option<&'e mut Expr> {
match prop {
PropName::Ident(p) => self.access_property(expr, &p.sym),
PropName::Str(p) => self.access_property(expr, &p.value),
PropName::Num(p) => self.access_numeric_property(expr, p.value as _),
PropName::Computed(_) => None,
PropName::BigInt(_) => None,
}
}
pub(super) fn access_numeric_property<'e>(
&mut self,
_expr: &'e mut Expr,

View File

@ -4,9 +4,6 @@ use super::Pure;
#[derive(Default, Clone, Copy)]
pub(super) struct Ctx {
/// This is actually config, but stored here.
pub force_str_for_tpl: bool,
pub par_depth: u8,
pub in_delete: bool,

View File

@ -34,40 +34,44 @@ mod strings;
mod unsafes;
mod vars;
#[derive(Debug, Clone, Copy)]
pub(crate) struct PureOptimizerConfig {
/// pass > 1
pub enable_join_vars: bool,
pub force_str_for_tpl: bool,
pub debug_infinite_loop: bool,
}
#[allow(clippy::needless_lifetimes)]
pub(crate) fn pure_optimizer<'a>(
options: &'a CompressOptions,
data: Option<&'a ProgramData>,
marks: Marks,
force_str_for_tpl: bool,
enable_everything: bool,
debug_infinite_loop: bool,
config: PureOptimizerConfig,
) -> impl 'a + VisitMut + Repeated {
Pure {
options,
config,
marks,
data,
ctx: Ctx {
force_str_for_tpl,
top_level: true,
..Default::default()
},
changed: Default::default(),
enable_everything,
debug_infinite_loop,
}
}
struct Pure<'a> {
options: &'a CompressOptions,
config: PureOptimizerConfig,
marks: Marks,
#[allow(unused)]
data: Option<&'a ProgramData>,
ctx: Ctx,
changed: bool,
enable_everything: bool,
debug_infinite_loop: bool,
}
impl Repeated for Pure<'_> {
@ -114,7 +118,7 @@ impl Pure<'_> {
stmts.visit_with(&mut AssertValid);
}
if self.enable_everything {
if self.config.enable_join_vars {
self.join_vars(stmts);
if cfg!(debug_assertions) {
@ -595,7 +599,7 @@ impl VisitMut for Pure<'_> {
}
fn visit_mut_stmt(&mut self, s: &mut Stmt) {
let _tracing = if cfg!(feature = "debug") && self.debug_infinite_loop {
let _tracing = if cfg!(feature = "debug") && self.config.debug_infinite_loop {
let text = dump(&*s, false);
if text.lines().count() < 10 {
@ -618,7 +622,7 @@ impl VisitMut for Pure<'_> {
s.visit_mut_children_with(&mut *self.with_ctx(ctx));
}
if cfg!(feature = "debug") && self.debug_infinite_loop {
if cfg!(feature = "debug") && self.config.debug_infinite_loop {
let text = dump(&*s, false);
if text.lines().count() < 10 {
@ -652,7 +656,7 @@ impl VisitMut for Pure<'_> {
}
}
if cfg!(feature = "debug") && self.debug_infinite_loop {
if cfg!(feature = "debug") && self.config.debug_infinite_loop {
let text = dump(&*s, false);
if text.lines().count() < 10 {

View File

@ -193,9 +193,10 @@ impl Pure<'_> {
if c.chars().all(|c| match c {
'\u{0020}'..='\u{007e}' => true,
'\n' | '\r' => self.ctx.force_str_for_tpl,
'\n' | '\r' => self.config.force_str_for_tpl,
_ => false,
}) && (self.ctx.force_str_for_tpl || (!c.contains("\\n") && !c.contains("\\r")))
}) && (self.config.force_str_for_tpl
|| (!c.contains("\\n") && !c.contains("\\r")))
&& !c.contains("\\0")
&& !c.contains("\\x")
{

View File

@ -9,7 +9,7 @@ use swc_ecma_utils::{ident::IdentLike, undefined, ExprExt, Id};
use swc_ecma_visit::{FoldWith, VisitMutWith};
use crate::{
compress::{compressor, pure_optimizer},
compress::{compressor, pure_optimizer, PureOptimizerConfig},
marks::Marks,
mode::Mode,
};
@ -227,9 +227,11 @@ impl Evaluator {
&serde_json::from_str("{}").unwrap(),
None,
self.marks,
Eval::force_str_for_tpl(),
true,
false,
PureOptimizerConfig {
enable_join_vars: false,
force_str_for_tpl: Eval::force_str_for_tpl(),
debug_infinite_loop: false,
},
));
}

View File

@ -26,7 +26,7 @@
#![allow(clippy::logic_bug)]
#![allow(unstable_name_collisions)]
use compress::pure_optimizer;
use compress::{pure_optimizer, PureOptimizerConfig};
use mode::Mode;
use swc_common::{comments::Comments, pass::Repeat, sync::Lrc, SourceMap, GLOBALS};
use swc_ecma_ast::Module;
@ -153,9 +153,11 @@ pub fn optimize(
options,
None,
marks,
Minification::force_str_for_tpl(),
true,
false,
PureOptimizerConfig {
force_str_for_tpl: Minification::force_str_for_tpl(),
enable_join_vars: true,
debug_infinite_loop: false,
},
)));
}