mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 13:51:19 +03:00
refactor(es/minifier): Cleanup (#4489)
This commit is contained in:
parent
b26ac6105e
commit
50b20496cd
@ -41,8 +41,8 @@ echo "----- ⚠️ Removing cache"
|
|||||||
(cd $dir && rm -rf .next)
|
(cd $dir && rm -rf .next)
|
||||||
|
|
||||||
echo "----- ⚠️ Replacing swc binary"
|
echo "----- ⚠️ Replacing swc binary"
|
||||||
cp packages/next-swc/native/*.node $dir/node_modules/@next/swc-*/
|
mv packages/next-swc/native/*.node $dir/node_modules/@next/swc-*/
|
||||||
ls -al $dir/node_modules/@next/swc-*/
|
ls -alh $dir/node_modules/@next/swc-*/
|
||||||
|
|
||||||
# Build and start
|
# Build and start
|
||||||
echo "----- ⚠️ Building the app using next"
|
echo "----- ⚠️ Building the app using next"
|
||||||
|
@ -24,7 +24,7 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, Vis
|
|||||||
use swc_timer::timer;
|
use swc_timer::timer;
|
||||||
use tracing::{debug, error};
|
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 self::{hoist_decls::DeclHoisterConfig, optimize::optimizer};
|
||||||
use crate::{
|
use crate::{
|
||||||
analyzer::{analyze, UsageAnalyzer},
|
analyzer::{analyze, UsageAnalyzer},
|
||||||
@ -319,9 +319,11 @@ where
|
|||||||
self.options,
|
self.options,
|
||||||
None,
|
None,
|
||||||
self.marks,
|
self.marks,
|
||||||
M::force_str_for_tpl(),
|
PureOptimizerConfig {
|
||||||
self.pass > 1,
|
enable_join_vars: self.pass > 1,
|
||||||
self.pass >= 20,
|
force_str_for_tpl: M::force_str_for_tpl(),
|
||||||
|
debug_infinite_loop: self.pass >= 20,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
n.apply(&mut visitor);
|
n.apply(&mut visitor);
|
||||||
|
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
#![allow(unused_imports)]
|
|
||||||
|
|
||||||
use std::{iter::once, mem::take};
|
use std::{iter::once, mem::take};
|
||||||
|
|
||||||
use retain_mut::RetainMut;
|
use retain_mut::RetainMut;
|
||||||
@ -98,6 +95,7 @@ struct Ctx {
|
|||||||
skip_standalone: bool,
|
skip_standalone: bool,
|
||||||
|
|
||||||
/// `true` if the [VarDecl] has const annotation.
|
/// `true` if the [VarDecl] has const annotation.
|
||||||
|
#[allow(dead_code)]
|
||||||
has_const_ann: bool,
|
has_const_ann: bool,
|
||||||
|
|
||||||
in_bool_ctx: bool,
|
in_bool_ctx: bool,
|
||||||
@ -149,13 +147,12 @@ struct Ctx {
|
|||||||
|
|
||||||
in_obj_of_non_computed_member: bool,
|
in_obj_of_non_computed_member: bool,
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
in_tpl_expr: bool,
|
in_tpl_expr: bool,
|
||||||
|
|
||||||
/// True while handling callee, except an arrow expression in callee.
|
/// True while handling callee, except an arrow expression in callee.
|
||||||
is_this_aware_callee: bool,
|
is_this_aware_callee: bool,
|
||||||
|
|
||||||
can_inline_arguments: bool,
|
|
||||||
|
|
||||||
is_nested_if_return_merging: bool,
|
is_nested_if_return_merging: bool,
|
||||||
|
|
||||||
dont_invoke_iife: bool,
|
dont_invoke_iife: bool,
|
||||||
@ -1458,10 +1455,7 @@ where
|
|||||||
fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
|
fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
|
||||||
let prepend = self.prepend_stmts.take();
|
let prepend = self.prepend_stmts.take();
|
||||||
|
|
||||||
let ctx = Ctx {
|
let ctx = Ctx { ..self.ctx };
|
||||||
can_inline_arguments: true,
|
|
||||||
..self.ctx
|
|
||||||
};
|
|
||||||
|
|
||||||
n.visit_mut_children_with(&mut *self.with_ctx(ctx));
|
n.visit_mut_children_with(&mut *self.with_ctx(ctx));
|
||||||
|
|
||||||
@ -2010,7 +2004,6 @@ where
|
|||||||
skip_standalone: self.ctx.skip_standalone || is_standalone,
|
skip_standalone: self.ctx.skip_standalone || is_standalone,
|
||||||
in_fn_like: true,
|
in_fn_like: true,
|
||||||
scope: n.span.ctxt,
|
scope: n.span.ctxt,
|
||||||
can_inline_arguments: true,
|
|
||||||
top_level: false,
|
top_level: false,
|
||||||
|
|
||||||
..self.ctx
|
..self.ctx
|
||||||
@ -2033,10 +2026,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let ctx = Ctx {
|
let ctx = Ctx { ..self.ctx };
|
||||||
can_inline_arguments: true,
|
|
||||||
..self.ctx
|
|
||||||
};
|
|
||||||
self.with_ctx(ctx).optimize_usage_of_arguments(n);
|
self.with_ctx(ctx).optimize_usage_of_arguments(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use swc_atoms::JsWord;
|
|
||||||
use swc_common::{Span, SyntaxContext};
|
use swc_common::{Span, SyntaxContext};
|
||||||
use swc_ecma_ast::*;
|
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 swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
@ -15,48 +14,6 @@ impl<'b, M> Optimizer<'b, M>
|
|||||||
where
|
where
|
||||||
M: Mode,
|
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>(
|
pub(super) fn access_numeric_property<'e>(
|
||||||
&mut self,
|
&mut self,
|
||||||
_expr: &'e mut Expr,
|
_expr: &'e mut Expr,
|
||||||
|
@ -4,9 +4,6 @@ use super::Pure;
|
|||||||
|
|
||||||
#[derive(Default, Clone, Copy)]
|
#[derive(Default, Clone, Copy)]
|
||||||
pub(super) struct Ctx {
|
pub(super) struct Ctx {
|
||||||
/// This is actually config, but stored here.
|
|
||||||
pub force_str_for_tpl: bool,
|
|
||||||
|
|
||||||
pub par_depth: u8,
|
pub par_depth: u8,
|
||||||
|
|
||||||
pub in_delete: bool,
|
pub in_delete: bool,
|
||||||
|
@ -34,40 +34,44 @@ mod strings;
|
|||||||
mod unsafes;
|
mod unsafes;
|
||||||
mod vars;
|
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)]
|
#[allow(clippy::needless_lifetimes)]
|
||||||
pub(crate) fn pure_optimizer<'a>(
|
pub(crate) fn pure_optimizer<'a>(
|
||||||
options: &'a CompressOptions,
|
options: &'a CompressOptions,
|
||||||
data: Option<&'a ProgramData>,
|
data: Option<&'a ProgramData>,
|
||||||
marks: Marks,
|
marks: Marks,
|
||||||
force_str_for_tpl: bool,
|
config: PureOptimizerConfig,
|
||||||
enable_everything: bool,
|
|
||||||
debug_infinite_loop: bool,
|
|
||||||
) -> impl 'a + VisitMut + Repeated {
|
) -> impl 'a + VisitMut + Repeated {
|
||||||
Pure {
|
Pure {
|
||||||
options,
|
options,
|
||||||
|
config,
|
||||||
marks,
|
marks,
|
||||||
data,
|
data,
|
||||||
ctx: Ctx {
|
ctx: Ctx {
|
||||||
force_str_for_tpl,
|
|
||||||
top_level: true,
|
top_level: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
changed: Default::default(),
|
changed: Default::default(),
|
||||||
enable_everything,
|
|
||||||
debug_infinite_loop,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Pure<'a> {
|
struct Pure<'a> {
|
||||||
options: &'a CompressOptions,
|
options: &'a CompressOptions,
|
||||||
|
config: PureOptimizerConfig,
|
||||||
marks: Marks,
|
marks: Marks,
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
data: Option<&'a ProgramData>,
|
data: Option<&'a ProgramData>,
|
||||||
ctx: Ctx,
|
ctx: Ctx,
|
||||||
changed: bool,
|
changed: bool,
|
||||||
enable_everything: bool,
|
|
||||||
|
|
||||||
debug_infinite_loop: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Repeated for Pure<'_> {
|
impl Repeated for Pure<'_> {
|
||||||
@ -114,7 +118,7 @@ impl Pure<'_> {
|
|||||||
stmts.visit_with(&mut AssertValid);
|
stmts.visit_with(&mut AssertValid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.enable_everything {
|
if self.config.enable_join_vars {
|
||||||
self.join_vars(stmts);
|
self.join_vars(stmts);
|
||||||
|
|
||||||
if cfg!(debug_assertions) {
|
if cfg!(debug_assertions) {
|
||||||
@ -595,7 +599,7 @@ impl VisitMut for Pure<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_mut_stmt(&mut self, s: &mut Stmt) {
|
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);
|
let text = dump(&*s, false);
|
||||||
|
|
||||||
if text.lines().count() < 10 {
|
if text.lines().count() < 10 {
|
||||||
@ -618,7 +622,7 @@ impl VisitMut for Pure<'_> {
|
|||||||
s.visit_mut_children_with(&mut *self.with_ctx(ctx));
|
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);
|
let text = dump(&*s, false);
|
||||||
|
|
||||||
if text.lines().count() < 10 {
|
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);
|
let text = dump(&*s, false);
|
||||||
|
|
||||||
if text.lines().count() < 10 {
|
if text.lines().count() < 10 {
|
||||||
|
@ -193,9 +193,10 @@ impl Pure<'_> {
|
|||||||
|
|
||||||
if c.chars().all(|c| match c {
|
if c.chars().all(|c| match c {
|
||||||
'\u{0020}'..='\u{007e}' => true,
|
'\u{0020}'..='\u{007e}' => true,
|
||||||
'\n' | '\r' => self.ctx.force_str_for_tpl,
|
'\n' | '\r' => self.config.force_str_for_tpl,
|
||||||
_ => false,
|
_ => 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("\\0")
|
||||||
&& !c.contains("\\x")
|
&& !c.contains("\\x")
|
||||||
{
|
{
|
||||||
|
@ -9,7 +9,7 @@ use swc_ecma_utils::{ident::IdentLike, undefined, ExprExt, Id};
|
|||||||
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
compress::{compressor, pure_optimizer},
|
compress::{compressor, pure_optimizer, PureOptimizerConfig},
|
||||||
marks::Marks,
|
marks::Marks,
|
||||||
mode::Mode,
|
mode::Mode,
|
||||||
};
|
};
|
||||||
@ -227,9 +227,11 @@ impl Evaluator {
|
|||||||
&serde_json::from_str("{}").unwrap(),
|
&serde_json::from_str("{}").unwrap(),
|
||||||
None,
|
None,
|
||||||
self.marks,
|
self.marks,
|
||||||
Eval::force_str_for_tpl(),
|
PureOptimizerConfig {
|
||||||
true,
|
enable_join_vars: false,
|
||||||
false,
|
force_str_for_tpl: Eval::force_str_for_tpl(),
|
||||||
|
debug_infinite_loop: false,
|
||||||
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
#![allow(clippy::logic_bug)]
|
#![allow(clippy::logic_bug)]
|
||||||
#![allow(unstable_name_collisions)]
|
#![allow(unstable_name_collisions)]
|
||||||
|
|
||||||
use compress::pure_optimizer;
|
use compress::{pure_optimizer, PureOptimizerConfig};
|
||||||
use mode::Mode;
|
use mode::Mode;
|
||||||
use swc_common::{comments::Comments, pass::Repeat, sync::Lrc, SourceMap, GLOBALS};
|
use swc_common::{comments::Comments, pass::Repeat, sync::Lrc, SourceMap, GLOBALS};
|
||||||
use swc_ecma_ast::Module;
|
use swc_ecma_ast::Module;
|
||||||
@ -153,9 +153,11 @@ pub fn optimize(
|
|||||||
options,
|
options,
|
||||||
None,
|
None,
|
||||||
marks,
|
marks,
|
||||||
Minification::force_str_for_tpl(),
|
PureOptimizerConfig {
|
||||||
true,
|
force_str_for_tpl: Minification::force_str_for_tpl(),
|
||||||
false,
|
enable_join_vars: true,
|
||||||
|
debug_infinite_loop: false,
|
||||||
|
},
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user