Skip to content

Commit

Permalink
feat(es/analyzer): Extract the analyzer from the minifier to a separa…
Browse files Browse the repository at this point in the history
…te crate (#6586)
  • Loading branch information
alexkirsz committed Dec 7, 2022
1 parent da5e18e commit e1d01d8
Show file tree
Hide file tree
Showing 27 changed files with 633 additions and 562 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/swc_core/Cargo.toml
Expand Up @@ -146,6 +146,9 @@ ecma_minifier = ["__ecma", "swc_ecma_minifier"]
# Enable swc_ecma_preset_env
ecma_preset_env = ["__ecma", "swc_ecma_preset_env"]

# Enable swc_ecma_usage_analyzer
ecma_usage_analyzer = ["__ecma", "swc_ecma_usage_analyzer"]

# Enable swc_css
css_ast = ["__css", "swc_css_ast"]
css_codegen = ["__css", "swc_css_codegen"]
Expand Down Expand Up @@ -366,6 +369,7 @@ swc_ecma_transforms_proposal = { optional = true, version = "0.145.5", path
swc_ecma_transforms_react = { optional = true, version = "0.156.5", path = "../swc_ecma_transforms_react" }
swc_ecma_transforms_testing = { optional = true, version = "0.115.6", path = "../swc_ecma_transforms_testing" }
swc_ecma_transforms_typescript = { optional = true, version = "0.160.5", path = "../swc_ecma_transforms_typescript" }
swc_ecma_usage_analyzer = { optional = true, version = "0.1.0", path = "../swc_ecma_usage_analyzer" }
swc_ecma_utils = { optional = true, version = "0.106.5", path = "../swc_ecma_utils" }
swc_ecma_visit = { optional = true, version = "0.81.3", path = "../swc_ecma_visit" }
swc_node_base = { optional = true, version = "0.5.8", path = "../swc_node_base" }
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_core/src/lib.rs
Expand Up @@ -109,6 +109,12 @@ pub mod ecma {
pub use swc_ecma_preset_env::*;
}

#[cfg(feature = "ecma_usage_analyzer")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecma_usage_analyzer")))]
pub mod usage_analyzer {
pub use swc_ecma_usage_analyzer::*;
}

// visit* interfaces
#[cfg(feature = "__visit")]
#[cfg_attr(docsrs, doc(cfg(feature = "__visit")))]
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/Cargo.toml
Expand Up @@ -54,6 +54,7 @@ swc_ecma_codegen = { version = "0.128.5", path = "../swc_ecma_co
swc_ecma_parser = { version = "0.123.5", path = "../swc_ecma_parser" }
swc_ecma_transforms_base = { version = "0.112.5", path = "../swc_ecma_transforms_base" }
swc_ecma_transforms_optimization = { version = "0.168.5", path = "../swc_ecma_transforms_optimization" }
swc_ecma_usage_analyzer = { version = "0.1.0", path = "../swc_ecma_usage_analyzer" }
swc_ecma_utils = { version = "0.106.5", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "0.81.3", path = "../swc_ecma_visit" }
swc_timer = { version = "0.17.20", path = "../swc_timer" }
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_minifier/src/compress/hoist_decls.rs
Expand Up @@ -2,12 +2,13 @@
use rayon::prelude::*;
use swc_common::{collections::AHashSet, pass::Repeated, util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::analyzer::UsageAnalyzer;
use swc_ecma_utils::{find_pat_ids, StmtLike};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};

use super::util::drop_invalid_stmts;
use crate::{
analyzer::{ProgramData, UsageAnalyzer},
program_data::ProgramData,
util::{is_hoisted_var_decl_without_init, sort::is_sorted_by, IsModuleItem, ModuleItemExt},
};

Expand Down Expand Up @@ -45,7 +46,7 @@ impl Hoister<'_> {
fn handle_stmt_likes<T>(&mut self, stmts: &mut Vec<T>)
where
T: StmtLike + IsModuleItem + ModuleItemExt,
Vec<T>: for<'aa> VisitMutWith<Hoister<'aa>> + VisitWith<UsageAnalyzer>,
Vec<T>: for<'aa> VisitMutWith<Hoister<'aa>> + VisitWith<UsageAnalyzer<ProgramData>>,
{
stmts.visit_mut_children_with(self);
let len = stmts.len();
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_minifier/src/compress/mod.rs
Expand Up @@ -17,19 +17,19 @@ use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::simplify::{
dead_branch_remover, expr_simplifier, ExprSimplifierConfig,
};
use swc_ecma_usage_analyzer::{analyzer::UsageAnalyzer, marks::Marks};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
use swc_timer::timer;
use tracing::{debug, error};

pub(crate) use self::pure::{pure_optimizer, PureOptimizerConfig};
use self::{hoist_decls::DeclHoisterConfig, optimize::optimizer};
use crate::{
analyzer::{analyze, ModuleInfo, UsageAnalyzer},
compress::hoist_decls::decl_hoister,
debug::{dump, AssertValid},
marks::Marks,
mode::Mode,
option::CompressOptions,
program_data::{analyze, ModuleInfo, ProgramData},
util::{now, unit::CompileUnit},
};

Expand Down Expand Up @@ -100,7 +100,7 @@ where
fn optimize_unit_repeatedly<N>(&mut self, n: &mut N)
where
N: CompileUnit
+ VisitWith<UsageAnalyzer>
+ VisitWith<UsageAnalyzer<ProgramData>>
+ for<'aa> VisitMutWith<Compressor<'aa, M>>
+ VisitWith<AssertValid>,
{
Expand Down Expand Up @@ -147,7 +147,7 @@ where
fn optimize_unit<N>(&mut self, n: &mut N)
where
N: CompileUnit
+ VisitWith<UsageAnalyzer>
+ VisitWith<UsageAnalyzer<ProgramData>>
+ for<'aa> VisitMutWith<Compressor<'aa, M>>
+ VisitWith<AssertValid>,
{
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Expand Up @@ -2,15 +2,15 @@ use swc_atoms::js_word;
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::simplify::expr_simplifier;
use swc_ecma_usage_analyzer::alias::{collect_infects_from, AliasConfig};
use swc_ecma_utils::{class_has_side_effect, find_pat_ids, ExprExt};
use swc_ecma_visit::VisitMutWith;

use super::Optimizer;
use crate::{
alias::{collect_infects_from, AliasConfig},
analyzer::VarUsageInfo,
compress::optimize::util::is_valid_for_lhs,
mode::Mode,
program_data::VarUsageInfo,
util::{
idents_captured_by, idents_used_by, idents_used_by_ignoring_nested, size::SizeWithCtxt,
},
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Expand Up @@ -8,6 +8,7 @@ use swc_common::{
};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_usage_analyzer::{analyzer::UsageAnalyzer, marks::Marks};
use swc_ecma_utils::{
prepend_stmts, undefined, ExprCtx, ExprExt, ExprFactory, IsEmpty, ModuleItemLike, StmtLike,
Type, Value,
Expand All @@ -25,13 +26,12 @@ use super::util::{drop_invalid_stmts, is_fine_for_if_cons};
#[cfg(feature = "debug")]
use crate::debug::dump;
use crate::{
analyzer::{ModuleInfo, ProgramData, UsageAnalyzer},
compress::util::is_pure_undefined,
debug::AssertValid,
marks::Marks,
maybe_par,
mode::Mode,
option::CompressOptions,
program_data::{ModuleInfo, ProgramData},
util::{
contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt,
},
Expand Down Expand Up @@ -332,7 +332,7 @@ where
fn handle_stmt_likes<T>(&mut self, stmts: &mut Vec<T>)
where
T: StmtLike + ModuleItemLike + ModuleItemExt + VisitMutWith<Self> + VisitWith<AssertValid>,
Vec<T>: VisitMutWith<Self> + VisitWith<UsageAnalyzer> + VisitWith<AssertValid>,
Vec<T>: VisitMutWith<Self> + VisitWith<UsageAnalyzer<ProgramData>> + VisitWith<AssertValid>,
{
let mut use_asm = false;
let prepend_stmts = self.prepend_stmts.take();
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Expand Up @@ -3,6 +3,10 @@ use std::mem::take;
use swc_atoms::js_word;
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::{
alias::{collect_infects_from, AccessKind, AliasConfig},
util::is_global_var_with_pure_property_access,
};
use swc_ecma_utils::{
contains_arguments, contains_this_expr, prepend_stmts, undefined, ExprExt, IdentUsageFinder,
StmtLike,
Expand All @@ -15,17 +19,13 @@ use super::{is_pure_undefined, Optimizer};
#[cfg(feature = "debug")]
use crate::debug::dump;
use crate::{
alias::{collect_infects_from, AccessKind, AliasConfig},
compress::{
optimize::{unused::PropertyAccessOpts, util::replace_id_with_expr},
util::{is_directive, is_ident_used_by, replace_expr},
},
mode::Mode,
option::CompressOptions,
util::{
idents_used_by, idents_used_by_ignoring_nested, is_global_var_with_pure_property_access,
ExprOptExt, ModuleItemExt,
},
util::{idents_used_by, idents_used_by_ignoring_nested, ExprOptExt, ModuleItemExt},
};

/// Methods related to the option `sequences`. All methods are noop if
Expand Down Expand Up @@ -1170,8 +1170,8 @@ where
.expand_infected(self.module_info, ids_used_by_a_init, 64);

let deps = match deps {
Ok(v) => v,
Err(()) => return false,
Some(v) => v,
_ => return false,
};
if deps.contains(&(e.to_id(), AccessKind::Reference))
|| deps.contains(&(e.to_id(), AccessKind::Call))
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/unused.rs
@@ -1,14 +1,14 @@
use swc_atoms::js_word;
use swc_common::{util::take::Take, Span, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::util::is_global_var_with_pure_property_access;
use swc_ecma_utils::contains_ident_ref;

use super::Optimizer;
#[cfg(feature = "debug")]
use crate::debug::dump;
use crate::{
compress::optimize::util::extract_class_side_effect, mode::Mode, option::PureGetterOption,
util::is_global_var_with_pure_property_access,
};

#[derive(Debug, Default, Clone, Copy)]
Expand Down
11 changes: 4 additions & 7 deletions crates/swc_ecma_minifier/src/compress/pure/misc.rs
Expand Up @@ -4,20 +4,17 @@ use swc_atoms::js_word;
use swc_common::{iter::IdentifyLast, util::take::Take, Span, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_usage_analyzer::util::is_global_var_with_pure_property_access;
use swc_ecma_utils::{
ExprExt, ExprFactory, IdentUsageFinder, Type,
Value::{self, Known},
};

use super::Pure;
use crate::{
compress::{
pure::strings::{convert_str_value_to_tpl_cooked, convert_str_value_to_tpl_raw},
util::is_pure_undefined,
},
util::is_global_var_with_pure_property_access,
use crate::compress::{
pure::strings::{convert_str_value_to_tpl_cooked, convert_str_value_to_tpl_raw},
util::is_pure_undefined,
};

impl Pure<'_> {
pub(super) fn remove_invalid(&mut self, e: &mut Expr) {
match e {
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/mod.rs
Expand Up @@ -5,6 +5,7 @@ use rayon::prelude::*;
use swc_common::{pass::Repeated, util::take::Take, SyntaxContext, DUMMY_SP, GLOBALS};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_utils::{undefined, ExprCtx};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
#[cfg(feature = "debug")]
Expand All @@ -14,7 +15,7 @@ use self::{ctx::Ctx, misc::DropOpts};
#[cfg(feature = "debug")]
use crate::debug::dump;
use crate::{
analyzer::ProgramData, debug::AssertValid, marks::Marks, maybe_par, option::CompressOptions,
debug::AssertValid, maybe_par, option::CompressOptions, program_data::ProgramData,
util::ModuleItemExt,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/eval.rs
Expand Up @@ -5,12 +5,12 @@ use swc_atoms::js_word;
use swc_common::{collections::AHashMap, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::simplify::{expr_simplifier, ExprSimplifierConfig};
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_utils::{undefined, ExprCtx, ExprExt};
use swc_ecma_visit::VisitMutWith;

use crate::{
compress::{compressor, pure_optimizer, PureOptimizerConfig},
marks::Marks,
mode::Mode,
};

Expand Down
12 changes: 7 additions & 5 deletions crates/swc_ecma_minifier/src/lib.rs
Expand Up @@ -41,14 +41,13 @@ use once_cell::sync::Lazy;
use swc_common::{comments::Comments, pass::Repeated, sync::Lrc, SourceMap, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_visit::VisitMutWith;
use swc_timer::timer;

pub use crate::pass::unique_scope::unique_scope;
use crate::{
analyzer::ModuleInfo,
compress::{compressor, pure_optimizer, PureOptimizerConfig},
marks::Marks,
metadata::info_marker,
mode::{Minification, Mode},
option::{CompressOptions, ExtraOptions, MinifyOptions},
Expand All @@ -61,25 +60,28 @@ use crate::{
postcompress::postcompress_optimizer,
precompress::precompress_optimizer,
},
program_data::ModuleInfo,
timing::Timings,
util::base54::CharFreq,
};

#[macro_use]
mod macros;
mod alias;
mod analyzer;
mod compress;
mod debug;
pub mod eval;
pub mod marks;
mod metadata;
mod mode;
pub mod option;
mod pass;
mod program_data;
pub mod timing;
mod util;

pub mod marks {
pub use swc_ecma_usage_analyzer::marks::Marks;
}

const DISABLE_BUGGY_PASSES: bool = true;

pub(crate) static CPU_COUNT: Lazy<usize> = Lazy::new(num_cpus::get);
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/metadata/mod.rs
Expand Up @@ -3,12 +3,13 @@ use swc_common::{
EqIgnoreSpan, Span, SyntaxContext,
};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith,
};

use crate::{marks::Marks, option::CompressOptions};
use crate::option::CompressOptions;

#[cfg(test)]
mod tests;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/pass/mangle_props.rs
Expand Up @@ -10,8 +10,8 @@ use swc_ecma_ast::{
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

use crate::{
analyzer::{analyze, ModuleInfo, ProgramData},
option::ManglePropertiesOptions,
program_data::{analyze, ModuleInfo, ProgramData},
util::base54::Base54Chars,
};

Expand Down

0 comments on commit e1d01d8

Please sign in to comment.