@@ -10,26 +10,38 @@ use std::{
1010use cow_utils:: CowUtils ;
1111use flate2:: { Compression , write:: GzEncoder } ;
1212use humansize:: { DECIMAL , format_size} ;
13+ use pico_args:: Arguments ;
14+ use rustc_hash:: FxHashMap ;
15+
1316use oxc_allocator:: Allocator ;
1417use oxc_codegen:: { Codegen , CodegenOptions } ;
15- use oxc_minifier:: { Minifier , MinifierOptions } ;
18+ use oxc_minifier:: { CompressOptions , MangleOptions , Minifier , MinifierOptions } ;
1619use oxc_parser:: Parser ;
1720use oxc_semantic:: SemanticBuilder ;
1821use oxc_span:: SourceType ;
1922use oxc_tasks_common:: { TestFile , TestFiles , project_root} ;
2023use oxc_transformer_plugins:: { ReplaceGlobalDefines , ReplaceGlobalDefinesConfig } ;
21- use rustc_hash:: FxHashMap ;
2224
2325#[ test]
2426#[ cfg( any( coverage, coverage_nightly) ) ]
2527fn test ( ) {
2628 run ( ) . unwrap ( ) ;
2729}
2830
31+ #[ derive( Debug , Clone , Copy ) ]
32+ struct Options {
33+ compress_only : bool ,
34+ }
35+
2936/// # Panics
3037/// # Errors
3138pub 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