Skip to content

Commit

Permalink
feat(es/minifier): Support mangle.eval (#7777)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #7754
  • Loading branch information
kdy1 authored Aug 9, 2023
1 parent 0edc420 commit eff0cac
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/benches/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn run(src: &str) {
keep_private_props: false,
ie8: false,
safari10: false,
reserved: Default::default(),
..Default::default()
}),
wrap: false,
enclose: false,
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/option/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct MangleOptions {

#[serde(default, alias = "reserved")]
pub reserved: Vec<JsWord>,

/// mangle names visible in scopes where eval or with are used
#[serde(default)]
pub eval: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, Merge)]
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub(crate) fn name_mangler(
swc_ecma_transforms_base::hygiene::Config {
keep_class_names: options.keep_class_names,
safari_10: options.safari10,
top_level_mark
top_level_mark,
ignore_eval: options.eval,
},
ManglingRenamer { chars, preserved }
)
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7754/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const foo = 1;
console.log(foo);

eval(`console.log(foo)`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"toplevel": true,
"eval": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const o = 1;
console.log(o), eval("console.log(foo)");
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7754/2/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const foo = 1;
console.log(foo);

eval(`console.log(foo)`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"toplevel": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
console.log(foo), eval("console.log(foo)");
3 changes: 3 additions & 0 deletions crates/swc_ecma_transforms_base/src/hygiene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct Config {
/// The marks derived from this marks will treated as `specified by user`
/// and other marks will be treated as `generated by swc`.
pub top_level_mark: Mark,

/// Mangle even if vars are visible to `eval` or `with`.
pub ignore_eval: bool,
}

/// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ macro_rules! unit {
($name:ident, $T:ty) => {
/// Only called if `eval` exists
fn $name(&mut self, n: &mut $T) {
if contains_eval(n, true) {
if !self.config.ignore_eval && contains_eval(n, true) {
n.visit_mut_children_with(self);
} else {
let map = self.get_map(n, false, false, false);
Expand All @@ -199,7 +199,7 @@ macro_rules! unit {
($name:ident, $T:ty, true) => {
/// Only called if `eval` exists
fn $name(&mut self, n: &mut $T) {
if contains_eval(n, true) {
if !self.config.ignore_eval && contains_eval(n, true) {
n.visit_mut_children_with(self);
} else {
let map = self.get_map(n, true, false, false);
Expand Down Expand Up @@ -239,7 +239,7 @@ where
fn visit_mut_module(&mut self, m: &mut Module) {
self.preserved = self.renamer.preserved_ids_for_module(m);

let has_eval = contains_eval(m, true);
let has_eval = !self.config.ignore_eval && contains_eval(m, true);

self.unresolved = self.get_unresolved(m, has_eval);

Expand Down Expand Up @@ -276,7 +276,7 @@ where
fn visit_mut_script(&mut self, m: &mut Script) {
self.preserved = self.renamer.preserved_ids_for_script(m);

let has_eval = contains_eval(m, true);
let has_eval = !self.config.ignore_eval && contains_eval(m, true);

self.unresolved = self.get_unresolved(m, has_eval);

Expand Down

1 comment on commit eff0cac

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: eff0cac Previous: ef8d121 Ratio
es/full/bugs-1 294983 ns/iter (± 10736) 299712 ns/iter (± 11532) 0.98
es/full/minify/libraries/antd 1393451284 ns/iter (± 12355364) 1394631089 ns/iter (± 18613016) 1.00
es/full/minify/libraries/d3 294944366 ns/iter (± 7342342) 302419949 ns/iter (± 5095944) 0.98
es/full/minify/libraries/echarts 1106663308 ns/iter (± 24141764) 1137580764 ns/iter (± 24783375) 0.97
es/full/minify/libraries/jquery 85503274 ns/iter (± 716073) 87629996 ns/iter (± 982207) 0.98
es/full/minify/libraries/lodash 100659215 ns/iter (± 918345) 102063730 ns/iter (± 1767861) 0.99
es/full/minify/libraries/moment 50414762 ns/iter (± 537849) 51367819 ns/iter (± 629236) 0.98
es/full/minify/libraries/react 18172136 ns/iter (± 122945) 18456412 ns/iter (± 165722) 0.98
es/full/minify/libraries/terser 229593321 ns/iter (± 5006638) 234136784 ns/iter (± 4056186) 0.98
es/full/minify/libraries/three 408699446 ns/iter (± 9219747) 413849425 ns/iter (± 6739863) 0.99
es/full/minify/libraries/typescript 2747289997 ns/iter (± 21911913) 2822062594 ns/iter (± 19076403) 0.97
es/full/minify/libraries/victory 590590452 ns/iter (± 13457291) 617819051 ns/iter (± 18509842) 0.96
es/full/minify/libraries/vue 123570004 ns/iter (± 779407) 126742550 ns/iter (± 2980845) 0.97
es/full/codegen/es3 34070 ns/iter (± 59) 33559 ns/iter (± 81) 1.02
es/full/codegen/es5 34039 ns/iter (± 51) 33453 ns/iter (± 75) 1.02
es/full/codegen/es2015 33970 ns/iter (± 108) 33416 ns/iter (± 48) 1.02
es/full/codegen/es2016 33982 ns/iter (± 75) 33414 ns/iter (± 107) 1.02
es/full/codegen/es2017 34028 ns/iter (± 88) 33483 ns/iter (± 91) 1.02
es/full/codegen/es2018 34018 ns/iter (± 106) 33459 ns/iter (± 75) 1.02
es/full/codegen/es2019 34051 ns/iter (± 80) 33461 ns/iter (± 58) 1.02
es/full/codegen/es2020 34087 ns/iter (± 77) 33459 ns/iter (± 92) 1.02
es/full/all/es3 171898682 ns/iter (± 607742) 172276978 ns/iter (± 1487369) 1.00
es/full/all/es5 164384543 ns/iter (± 1577156) 165624013 ns/iter (± 1086043) 0.99
es/full/all/es2015 123491325 ns/iter (± 447676) 123850732 ns/iter (± 703935) 1.00
es/full/all/es2016 122848446 ns/iter (± 1287123) 123298297 ns/iter (± 723889) 1.00
es/full/all/es2017 122586959 ns/iter (± 728454) 121849202 ns/iter (± 865918) 1.01
es/full/all/es2018 120143891 ns/iter (± 651318) 120433284 ns/iter (± 772849) 1.00
es/full/all/es2019 119627782 ns/iter (± 1547531) 119539674 ns/iter (± 1004071) 1.00
es/full/all/es2020 115429026 ns/iter (± 451329) 114590260 ns/iter (± 642664) 1.01
es/full/parser 536594 ns/iter (± 5615) 531729 ns/iter (± 4949) 1.01
es/full/base/fixer 18715 ns/iter (± 1036) 20523 ns/iter (± 302) 0.91
es/full/base/resolver_and_hygiene 80956 ns/iter (± 357) 82519 ns/iter (± 186) 0.98
serialization of serde 293 ns/iter (± 4) 290 ns/iter (± 0) 1.01
css/minify/libraries/bootstrap 29355574 ns/iter (± 77669) 30058238 ns/iter (± 361468) 0.98
css/visitor/compare/clone 1635441 ns/iter (± 2344) 1644864 ns/iter (± 6686) 0.99
css/visitor/compare/visit_mut_span 1758759 ns/iter (± 1752) 1781529 ns/iter (± 10874) 0.99
css/visitor/compare/visit_mut_span_panic 1850422 ns/iter (± 5631) 1852683 ns/iter (± 4170) 1.00
css/visitor/compare/fold_span 2591087 ns/iter (± 12405) 2615145 ns/iter (± 16861) 0.99
css/visitor/compare/fold_span_panic 2761411 ns/iter (± 10650) 2803104 ns/iter (± 34289) 0.99
css/lexer/bootstrap_5_1_3 4458079 ns/iter (± 4684) 4554118 ns/iter (± 5067) 0.98
css/lexer/foundation_6_7_4 3756489 ns/iter (± 1995) 3835552 ns/iter (± 3004) 0.98
css/lexer/tailwind_3_1_1 713534 ns/iter (± 532) 729151 ns/iter (± 324) 0.98
css/parser/bootstrap_5_1_3 19735381 ns/iter (± 51781) 19473371 ns/iter (± 121937) 1.01
css/parser/foundation_6_7_4 15985505 ns/iter (± 19095) 15343105 ns/iter (± 941742) 1.04
css/parser/tailwind_3_1_1 3060712 ns/iter (± 5656) 2931218 ns/iter (± 39326) 1.04
es/codegen/colors 731655 ns/iter (± 395973) 722797 ns/iter (± 397033) 1.01
es/codegen/large 2889574 ns/iter (± 1531028) 2918435 ns/iter (± 1541553) 0.99
es/codegen/with-parser/colors 45014 ns/iter (± 128) 45478 ns/iter (± 620) 0.99
es/codegen/with-parser/large 484317 ns/iter (± 675) 489493 ns/iter (± 1355) 0.99
es/minify/libraries/antd 1153314101 ns/iter (± 9359004) 1242606014 ns/iter (± 25169138) 0.93
es/minify/libraries/d3 242730983 ns/iter (± 575532) 255780279 ns/iter (± 6807460) 0.95
es/minify/libraries/echarts 906891977 ns/iter (± 2527853) 996370504 ns/iter (± 23538535) 0.91
es/minify/libraries/jquery 74745881 ns/iter (± 132608) 75932903 ns/iter (± 555420) 0.98
es/minify/libraries/lodash 88935000 ns/iter (± 294006) 90428196 ns/iter (± 399630) 0.98
es/minify/libraries/moment 44254677 ns/iter (± 42933) 44681947 ns/iter (± 388386) 0.99
es/minify/libraries/react 16164488 ns/iter (± 35719) 16354682 ns/iter (± 156272) 0.99
es/minify/libraries/terser 190080754 ns/iter (± 390454) 195367956 ns/iter (± 1864302) 0.97
es/minify/libraries/three 322999123 ns/iter (± 506817) 342422021 ns/iter (± 5546330) 0.94
es/minify/libraries/typescript 2277740778 ns/iter (± 8549469) 2377877713 ns/iter (± 20813513) 0.96
es/minify/libraries/victory 480413938 ns/iter (± 1843816) 514082512 ns/iter (± 6690608) 0.93
es/minify/libraries/vue 108209962 ns/iter (± 95334) 109985116 ns/iter (± 1103594) 0.98
es/visitor/compare/clone 1947301 ns/iter (± 4848) 1987662 ns/iter (± 9582) 0.98
es/visitor/compare/visit_mut_span 2294285 ns/iter (± 4182) 2335438 ns/iter (± 7790) 0.98
es/visitor/compare/visit_mut_span_panic 2324281 ns/iter (± 5195) 2367441 ns/iter (± 28112) 0.98
es/visitor/compare/fold_span 3352974 ns/iter (± 6416) 3411996 ns/iter (± 25372) 0.98
es/visitor/compare/fold_span_panic 3472470 ns/iter (± 6210) 3536757 ns/iter (± 44235) 0.98
es/lexer/colors 13173 ns/iter (± 49) 12865 ns/iter (± 17) 1.02
es/lexer/angular 6141500 ns/iter (± 6791) 6004294 ns/iter (± 6027) 1.02
es/lexer/backbone 803811 ns/iter (± 1508) 770668 ns/iter (± 2712) 1.04
es/lexer/jquery 4545671 ns/iter (± 2193) 4375412 ns/iter (± 4591) 1.04
es/lexer/jquery mobile 6949075 ns/iter (± 3419) 6707206 ns/iter (± 4530) 1.04
es/lexer/mootools 3606903 ns/iter (± 2577) 3464410 ns/iter (± 4578) 1.04
es/lexer/underscore 684498 ns/iter (± 812) 647259 ns/iter (± 648) 1.06
es/lexer/three 21557565 ns/iter (± 90565) 20826154 ns/iter (± 33028) 1.04
es/lexer/yui 3880218 ns/iter (± 4263) 3756131 ns/iter (± 1188) 1.03
es/parser/colors 27754 ns/iter (± 32) 27219 ns/iter (± 54) 1.02
es/parser/angular 13587234 ns/iter (± 67998) 13908487 ns/iter (± 162043) 0.98
es/parser/backbone 2003605 ns/iter (± 9224) 2023335 ns/iter (± 4014) 0.99
es/parser/jquery 10983409 ns/iter (± 50318) 11300653 ns/iter (± 269748) 0.97
es/parser/jquery mobile 16870868 ns/iter (± 152349) 17344700 ns/iter (± 359685) 0.97
es/parser/mootools 8438775 ns/iter (± 16159) 8545234 ns/iter (± 67523) 0.99
es/parser/underscore 1730043 ns/iter (± 8135) 1741986 ns/iter (± 11420) 0.99
es/parser/three 46910176 ns/iter (± 182108) 49239028 ns/iter (± 732146) 0.95
es/parser/yui 8350564 ns/iter (± 26998) 8453282 ns/iter (± 99648) 0.99
es/preset-env/usage/builtin_type 138109 ns/iter (± 31782) 138407 ns/iter (± 33241) 1.00
es/preset-env/usage/property 16959 ns/iter (± 121) 16677 ns/iter (± 73) 1.02
es/resolver/typescript 87530002 ns/iter (± 1201609) 90534643 ns/iter (± 1558133) 0.97
es/fixer/typescript 61935767 ns/iter (± 375114) 64557092 ns/iter (± 841588) 0.96
es/hygiene/typescript 129637016 ns/iter (± 858790) 135372924 ns/iter (± 1688939) 0.96
es/resolver_with_hygiene/typescript 239371918 ns/iter (± 1210757) 252522879 ns/iter (± 4801228) 0.95
es/visitor/base-perf/module_clone 58697 ns/iter (± 105) 58970 ns/iter (± 323) 1.00
es/visitor/base-perf/fold_empty 62015 ns/iter (± 388) 63315 ns/iter (± 797) 0.98
es/visitor/base-perf/fold_noop_impl_all 62755 ns/iter (± 201) 62726 ns/iter (± 491) 1.00
es/visitor/base-perf/fold_noop_impl_vec 62197 ns/iter (± 310) 63412 ns/iter (± 170) 0.98
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 0) 57 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 38 ns/iter (± 0) 40 ns/iter (± 0) 0.95
es/visitor/base-perf/boxing_boxed 106 ns/iter (± 0) 106 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 75 ns/iter (± 0) 75 ns/iter (± 0) 1
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2514 ns/iter (± 10) 2432 ns/iter (± 12) 1.03
es/base/parallel/resolver/typescript 3649089563 ns/iter (± 232440358) 3645814264 ns/iter (± 231872778) 1.00
es/base/parallel/hygiene/typescript 1460780479 ns/iter (± 24221190) 1458012924 ns/iter (± 17085985) 1.00
misc/visitors/time-complexity/time 5 133 ns/iter (± 1) 137 ns/iter (± 8) 0.97
misc/visitors/time-complexity/time 10 259 ns/iter (± 0) 372 ns/iter (± 8) 0.70
misc/visitors/time-complexity/time 15 500 ns/iter (± 2) 674 ns/iter (± 3) 0.74
misc/visitors/time-complexity/time 20 1090 ns/iter (± 23) 1052 ns/iter (± 28) 1.04
misc/visitors/time-complexity/time 40 3555 ns/iter (± 18) 3705 ns/iter (± 64) 0.96
misc/visitors/time-complexity/time 60 7352 ns/iter (± 96) 7715 ns/iter (± 64) 0.95
es/full-target/es2016 229875 ns/iter (± 330) 231134 ns/iter (± 810) 0.99
es/full-target/es2017 217448 ns/iter (± 981) 218523 ns/iter (± 461) 1.00
es/full-target/es2018 204979 ns/iter (± 682) 206198 ns/iter (± 523) 0.99
es2020_nullish_coalescing 68658 ns/iter (± 565) 71468 ns/iter (± 330) 0.96
es2020_optional_chaining 79088 ns/iter (± 255) 83657 ns/iter (± 370) 0.95
es2022_class_properties 113695 ns/iter (± 420) 114673 ns/iter (± 185) 0.99
es2018_object_rest_spread 73132 ns/iter (± 351) 75465 ns/iter (± 426) 0.97
es2019_optional_catch_binding 61842 ns/iter (± 288) 65574 ns/iter (± 244) 0.94
es2017_async_to_generator 62216 ns/iter (± 265) 65455 ns/iter (± 241) 0.95
es2016_exponentiation 66595 ns/iter (± 552) 69724 ns/iter (± 314) 0.96
es2015_arrow 69565 ns/iter (± 290) 73264 ns/iter (± 275) 0.95
es2015_block_scoped_fn 66215 ns/iter (± 214) 69774 ns/iter (± 263) 0.95
es2015_block_scoping 121310 ns/iter (± 414) 123642 ns/iter (± 671) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.