Skip to content

Commit

Permalink
refactor(ecma/minifier): code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 17, 2022
1 parent 00d7695 commit 8eecdf0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
42 changes: 21 additions & 21 deletions crates/swc_ecma_minifier/src/lib.rs
Expand Up @@ -87,7 +87,7 @@ pub(crate) static HEAVY_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT *
pub(crate) static LIGHT_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT * 100);

pub fn optimize(
mut m: Program,
mut n: Program,
_cm: Lrc<SourceMap>,
comments: Option<&dyn Comments>,
mut timings: Option<&mut Timings>,
Expand All @@ -99,7 +99,7 @@ pub fn optimize(
let mut marks = Marks::new();
marks.unresolved_mark = extra.unresolved_mark;

debug_assert_valid(&m);
debug_assert_valid(&n);

if let Some(defs) = options.compress.as_ref().map(|c| &c.global_defs) {
let _timer = timer!("inline global defs");
Expand All @@ -111,15 +111,15 @@ pub fn optimize(

if !defs.is_empty() {
let defs = defs.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
m.visit_mut_with(&mut global_defs::globals_defs(
n.visit_mut_with(&mut global_defs::globals_defs(
defs,
extra.unresolved_mark,
extra.top_level_mark,
));
}
}

let module_info = match &m {
let module_info = match &n {
Program::Script(_) => ModuleInfo::default(),
Program::Module(m) => ModuleInfo {
blackbox_imports: m
Expand Down Expand Up @@ -162,20 +162,20 @@ pub fn optimize(
if let Some(_options) = &options.compress {
let _timer = timer!("precompress");

m.visit_mut_with(&mut precompress_optimizer());
debug_assert_valid(&m);
n.visit_mut_with(&mut precompress_optimizer());
debug_assert_valid(&n);
}

if options.compress.is_some() {
m.visit_mut_with(&mut info_marker(
n.visit_mut_with(&mut info_marker(
options.compress.as_ref(),
comments,
marks,
extra.unresolved_mark,
));
debug_assert_valid(&m);
debug_assert_valid(&n);
}
m.visit_mut_with(&mut unique_scope());
n.visit_mut_with(&mut unique_scope());

if options.wrap {
// TODO: wrap_common_js
Expand All @@ -191,8 +191,8 @@ pub fn optimize(
}
if let Some(options) = &options.compress {
if options.unused {
perform_dce(&mut m, options, extra);
debug_assert_valid(&m);
perform_dce(&mut n, options, extra);
debug_assert_valid(&n);
}
}

Expand All @@ -207,7 +207,7 @@ pub fn optimize(
if options.rename && DISABLE_BUGGY_PASSES {
// toplevel.figure_out_scope(options.mangle);
// TODO: Pass `options.mangle` to name expander.
m.visit_mut_with(&mut name_expander());
n.visit_mut_with(&mut name_expander());
}

if let Some(ref mut t) = timings {
Expand All @@ -217,14 +217,14 @@ pub fn optimize(
{
let _timer = timer!("compress ast");

m.visit_mut_with(&mut compressor(&module_info, marks, options, &Minification))
n.visit_mut_with(&mut compressor(&module_info, marks, options, &Minification))
}

// Again, we don't need to validate ast

let _timer = timer!("postcompress");

m.visit_mut_with(&mut postcompress_optimizer(options));
n.visit_mut_with(&mut postcompress_optimizer(options));

let mut pass = 0;
loop {
Expand All @@ -241,7 +241,7 @@ pub fn optimize(
debug_infinite_loop: false,
},
);
m.visit_mut_with(&mut v);
n.visit_mut_with(&mut v);
if !v.changed() || options.passes <= pass {
break;
}
Expand All @@ -263,30 +263,30 @@ pub fn optimize(
let _timer = timer!("mangle names");
// TODO: base54.reset();

let preserved = idents_to_preserve(mangle.clone(), &m);
let preserved = idents_to_preserve(mangle.clone(), &n);

let chars = CharFreq::compute(
&m,
&n,
&preserved,
SyntaxContext::empty().apply_mark(marks.unresolved_mark),
)
.compile();

m.visit_mut_with(&mut name_mangler(mangle.clone(), preserved, chars));
n.visit_mut_with(&mut name_mangler(mangle.clone(), preserved, chars));

if let Some(property_mangle_options) = &mangle.props {
mangle_properties(&mut m, &module_info, property_mangle_options.clone(), chars);
mangle_properties(&mut n, &module_info, property_mangle_options.clone(), chars);
}
}

m.visit_mut_with(&mut merge_exports());
n.visit_mut_with(&mut merge_exports());

if let Some(ref mut t) = timings {
t.section("hygiene");
t.end_section();
}

m
n
}

fn perform_dce(m: &mut Program, options: &CompressOptions, extra: &ExtraOptions) {
Expand Down
9 changes: 4 additions & 5 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Expand Up @@ -242,12 +242,11 @@ where

if contains_eval(s, true) {
s.visit_mut_children_with(self);
return;
}

let map = self.get_map(s, false, true);
} else {
let map = self.get_map(s, false, true);

s.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
s.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}
}

Expand Down

0 comments on commit 8eecdf0

Please sign in to comment.