mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 13:51:19 +03:00
chore(es/minifier): Add logging (#4156)
This commit is contained in:
parent
d3ef18bfe3
commit
97dfcb9ccb
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -3379,8 +3379,8 @@ dependencies = [
|
||||
"swc_ecma_ast",
|
||||
"swc_ecma_codegen",
|
||||
"swc_ecma_parser",
|
||||
"swc_ecma_transforms",
|
||||
"swc_ecma_transforms_base",
|
||||
"swc_ecma_transforms_optimization",
|
||||
"swc_ecma_utils",
|
||||
"swc_ecma_visit",
|
||||
"swc_node_base",
|
||||
|
@ -14,7 +14,7 @@ all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[features]
|
||||
debug = ["backtrace"]
|
||||
debug = ["backtrace", "swc_ecma_transforms_optimization/debug"]
|
||||
|
||||
[dependencies]
|
||||
ahash = "0.7.6"
|
||||
@ -35,10 +35,8 @@ swc_common = { version = "0.17.0", path = "../swc_common" }
|
||||
swc_ecma_ast = { version = "0.73.0", path = "../swc_ecma_ast" }
|
||||
swc_ecma_codegen = { version = "0.100.0", path = "../swc_ecma_codegen" }
|
||||
swc_ecma_parser = { version = "0.97.0", path = "../swc_ecma_parser" }
|
||||
swc_ecma_transforms = { version = "0.138.0", path = "../swc_ecma_transforms/", features = [
|
||||
"optimization",
|
||||
] }
|
||||
swc_ecma_transforms_base = { version = "0.72.0", path = "../swc_ecma_transforms_base" }
|
||||
swc_ecma_transforms_optimization = { version = "0.108.0", path = "../swc_ecma_transforms_optimization" }
|
||||
swc_ecma_utils = { version = "0.76.0", path = "../swc_ecma_utils" }
|
||||
swc_ecma_visit = { version = "0.59.0", path = "../swc_ecma_visit" }
|
||||
swc_timer = { version = "0.5.0", path = "../swc_timer" }
|
||||
|
@ -10,7 +10,7 @@ use swc_ecma_minifier::{
|
||||
option::{ExtraOptions, MangleOptions, MinifyOptions},
|
||||
};
|
||||
use swc_ecma_parser::parse_file_as_module;
|
||||
use swc_ecma_transforms::{fixer, resolver_with_mark};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, resolver::resolver_with_mark};
|
||||
use swc_ecma_visit::FoldWith;
|
||||
|
||||
pub fn bench_files(c: &mut Criterion) {
|
||||
|
@ -11,7 +11,7 @@ use swc_ecma_minifier::{
|
||||
option::{ExtraOptions, MangleOptions, MinifyOptions},
|
||||
};
|
||||
use swc_ecma_parser::parse_file_as_module;
|
||||
use swc_ecma_transforms::{fixer, resolver_with_mark};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, resolver::resolver_with_mark};
|
||||
use swc_ecma_visit::FoldWith;
|
||||
|
||||
fn main() {
|
||||
|
@ -7,5 +7,5 @@
|
||||
|
||||
set -eux
|
||||
|
||||
export RUST_LOG=swc_ecma_minifier=trace
|
||||
cargo test $@ --features debug --test compress
|
||||
export RUST_LOG=swc_ecma_minifier=trace,swc_ecma_transforms_optimization=trace
|
||||
cargo test $@ --features debug --features swc_ecma_transforms_optimization/debug --test compress
|
@ -3,7 +3,7 @@ use std::path::PathBuf;
|
||||
use swc_common::{comments::SingleThreadedComments, Mark};
|
||||
use swc_ecma_ast::Id;
|
||||
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
|
||||
use swc_ecma_transforms::resolver_with_mark;
|
||||
use swc_ecma_transforms_base::resolver::resolver_with_mark;
|
||||
use swc_ecma_visit::FoldWith;
|
||||
use testing::NormalizedOutput;
|
||||
|
||||
|
@ -15,9 +15,9 @@ use swc_common::{
|
||||
Globals,
|
||||
};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms::{
|
||||
optimization::simplify::{dead_branch_remover, expr_simplifier, ExprSimplifierConfig},
|
||||
pass::JsPass,
|
||||
use swc_ecma_transforms_base::pass::JsPass;
|
||||
use swc_ecma_transforms_optimization::simplify::{
|
||||
dead_branch_remover, expr_simplifier, ExprSimplifierConfig,
|
||||
};
|
||||
use swc_ecma_utils::StmtLike;
|
||||
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
|
||||
|
@ -3,7 +3,7 @@ use std::f64;
|
||||
use swc_atoms::js_word;
|
||||
use swc_common::{util::take::Take, DUMMY_SP};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms::fixer;
|
||||
use swc_ecma_transforms_base::fixer::fixer;
|
||||
use swc_ecma_utils::{ExprExt, Id, UsageFinder, Value};
|
||||
use swc_ecma_visit::{
|
||||
as_folder, noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use swc_common::{util::take::Take, FileName};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_parser::parse_file_as_expr;
|
||||
use swc_ecma_transforms::fixer;
|
||||
use swc_ecma_transforms_base::fixer::fixer;
|
||||
use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith};
|
||||
use tracing::{info, warn};
|
||||
|
||||
|
@ -6,7 +6,7 @@ use once_cell::sync::Lazy;
|
||||
use swc_common::{sync::Lrc, SourceMap, SyntaxContext};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
||||
use swc_ecma_transforms::{fixer, hygiene};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene};
|
||||
use swc_ecma_utils::{drop_span, DropSpan};
|
||||
use swc_ecma_visit::{
|
||||
noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith,
|
||||
|
@ -4,7 +4,7 @@ use parking_lot::Mutex;
|
||||
use swc_atoms::js_word;
|
||||
use swc_common::{collections::AHashMap, util::take::Take, DUMMY_SP};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms::optimization::simplify::{expr_simplifier, ExprSimplifierConfig};
|
||||
use swc_ecma_transforms_optimization::simplify::{expr_simplifier, ExprSimplifierConfig};
|
||||
use swc_ecma_utils::{ident::IdentLike, undefined, ExprExt, Id};
|
||||
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use swc_common::{SyntaxContext, DUMMY_SP};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms::hygiene::rename;
|
||||
use swc_ecma_transforms_base::hygiene::rename;
|
||||
use swc_ecma_utils::UsageFinder;
|
||||
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
|
||||
|
||||
|
@ -2,7 +2,7 @@ use std::fmt::Debug;
|
||||
|
||||
use swc_common::Mark;
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms::{fixer, hygiene};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene};
|
||||
use swc_ecma_utils::DropSpan;
|
||||
use swc_ecma_visit::{as_folder, FoldWith, VisitMut, VisitMutWith, VisitWith};
|
||||
|
||||
|
@ -36,7 +36,7 @@ use swc_ecma_parser::{
|
||||
lexer::{input::SourceFileInput, Lexer},
|
||||
EsConfig, Parser, Syntax,
|
||||
};
|
||||
use swc_ecma_transforms::{fixer, hygiene::hygiene, resolver_with_mark};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver::resolver_with_mark};
|
||||
use swc_ecma_utils::drop_span;
|
||||
use swc_ecma_visit::{FoldWith, Visit, VisitMutWith, VisitWith};
|
||||
use testing::{assert_eq, DebugUsingDisplay, NormalizedOutput};
|
||||
|
@ -8,7 +8,7 @@ use swc_ecma_minifier::{
|
||||
marks::Marks,
|
||||
};
|
||||
use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsConfig, Syntax};
|
||||
use swc_ecma_transforms::resolver;
|
||||
use swc_ecma_transforms_base::resolver::resolver;
|
||||
use swc_ecma_utils::ExprExt;
|
||||
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
|
||||
use testing::{assert_eq, DebugUsingDisplay};
|
||||
|
@ -23,7 +23,7 @@ use swc_ecma_minifier::{
|
||||
},
|
||||
};
|
||||
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
|
||||
use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark};
|
||||
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver::resolver_with_mark};
|
||||
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
||||
use testing::DebugUsingDisplay;
|
||||
|
||||
|
@ -13,7 +13,7 @@ use swc_ecma_minifier::{
|
||||
option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions},
|
||||
};
|
||||
use swc_ecma_parser::parse_file_as_module;
|
||||
use swc_ecma_transforms::resolver_with_mark;
|
||||
use swc_ecma_transforms_base::resolver::resolver_with_mark;
|
||||
use swc_ecma_visit::VisitMutWith;
|
||||
use testing::NormalizedOutput;
|
||||
|
||||
|
@ -11,6 +11,7 @@ version = "0.108.0"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
debug = []
|
||||
# Process in parallel.
|
||||
concurrent = [
|
||||
"swc_common/concurrent",
|
||||
|
@ -15,6 +15,7 @@ use swc_ecma_utils::{
|
||||
use swc_ecma_visit::{
|
||||
as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith,
|
||||
};
|
||||
use tracing::{debug, trace};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@ -69,6 +70,10 @@ impl VisitMut for Remover {
|
||||
}
|
||||
|
||||
if let Some(i) = preserved {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Removing elements of an array pattern");
|
||||
}
|
||||
|
||||
p.elems.drain(i..);
|
||||
}
|
||||
}
|
||||
@ -90,6 +95,9 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping assignment to the same variable");
|
||||
}
|
||||
*e = Expr::Ident(r.take().ident().unwrap());
|
||||
}
|
||||
|
||||
@ -103,6 +111,9 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping assignment to an empty array pattern");
|
||||
}
|
||||
*e = *right.take();
|
||||
}
|
||||
|
||||
@ -116,6 +127,9 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping assignment to an empty object pattern");
|
||||
}
|
||||
*e = *right.take();
|
||||
}
|
||||
|
||||
@ -134,6 +148,9 @@ impl VisitMut for Remover {
|
||||
..
|
||||
}) if !arg.may_have_side_effects())) =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping side-effect-free expressions");
|
||||
}
|
||||
*e = *cond.cons.take();
|
||||
}
|
||||
|
||||
@ -169,6 +186,9 @@ impl VisitMut for Remover {
|
||||
}
|
||||
|
||||
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Removing dead branches");
|
||||
}
|
||||
self.fold_stmt_like(n)
|
||||
}
|
||||
|
||||
@ -191,6 +211,11 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!(
|
||||
"Dropping key-value pattern property because it's an empty object pattern"
|
||||
);
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
@ -200,6 +225,11 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!(
|
||||
"Dropping key-value pattern property because it's an empty array pattern"
|
||||
);
|
||||
}
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
@ -333,6 +363,10 @@ impl VisitMut for Remover {
|
||||
|
||||
let mut stmts = vec![];
|
||||
if let (p, Known(v)) = test.as_bool() {
|
||||
if cfg!(feature = "debug") {
|
||||
trace!("The condition for if statement is always {}", v);
|
||||
}
|
||||
|
||||
// Preserve effect of the test
|
||||
if !p.is_pure() {
|
||||
if let Some(expr) = ignore_result(*test).map(Box::new) {
|
||||
@ -360,6 +394,10 @@ impl VisitMut for Remover {
|
||||
return Stmt::Empty(EmptyStmt { span });
|
||||
}
|
||||
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Optimized an if statement with known condition");
|
||||
}
|
||||
|
||||
self.changed = true;
|
||||
|
||||
let mut block = Stmt::Block(BlockStmt { span, stmts });
|
||||
@ -395,10 +433,16 @@ impl VisitMut for Remover {
|
||||
}
|
||||
|
||||
Stmt::Decl(Decl::Var(v)) if v.decls.is_empty() => {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping an empty var declaration");
|
||||
}
|
||||
Stmt::Empty(EmptyStmt { span: v.span })
|
||||
}
|
||||
|
||||
Stmt::Labeled(LabeledStmt { span, body, .. }) if body.is_empty() => {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping an empty label statement");
|
||||
}
|
||||
Stmt::Empty(EmptyStmt { span })
|
||||
}
|
||||
|
||||
@ -412,6 +456,9 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping a label statement with instant break");
|
||||
}
|
||||
Stmt::Empty(EmptyStmt { span })
|
||||
}
|
||||
|
||||
@ -429,11 +476,19 @@ impl VisitMut for Remover {
|
||||
|
||||
Stmt::Block(BlockStmt { span, stmts }) => {
|
||||
if stmts.is_empty() {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Drooping an empty block statement");
|
||||
}
|
||||
|
||||
Stmt::Empty(EmptyStmt { span })
|
||||
} else if stmts.len() == 1
|
||||
&& !is_block_scoped_stuff(&stmts[0])
|
||||
&& stmt_depth(&stmts[0]) <= 1
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Optimizing a block statement with a single statement");
|
||||
}
|
||||
|
||||
let mut v = stmts.into_iter().next().unwrap();
|
||||
v.visit_mut_with(self);
|
||||
v
|
||||
@ -466,6 +521,10 @@ impl VisitMut for Remover {
|
||||
// If catch block is not specified and finally block is empty, fold it to simple
|
||||
// block.
|
||||
if handler.is_none() && finalizer.is_empty() {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Converting a try statement to a block statement");
|
||||
}
|
||||
|
||||
return Stmt::Block(block);
|
||||
}
|
||||
|
||||
@ -857,6 +916,10 @@ impl VisitMut for Remover {
|
||||
_ => false,
|
||||
} =>
|
||||
{
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Optimizing a for statement with a false test");
|
||||
}
|
||||
|
||||
let decl = s.body.extract_var_ids_as_var();
|
||||
let body = if let Some(var) = decl {
|
||||
Stmt::Decl(Decl::Var(var))
|
||||
@ -950,14 +1013,30 @@ impl VisitMut for Remover {
|
||||
|
||||
//
|
||||
match &v.name {
|
||||
Pat::Object(o) if o.props.is_empty() => None,
|
||||
Pat::Array(a) if a.elems.is_empty() => None,
|
||||
Pat::Object(o) if o.props.is_empty() => {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping an object pattern in a var declaration");
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
Pat::Array(a) if a.elems.is_empty() => {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping an array pattern in a var declaration");
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
_ => Some(v),
|
||||
}
|
||||
});
|
||||
|
||||
if decls.is_empty() {
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping a useless variable declaration");
|
||||
}
|
||||
|
||||
return Stmt::Empty(EmptyStmt { span: v.span });
|
||||
}
|
||||
|
||||
@ -1063,6 +1142,10 @@ impl Remover {
|
||||
new_stmts.extend(hoisted_fns);
|
||||
|
||||
*stmts = new_stmts;
|
||||
|
||||
if cfg!(feature = "debug") {
|
||||
debug!("Dropping statements after a control keyword");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user