1
- use std:: { io:: Write , sync:: Arc } ;
1
+ use std:: { borrow :: Cow , io:: Write , sync:: Arc } ;
2
2
3
3
use anyhow:: { bail, Context , Result } ;
4
4
use swc_core:: {
@@ -19,7 +19,8 @@ use swc_core::{
19
19
transforms:: base:: fixer:: paren_remover,
20
20
} ,
21
21
} ;
22
- use turbo_tasks:: { ResolvedVc , Vc } ;
22
+ use tracing:: { field:: Empty , Instrument } ;
23
+ use turbo_tasks:: { ResolvedVc , ValueToString , Vc } ;
23
24
use turbo_tasks_fs:: FileSystemPath ;
24
25
use turbopack_core:: {
25
26
code_builder:: { Code , CodeBuilder } ,
@@ -34,6 +35,22 @@ pub async fn minify(
34
35
code : Vc < Code > ,
35
36
source_maps : Vc < bool > ,
36
37
) -> Result < Vc < Code > > {
38
+ let code_ref = code. await ?;
39
+ let code_str = code_ref. source_code ( ) . to_str ( ) ?;
40
+ let span = tracing:: info_span!( "minify" , path = %path. to_string( ) . await ?, input_len = code_str. len( ) , output_len = Empty ) ;
41
+ let code = async move { minify_inner ( path, code, code_str, source_maps) . await }
42
+ . instrument ( span. clone ( ) )
43
+ . await ?;
44
+ span. record ( "output_len" , code. source_code ( ) . len ( ) ) ;
45
+ Ok ( code. cell ( ) )
46
+ }
47
+
48
+ pub async fn minify_inner (
49
+ path : Vc < FileSystemPath > ,
50
+ code : Vc < Code > ,
51
+ code_str : Cow < ' _ , str > ,
52
+ source_maps : Vc < bool > ,
53
+ ) -> Result < Code > {
37
54
let path = path. await ?;
38
55
let source_maps = source_maps. await ?. then ( || code. generate_source_map ( ) ) ;
39
56
let code = code. await ?;
@@ -43,7 +60,7 @@ pub async fn minify(
43
60
let compiler = Arc :: new ( Compiler :: new ( cm. clone ( ) ) ) ;
44
61
let fm = compiler. cm . new_source_file (
45
62
FileName :: Custom ( path. path . to_string ( ) ) . into ( ) ,
46
- code . source_code ( ) . to_str ( ) ? . into_owned ( ) ,
63
+ code_str . into_owned ( ) ,
47
64
) ;
48
65
49
66
let lexer = Lexer :: new (
@@ -134,7 +151,7 @@ pub async fn minify(
134
151
} else {
135
152
builder. push_source ( & src. into ( ) , None ) ;
136
153
}
137
- Ok ( builder. build ( ) . cell ( ) )
154
+ Ok ( builder. build ( ) )
138
155
}
139
156
140
157
// From https://github.com/swc-project/swc/blob/11efd4e7c5e8081f8af141099d3459c3534c1e1d/crates/swc/src/lib.rs#L523-L560
0 commit comments