@@ -9,8 +9,6 @@ use farmfe_core::{config::Config, plugin::Plugin};
99
1010use farmfe_macro_plugin:: farm_plugin;
1111
12- use utils:: compress_buffer;
13-
1412mod utils;
1513
1614#[ derive( serde:: Deserialize , serde:: Serialize , Default ) ]
@@ -31,13 +29,19 @@ fn default_level() -> u32 {
3129 6
3230}
3331
32+ fn default_threshold ( ) -> usize {
33+ 1024
34+ }
35+
3436#[ derive( serde:: Deserialize , serde:: Serialize , Default ) ]
3537#[ serde( rename_all = "camelCase" ) ]
3638pub struct Options {
3739 #[ serde( default ) ]
3840 pub algorithm : CompressAlgorithm ,
3941 #[ serde( default = "default_level" ) ]
4042 pub level : u32 ,
43+ #[ serde( default = "default_threshold" ) ]
44+ pub threshold : usize ,
4145 #[ serde( default = "default_filter" ) ]
4246 pub filter : String ,
4347 pub delete_origin_file : Option < bool > ,
@@ -78,13 +82,13 @@ impl Plugin for FarmfePluginCompress {
7882 let start = std:: time:: Instant :: now ( ) ;
7983 let ext_name = utils:: get_ext_name ( & self . options . algorithm ) ;
8084 let filter = Regex :: new ( & self . options . filter )
81- . map_err ( |e| CompilationError :: GenericError ( format ! ( "Invalid regex expression: {}" , e) ) ) ?;
85+ . map_err ( |e| CompilationError :: GenericError ( format ! ( "Invalid regex expression for compress plugin : {}" , e) ) ) ?;
8286
8387 let compressed_buffers = param
8488 . resources_map
8589 . par_iter_mut ( )
8690 . filter_map ( |( resource_id, resource) | {
87- if !filter. is_match ( & resource_id) {
91+ if !filter. is_match ( & resource_id) || resource . bytes . len ( ) < self . options . threshold {
8892 return None ;
8993 }
9094 if self . options . delete_origin_file . unwrap_or ( false ) {
@@ -93,7 +97,7 @@ impl Plugin for FarmfePluginCompress {
9397 Some ( (
9498 resource_id. to_string ( ) ,
9599 resource. origin . clone ( ) ,
96- compress_buffer ( & resource. bytes , & self . options . algorithm , self . options . level ) ,
100+ utils :: compress_buffer ( & resource. bytes , & self . options . algorithm , self . options . level ) ,
97101 resource. bytes . len ( ) ,
98102 ) )
99103 } )
0 commit comments