11#[ derive( Debug , Clone , Serialize , Deserialize ) ]
22pub struct FileInfo {
3- path : PathBuf ,
4- last_modified : SystemTime ,
3+ path : PathBuf ,
4+ last_modified : SystemTime ,
55}
66
77#[ derive( Debug , Clone , Serialize , Deserialize ) ]
88pub struct CompilerConfig {
9- pub Target : String ,
10- pub Module : String ,
11- pub Strict : bool ,
12- pub EmitDecoratorsMetadata : bool ,
9+ pub Target : String ,
10+ pub Module : String ,
11+ pub Strict : bool ,
12+ pub EmitDecoratorsMetadata : bool ,
1313}
1414
1515#[ derive( Debug , Clone ) ]
1616pub struct Option {
17- pub entry : Vec < Vec < String > > ,
18- pub separator : char ,
19- pub pattern : String ,
20- pub config : CompilerConfig ,
17+ pub entry : Vec < Vec < String > > ,
18+ pub separator : char ,
19+ pub pattern : String ,
20+ pub config : CompilerConfig ,
2121}
2222
2323#[ derive( Debug , Default ) ]
2424pub struct CompilerMetrics {
25- pub Count : usize ,
26- pub Elapsed : Duration ,
27- pub Error : usize ,
25+ pub Count : usize ,
26+ pub Elapsed : Duration ,
27+ pub Error : usize ,
2828}
2929
3030impl Default for CompilerConfig {
3131 fn default ( ) -> Self {
3232 Self {
33- Target : "es2022" . to_string ( ) ,
34- Module : "commonjs" . to_string ( ) ,
35- Strict : true ,
36- EmitDecoratorsMetadata : true ,
33+ Target : "es2022" . to_string ( ) ,
34+ Module : "commonjs" . to_string ( ) ,
35+ Strict : true ,
36+ EmitDecoratorsMetadata : true ,
3737 }
3838 }
3939}
4040
4141#[ derive( Debug ) ]
4242pub struct Compiler {
43- pub config : CompilerConfig ,
44- pub Outlook : Arc < Mutex < CompilerMetrics > > ,
43+ pub config : CompilerConfig ,
44+ pub Outlook : Arc < Mutex < CompilerMetrics > > ,
4545}
4646
4747impl Compiler {
48- pub fn new ( config : CompilerConfig ) -> Self {
49- Self { config, Outlook : Arc :: new ( Mutex :: new ( CompilerMetrics :: default ( ) ) ) }
48+ pub fn new ( config : CompilerConfig ) -> Self {
49+ Self { config, Outlook : Arc :: new ( Mutex :: new ( CompilerMetrics :: default ( ) ) ) }
5050 }
5151
5252 #[ tracing:: instrument( skip( self , input) ) ]
53- pub fn compile_file ( & self , File : & str , input : String ) -> anyhow:: Result < String > {
53+ pub fn compile_file ( & self , File : & str , input : String ) -> anyhow:: Result < String > {
5454 let Begin = Instant :: now ( ) ;
5555
5656 let cm = Arc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
@@ -64,7 +64,9 @@ impl Compiler {
6464 None ,
6565 ) ) ;
6666
67- let module = parser. parse_module ( ) . map_err ( |e| anyhow:: anyhow!( "Failed to parse TypeScript module: {:?}" , e) ) ?;
67+ let module = parser
68+ . parse_module ( )
69+ . map_err ( |e| anyhow:: anyhow!( "Failed to parse TypeScript module: {:?}" , e) ) ?;
6870
6971 let Unresolved = Mark :: new ( ) ;
7072 let Top = Mark :: new ( ) ;
@@ -73,7 +75,7 @@ impl Compiler {
7375 let mut program = swc_ecma_ast:: Program :: Module ( module) ;
7476
7577 // Apply transforms using process() or visit_mut_with()
76-
78+
7779 // 1. Resolver
7880 {
7981 let mut pass = swc_ecma_transforms_base:: resolver ( Unresolved , Top , true ) ;
@@ -85,13 +87,13 @@ impl Compiler {
8587 let mut pass = swc_ecma_transforms_typescript:: strip ( Unresolved , Top ) ;
8688 pass. process ( & mut program) ;
8789 }
88-
90+
8991 // 3. Decorators
9092 {
9193 let mut pass = decorators:: decorators ( decorators:: Config {
92- legacy : false ,
93- emit_metadata : self . config . EmitDecoratorsMetadata ,
94- use_define_for_class_fields : true ,
94+ legacy : false ,
95+ emit_metadata : self . config . EmitDecoratorsMetadata ,
96+ use_define_for_class_fields : true ,
9597 ..Default :: default ( )
9698 } ) ;
9799 pass. process ( & mut program) ;
@@ -112,13 +114,15 @@ impl Compiler {
112114 let mut Output = vec ! [ ] ;
113115
114116 let mut Emitter = Emitter {
115- cfg : swc_ecma_codegen:: Config :: default ( ) ,
116- cm : cm. clone ( ) ,
117- comments : None ,
118- wr : JsWriter :: new ( cm. clone ( ) , "\n " , & mut Output , None ) ,
117+ cfg : swc_ecma_codegen:: Config :: default ( ) ,
118+ cm : cm. clone ( ) ,
119+ comments : None ,
120+ wr : JsWriter :: new ( cm. clone ( ) , "\n " , & mut Output , None ) ,
119121 } ;
120122
121- Emitter . emit_module ( & module) . map_err ( |e| anyhow:: anyhow!( "Failed to emit JavaScript: {:?}" , e) ) ?;
123+ Emitter
124+ . emit_module ( & module)
125+ . map_err ( |e| anyhow:: anyhow!( "Failed to emit JavaScript: {:?}" , e) ) ?;
122126
123127 let Path = Path :: new ( File ) . with_extension ( "js" ) ;
124128
@@ -138,12 +142,15 @@ impl Compiler {
138142 }
139143}
140144
141- use std:: path:: { Path , PathBuf } ;
142- use std:: sync:: { Arc , Mutex } ;
143- use std:: time:: { Duration , Instant , SystemTime } ;
145+ use std:: {
146+ path:: { Path , PathBuf } ,
147+ sync:: { Arc , Mutex } ,
148+ time:: { Duration , Instant , SystemTime } ,
149+ } ;
150+
144151use serde:: { Deserialize , Serialize } ;
145152use tracing:: debug;
146- use swc_common:: { SourceMap , FilePathMapping , FileName , Mark } ;
153+ use swc_common:: { FileName , FilePathMapping , Mark , SourceMap } ;
147154use swc_ecma_ast:: { EsVersion , Pass } ;
148155use swc_ecma_parser:: { Parser , StringInput , Syntax , lexer:: Lexer } ;
149156use swc_ecma_codegen:: { Emitter , text_writer:: JsWriter } ;
0 commit comments