-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(es/minifier): Enable
conditionals
by default (#4687)
- Loading branch information
Showing
40 changed files
with
454 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
/*.output.js | ||
/input.js | ||
*.orig | ||
input.new.js | ||
input.orig.js | ||
|
||
*.txt | ||
|
||
/.input | ||
/.compare | ||
|
||
# creduce | ||
creduce_bug* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use std::{ | ||
fs, | ||
path::{Path, PathBuf}, | ||
process::Command, | ||
sync::Arc, | ||
}; | ||
|
||
use anyhow::{Context, Result}; | ||
use clap::Args; | ||
use rayon::prelude::*; | ||
use swc_common::{SourceMap, GLOBALS}; | ||
use swc_ecma_minifier::option::{CompressOptions, MinifyOptions}; | ||
use swc_ecma_transforms_base::fixer::fixer; | ||
use swc_ecma_visit::VisitMutWith; | ||
use tracing::info; | ||
|
||
use crate::util::{all_js_files, make_pretty, parse_js, print_js}; | ||
|
||
/// Useful for checking if the minifier is working correctly, even with the new | ||
/// option. | ||
#[derive(Debug, Args)] | ||
pub struct DiffOptionCommand { | ||
pub path: PathBuf, | ||
|
||
#[clap(long)] | ||
pub open: bool, | ||
} | ||
|
||
impl DiffOptionCommand { | ||
pub fn run(self, cm: Arc<SourceMap>) -> Result<()> { | ||
let js_files = all_js_files(&self.path)?; | ||
|
||
let inputs = js_files | ||
.into_iter() | ||
.filter(|p| p.file_name() == Some("input.js".as_ref())) | ||
.collect::<Vec<_>>(); | ||
|
||
let files = GLOBALS.with(|globals| { | ||
inputs | ||
.into_par_iter() | ||
.map(|f| GLOBALS.set(globals, || self.process_file(cm.clone(), &f))) | ||
.collect::<Result<Vec<_>>>() | ||
})?; | ||
|
||
for (orig_path, new_path) in files.into_iter().flatten() { | ||
if self.open { | ||
let mut c = Command::new("code"); | ||
c.arg("--diff").arg("--wait"); | ||
c.arg(&orig_path); | ||
c.arg(&new_path); | ||
c.output().context("failed to run vscode")?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn process_file(&self, cm: Arc<SourceMap>, f: &Path) -> Result<Option<(PathBuf, PathBuf)>> { | ||
info!("Processing `{}`", f.display()); | ||
|
||
let fm = cm.load_file(f)?; | ||
let m = parse_js(fm)?; | ||
|
||
let orig = { | ||
let m = m.clone(); | ||
let mut m = swc_ecma_minifier::optimize( | ||
m.module, | ||
cm.clone(), | ||
Some(&m.comments), | ||
None, | ||
&MinifyOptions { | ||
compress: Some(Default::default()), | ||
mangle: None, | ||
..Default::default() | ||
}, | ||
&swc_ecma_minifier::option::ExtraOptions { | ||
unresolved_mark: m.unresolved_mark, | ||
top_level_mark: m.top_level_mark, | ||
}, | ||
); | ||
m.visit_mut_with(&mut fixer(None)); | ||
print_js(cm.clone(), &m, false)? | ||
}; | ||
|
||
let new = { | ||
let mut m = swc_ecma_minifier::optimize( | ||
m.module, | ||
cm.clone(), | ||
Some(&m.comments), | ||
None, | ||
&MinifyOptions { | ||
compress: Some(CompressOptions { | ||
conditionals: true, | ||
..Default::default() | ||
}), | ||
mangle: None, | ||
..Default::default() | ||
}, | ||
&swc_ecma_minifier::option::ExtraOptions { | ||
unresolved_mark: m.unresolved_mark, | ||
top_level_mark: m.top_level_mark, | ||
}, | ||
); | ||
m.visit_mut_with(&mut fixer(None)); | ||
print_js(cm, &m, false)? | ||
}; | ||
|
||
if orig == new { | ||
fs::remove_file(f)?; | ||
return Ok(None); | ||
} | ||
|
||
let orig_path = f.with_extension("orig.js"); | ||
fs::write(&orig_path, orig)?; | ||
make_pretty(&orig_path)?; | ||
let new_path = f.with_extension("new.js"); | ||
fs::write(&new_path, new)?; | ||
make_pretty(&new_path)?; | ||
|
||
Ok(Some((orig_path, new_path))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
foo({ | ||
bar: function(data, baz) { | ||
!(!(baz ? data.quxA : data.quxB) && !(baz ? data.corgeA : data.corgeB) && (baz ? data.get("waldo") : data.waldo)) ? (baz ? data.quxA : data.quxB) || (baz ? data.get("waldo") : data.waldo) || (baz ? !data.corgeA : !data.corgeB) || pass() : pass(); | ||
!(!(baz ? data.quxA : data.quxB) && !(baz ? data.corgeA : data.corgeB) && (baz ? data.get("waldo") : data.waldo)) && ((baz ? data.quxA : data.quxB) || (baz ? data.get("waldo") : data.waldo) || (baz ? !data.corgeA : !data.corgeB)) || pass(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
crates/swc/tests/vercel/full-compact/react-autosuggest/1/output/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c01476d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
es/full/minify/libraries/antd
2486276202
ns/iter (± 38932727
)2551286039
ns/iter (± 44008653
)0.97
es/full/minify/libraries/d3
809098180
ns/iter (± 10280469
)832354710
ns/iter (± 19444260
)0.97
es/full/minify/libraries/echarts
5123759512
ns/iter (± 78734500
)5190265639
ns/iter (± 36434341
)0.99
es/full/minify/libraries/jquery
126790155
ns/iter (± 2344981
)133526450
ns/iter (± 1449325
)0.95
es/full/minify/libraries/lodash
203205236
ns/iter (± 3422489
)208813964
ns/iter (± 2650426
)0.97
es/full/minify/libraries/moment
74996608
ns/iter (± 250651
)76908023
ns/iter (± 574234
)0.98
es/full/minify/libraries/react
23542459
ns/iter (± 406032
)23776073
ns/iter (± 176646
)0.99
es/full/minify/libraries/terser
589474826
ns/iter (± 9330612
)608335834
ns/iter (± 7926606
)0.97
es/full/minify/libraries/three
804049009
ns/iter (± 11266478
)926811652
ns/iter (± 16880900
)0.87
es/full/minify/libraries/typescript
6752645881
ns/iter (± 42588506
)7011257573
ns/iter (± 34107361
)0.96
es/full/minify/libraries/victory
988436202
ns/iter (± 23282483
)1075313439
ns/iter (± 14748155
)0.92
es/full/minify/libraries/vue
202579114
ns/iter (± 841494
)203196352
ns/iter (± 3054174
)1.00
es/full/codegen/es3
38381
ns/iter (± 482
)37749
ns/iter (± 522
)1.02
es/full/codegen/es5
38564
ns/iter (± 174
)37676
ns/iter (± 1202
)1.02
es/full/codegen/es2015
38532
ns/iter (± 220
)37126
ns/iter (± 617
)1.04
es/full/codegen/es2016
38436
ns/iter (± 259
)37884
ns/iter (± 243
)1.01
es/full/codegen/es2017
38516
ns/iter (± 190
)37879
ns/iter (± 317
)1.02
es/full/codegen/es2018
38445
ns/iter (± 290
)37616
ns/iter (± 618
)1.02
es/full/codegen/es2019
38445
ns/iter (± 219
)37202
ns/iter (± 670
)1.03
es/full/codegen/es2020
38561
ns/iter (± 199
)37545
ns/iter (± 579
)1.03
es/full/all/es3
241003624
ns/iter (± 5726783
)235370181
ns/iter (± 2200007
)1.02
es/full/all/es5
224570544
ns/iter (± 4666950
)224277941
ns/iter (± 4730444
)1.00
es/full/all/es2015
178014121
ns/iter (± 5030391
)176681686
ns/iter (± 5454013
)1.01
es/full/all/es2016
177579022
ns/iter (± 4736524
)173172683
ns/iter (± 2911662
)1.03
es/full/all/es2017
175012763
ns/iter (± 5979231
)172621706
ns/iter (± 3656000
)1.01
es/full/all/es2018
171645799
ns/iter (± 6248211
)171523709
ns/iter (± 2577599
)1.00
es/full/all/es2019
170847270
ns/iter (± 4950822
)168351712
ns/iter (± 3197115
)1.01
es/full/all/es2020
163627474
ns/iter (± 1954442
)162341438
ns/iter (± 5184088
)1.01
es/full/parser
755415
ns/iter (± 9611
)731062
ns/iter (± 24974
)1.03
es/full/base/fixer
49996
ns/iter (± 1144
)48704
ns/iter (± 1704
)1.03
es/full/base/resolver_and_hygiene
190467
ns/iter (± 1447
)179085
ns/iter (± 6828
)1.06
serialization of ast node
215
ns/iter (± 7
)203
ns/iter (± 7
)1.06
serialization of serde
213
ns/iter (± 0
)201
ns/iter (± 6
)1.06
This comment was automatically generated by workflow using github-action-benchmark.