Skip to content

Commit cd90549

Browse files
committed
feat(tasks/minsize): generate a quick diff for inspection (#12238)
1 parent 30e8690 commit cd90549

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

justfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,15 @@ website path:
222222
cargo run -p website -- linter-rules --table {{path}}/src/docs/guide/usage/linter/generated-rules.md --rule-docs {{path}}/src/docs/guide/usage/linter/rules --git-ref $(git rev-parse HEAD)
223223
cargo run -p website -- linter-cli > {{path}}/src/docs/guide/usage/linter/generated-cli.md
224224
cargo run -p website -- linter-schema-markdown > {{path}}/src/docs/guide/usage/linter/generated-config.md
225+
226+
minifier-diff:
227+
#!/usr/bin/env bash
228+
cargo minsize --compress-only pr
229+
git checkout main
230+
cargo minsize --compress-only main
231+
for file in antd bundle.min d3 echarts jquery lodash moment react.development three typescript victory vue
232+
do
233+
echo $file.js >> diff
234+
diff target/minifier/main/$file.js target/minifier/pr/$file.js >> diff
235+
done
236+
git checkout -

tasks/minsize/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ oxc_minifier = { workspace = true }
2424
oxc_parser = { workspace = true }
2525
oxc_semantic = { workspace = true }
2626
oxc_span = { workspace = true }
27-
oxc_transformer_plugins = { workspace = true }
28-
29-
flate2 = { workspace = true }
3027
oxc_tasks_common = { workspace = true }
28+
oxc_transformer_plugins = { workspace = true }
3129

3230
cow-utils = { workspace = true }
31+
flate2 = { workspace = true }
3332
humansize = { workspace = true }
33+
pico-args = { workspace = true }
3434
rustc-hash = { workspace = true }
3535
similar-asserts = { workspace = true }

tasks/minsize/src/lib.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,38 @@ use std::{
1010
use cow_utils::CowUtils;
1111
use flate2::{Compression, write::GzEncoder};
1212
use humansize::{DECIMAL, format_size};
13+
use pico_args::Arguments;
14+
use rustc_hash::FxHashMap;
15+
1316
use oxc_allocator::Allocator;
1417
use oxc_codegen::{Codegen, CodegenOptions};
15-
use oxc_minifier::{Minifier, MinifierOptions};
18+
use oxc_minifier::{CompressOptions, MangleOptions, Minifier, MinifierOptions};
1619
use oxc_parser::Parser;
1720
use oxc_semantic::SemanticBuilder;
1821
use oxc_span::SourceType;
1922
use oxc_tasks_common::{TestFile, TestFiles, project_root};
2023
use oxc_transformer_plugins::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};
21-
use rustc_hash::FxHashMap;
2224

2325
#[test]
2426
#[cfg(any(coverage, coverage_nightly))]
2527
fn test() {
2628
run().unwrap();
2729
}
2830

31+
#[derive(Debug, Clone, Copy)]
32+
struct Options {
33+
compress_only: bool,
34+
}
35+
2936
/// # Panics
3037
/// # Errors
3138
pub fn run() -> Result<(), io::Error> {
32-
let marker = std::env::args().nth(1).unwrap_or_else(|| String::from("default"));
39+
let mut args = Arguments::from_env();
40+
41+
let options = Options { compress_only: args.contains("--compress-only") };
42+
43+
let marker = args.free_from_str().unwrap_or_else(|_| "default".to_string());
44+
3345
let files = TestFiles::minifier();
3446

3547
let path = project_root().join("tasks/minsize/minsize.snap");
@@ -104,7 +116,7 @@ pub fn run() -> Result<(), io::Error> {
104116
let save_path = Path::new("./target/minifier").join(marker);
105117

106118
for file in files.files() {
107-
let minified = minify_twice(file);
119+
let minified = minify_twice(file, options);
108120

109121
fs::create_dir_all(&save_path).unwrap();
110122
fs::write(save_path.join(&file.file_name), &minified).unwrap();
@@ -124,21 +136,23 @@ pub fn run() -> Result<(), io::Error> {
124136

125137
println!("{out}");
126138

127-
let mut snapshot = File::create(path)?;
128-
snapshot.write_all(out.as_bytes())?;
129-
snapshot.flush()?;
139+
if !options.compress_only {
140+
let mut snapshot = File::create(path)?;
141+
snapshot.write_all(out.as_bytes())?;
142+
snapshot.flush()?;
143+
}
130144
Ok(())
131145
}
132146

133-
fn minify_twice(file: &TestFile) -> String {
147+
fn minify_twice(file: &TestFile, options: Options) -> String {
134148
let source_type = SourceType::from_path(&file.file_name).unwrap();
135-
let code1 = minify(&file.source_text, source_type);
136-
let code2 = minify(&code1, source_type);
149+
let code1 = minify(&file.source_text, source_type, options);
150+
let code2 = minify(&code1, source_type, options);
137151
assert_eq_minified_code(&code1, &code2, &file.file_name);
138152
code2
139153
}
140154

141-
fn minify(source_text: &str, source_type: SourceType) -> String {
155+
fn minify(source_text: &str, source_type: SourceType, options: Options) -> String {
142156
let allocator = Allocator::default();
143157
let ret = Parser::new(&allocator, source_text, source_type).parse();
144158
let mut program = ret.program;
@@ -148,9 +162,13 @@ fn minify(source_text: &str, source_type: SourceType) -> String {
148162
ReplaceGlobalDefinesConfig::new(&[("process.env.NODE_ENV", "'development'")]).unwrap(),
149163
)
150164
.build(scoping, &mut program);
151-
let ret = Minifier::new(MinifierOptions::default()).build(&allocator, &mut program);
165+
let ret = Minifier::new(MinifierOptions {
166+
mangle: (!options.compress_only).then(MangleOptions::default),
167+
compress: Some(CompressOptions::default()),
168+
})
169+
.build(&allocator, &mut program);
152170
Codegen::new()
153-
.with_options(CodegenOptions::minify())
171+
.with_options(CodegenOptions { minify: !options.compress_only, ..CodegenOptions::minify() })
154172
.with_scoping(ret.scoping)
155173
.build(&program)
156174
.code

0 commit comments

Comments
 (0)