Skip to content

Commit 1dcdbbc

Browse files
authoredMar 17, 2025··
refactor(es/minifier): Move some deps to dev deps (#10216)
**Description:** I made a mistake. This PR also adds a size test for the `swc` crate, to ensure that the issue described in privatenumber/minification-benchmarks#664 is not our fault.
1 parent 80ccd86 commit 1dcdbbc

File tree

9 files changed

+183
-4
lines changed

9 files changed

+183
-4
lines changed
 

‎.changeset/moody-mangos-decide.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc_ecma_compat_es2015: patch
3+
swc_ecma_minifier: patch
4+
swc_core: patch
5+
---
6+
7+
refactor(es/minifier): Move some deps to dev deps

‎Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+11-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ resolver = "2"
5353
glob = "0.3.0"
5454
hashbrown = "0.14.5"
5555
hex = "0.4.3"
56+
humansize = "2.1.3"
5657
indexmap = "2.0.0"
5758
is-macro = "0.3.5"
5859
js-sys = "0.3.59"
@@ -129,7 +130,6 @@ resolver = "2"
129130
wasmer = { version = "=5.0.5-rc1", default-features = false }
130131
wasmer-wasix = { version = "0.35.0", default-features = false }
131132
wide = "0.7.32"
132-
humansize = "2.1.3"
133133

134134
[profile.release]
135135
lto = true
@@ -150,3 +150,13 @@ opt-level = 3
150150

151151
[profile.dev.package."*"]
152152
opt-level = 3
153+
154+
155+
[profile.test.package.swc_ecma_minifier]
156+
opt-level = 3
157+
158+
[profile.test.package.swc_ecma_transforms_optimization]
159+
opt-level = 3
160+
161+
[profile.test.package.swc_ecma_usage_analyzer]
162+
opt-level = 3

‎crates/swc/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ swc_visit = { version = "2.0.0", path = "../swc_visit" }
130130
[dev-dependencies]
131131
ansi_term = { workspace = true }
132132
criterion = { workspace = true }
133+
flate2 = { workspace = true }
134+
humansize = { workspace = true }
133135
rayon = { workspace = true }
134136
walkdir = { workspace = true }
135137

138+
136139
codspeed-criterion-compat = { workspace = true }
137140
swc_ecma_ast = { version = "8.0.0", path = "../swc_ecma_ast", features = [
138141
"serde-impl",
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
| File | Original Size | Compressed Size | Gzipped Size |
2+
| --- | --- | --- | --- |
3+
| antd.js | 6.38 MiB | 2.05 MiB | 444.65 KiB |
4+
| d3.js | 542.74 KiB | 259.09 KiB | 85.07 KiB |
5+
| echarts.js | 3.41 MiB | 971.28 KiB | 313.35 KiB |
6+
| jquery.js | 280.89 KiB | 87.07 KiB | 30.17 KiB |
7+
| lodash.js | 531.35 KiB | 68.27 KiB | 24.49 KiB |
8+
| moment.js | 169.83 KiB | 56.90 KiB | 18.18 KiB |
9+
| react.js | 70.45 KiB | 22.33 KiB | 7.99 KiB |
10+
| terser.js | 1.08 MiB | 444.77 KiB | 120.27 KiB |
11+
| three.js | 1.19 MiB | 628.06 KiB | 154.56 KiB |
12+
| typescript.js | 10.45 MiB | 3.17 MiB | 843.24 KiB |
13+
| victory.js | 2.30 MiB | 690.95 KiB | 153.66 KiB |
14+
| vue.js | 334.13 KiB | 113.02 KiB | 41.67 KiB |

‎crates/swc/tests/size.rs

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use std::{
2+
fmt::Write,
3+
path::{Path, PathBuf},
4+
};
5+
6+
use flate2::{write::GzEncoder, Compression};
7+
use humansize::format_size;
8+
use indexmap::IndexMap;
9+
use rustc_hash::FxBuildHasher;
10+
use swc::{config::JsMinifyOptions, BoolOrDataConfig, Compiler};
11+
use swc_common::{FileName, GLOBALS};
12+
use swc_ecma_utils::parallel::{Parallel, ParallelExt};
13+
use testing::NormalizedOutput;
14+
use walkdir::WalkDir;
15+
16+
struct FileSize {
17+
original_size: usize,
18+
compressed_size: usize,
19+
gzipped_size: usize,
20+
}
21+
22+
#[test]
23+
fn bench_libs() {
24+
let dir = PathBuf::from("../swc_ecma_minifier/benches/full");
25+
26+
let files = expand_dirs(&dir);
27+
28+
let result = calc_size_par(&files);
29+
let output = format_result(result);
30+
31+
NormalizedOutput::from(output)
32+
.compare_to_file("tests/libs-size.snapshot.md")
33+
.unwrap();
34+
}
35+
36+
fn format_result(mut result: IndexMap<String, FileSize, FxBuildHasher>) -> String {
37+
let mut output = String::new();
38+
39+
// Sort by file name
40+
result.sort_by_cached_key(|file_name, _| file_name.clone());
41+
42+
writeln!(
43+
output,
44+
"| File | Original Size | Compressed Size | Gzipped Size |"
45+
)
46+
.unwrap();
47+
writeln!(output, "| --- | --- | --- | --- |").unwrap();
48+
49+
for (file_name, file_size) in result {
50+
writeln!(
51+
output,
52+
"| {} | {} | {} | {} |",
53+
file_name,
54+
format_size(file_size.original_size, humansize::BINARY),
55+
format_size(file_size.compressed_size, humansize::BINARY),
56+
format_size(file_size.gzipped_size, humansize::BINARY)
57+
)
58+
.unwrap();
59+
}
60+
61+
output
62+
}
63+
64+
fn calc_size_par(files: &[PathBuf]) -> IndexMap<String, FileSize, FxBuildHasher> {
65+
GLOBALS.set(&Default::default(), || {
66+
let mut worker = Worker::default();
67+
68+
worker.maybe_par(0, files, |worker, path| {
69+
let file_name = path.file_name().unwrap().to_str().unwrap();
70+
let src = std::fs::read_to_string(path).unwrap();
71+
let file_size = run(&src);
72+
73+
worker.result.insert(file_name.to_string(), file_size);
74+
});
75+
76+
worker.result
77+
})
78+
}
79+
80+
/// Return the whole input files as abolute path.
81+
fn expand_dirs(dir: &Path) -> Vec<PathBuf> {
82+
WalkDir::new(dir)
83+
.into_iter()
84+
.filter_map(Result::ok)
85+
.filter_map(|entry| {
86+
if entry.metadata().map(|v| v.is_file()).unwrap_or(false) {
87+
Some(entry.into_path())
88+
} else {
89+
None
90+
}
91+
})
92+
.filter(|path| path.extension().map(|ext| ext == "js").unwrap_or(false))
93+
.collect::<Vec<_>>()
94+
}
95+
96+
#[derive(Default)]
97+
struct Worker {
98+
result: IndexMap<String, FileSize, FxBuildHasher>,
99+
}
100+
101+
impl Parallel for Worker {
102+
fn create(&self) -> Self {
103+
Worker {
104+
..Default::default()
105+
}
106+
}
107+
108+
fn merge(&mut self, other: Self) {
109+
self.result.extend(other.result);
110+
}
111+
}
112+
113+
fn run(src: &str) -> FileSize {
114+
testing::run_test2(false, |cm, handler| {
115+
let c = Compiler::new(cm.clone());
116+
let fm = cm.new_source_file(FileName::Anon.into(), src.into());
117+
118+
let mut config = JsMinifyOptions::default();
119+
config.format.comments = BoolOrDataConfig::from_bool(false);
120+
121+
let output = c.minify(fm, &handler, &config, Default::default()).unwrap();
122+
123+
let code = output.code;
124+
let gzipped_size = gzip_size(code.as_bytes());
125+
126+
Ok(FileSize {
127+
original_size: src.len(),
128+
compressed_size: code.len(),
129+
gzipped_size,
130+
})
131+
})
132+
.unwrap()
133+
}
134+
135+
fn gzip_size(data: &[u8]) -> usize {
136+
use std::io::Write;
137+
138+
let mut encoder = GzEncoder::new(Vec::new(), Compression::best());
139+
encoder.write_all(data).unwrap();
140+
encoder.finish().unwrap().len()
141+
}

‎crates/swc_ecma_compat_es2015/src/generator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,7 @@ impl Generator {
22122212
self.block_stack = Some(Default::default());
22132213
}
22142214

2215+
#[cfg(debug_assertions)]
22152216
let index = self.block_actions.as_ref().unwrap().len();
22162217

22172218
#[cfg(debug_assertions)]
@@ -2236,6 +2237,7 @@ impl Generator {
22362237
fn end_block(&mut self) -> Ptr<CodeBlock> {
22372238
let block = self.peek_block().expect("beginBlock was never called.");
22382239

2240+
#[cfg(debug_assertions)]
22392241
let index = self.block_actions.as_ref().unwrap().len();
22402242

22412243
#[cfg(debug_assertions)]

‎crates/swc_ecma_minifier/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ trace-ast = []
3535
[dependencies]
3636
arrayvec = { workspace = true }
3737
backtrace = { workspace = true, optional = true }
38-
flate2 = { workspace = true }
39-
humansize = { workspace = true }
4038
indexmap = { workspace = true }
4139
num-bigint = { workspace = true }
4240
num_cpus = { workspace = true }
@@ -77,6 +75,8 @@ swc_timer = { version = "1.0.0", path = "../swc_timer" }
7775
ansi_term = { workspace = true }
7876
anyhow = { workspace = true }
7977
criterion = { workspace = true }
78+
flate2 = { workspace = true }
79+
humansize = { workspace = true }
8080
pretty_assertions = { workspace = true }
8181
walkdir = { workspace = true }
8282

‎crates/swc_ecma_minifier/scripts/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export SWC_RUN=0
88

99
touch tests/compress.rs
1010

11-
UPDATE=1 cargo test -p swc_ecma_minifier --release --test size &
11+
UPDATE=1 cargo test -p swc_ecma_minifier --test size &
1212
cargo test -p swc_ecma_minifier -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@
1313

1414
# find ../swc/tests/ -type f -empty -delete

0 commit comments

Comments
 (0)
Please sign in to comment.