|
1 | 1 | #[derive(Debug, Clone, Serialize, Deserialize)] |
2 | 2 | pub struct FileInfo { |
3 | | - path:PathBuf, |
4 | | - |
5 | | - last_modified:SystemTime, |
| 3 | + path: PathBuf, |
| 4 | + last_modified: SystemTime, |
6 | 5 | } |
7 | 6 |
|
8 | 7 | #[derive(Debug, Clone, Serialize, Deserialize)] |
9 | 8 | pub struct CompilerConfig { |
10 | | - Target:String, |
11 | | - |
12 | | - Module:String, |
13 | | - |
14 | | - Strict:bool, |
15 | | - |
16 | | - EmitDecoratorsMetadata:bool, |
| 9 | + pub Target: String, |
| 10 | + pub Module: String, |
| 11 | + pub Strict: bool, |
| 12 | + pub EmitDecoratorsMetadata: bool, |
17 | 13 | } |
18 | 14 |
|
19 | 15 | #[derive(Debug, Clone)] |
20 | 16 | pub struct Option { |
21 | | - pub entry:Vec<Vec<String>>, |
22 | | - |
23 | | - pub separator:char, |
24 | | - |
25 | | - pub pattern:String, |
26 | | - |
27 | | - pub config:CompilerConfig, |
| 17 | + pub entry: Vec<Vec<String>>, |
| 18 | + pub separator: char, |
| 19 | + pub pattern: String, |
| 20 | + pub config: CompilerConfig, |
28 | 21 | } |
29 | 22 |
|
30 | 23 | #[derive(Debug, Default)] |
31 | 24 | pub struct CompilerMetrics { |
32 | | - Count:usize, |
33 | | - |
34 | | - Elapsed:Duration, |
35 | | - |
36 | | - Error:usize, |
| 25 | + pub Count: usize, |
| 26 | + pub Elapsed: Duration, |
| 27 | + pub Error: usize, |
37 | 28 | } |
38 | 29 |
|
39 | 30 | impl Default for CompilerConfig { |
40 | 31 | fn default() -> Self { |
41 | 32 | Self { |
42 | | - Target:"es2022".to_string(), |
43 | | - |
44 | | - Module:"commonjs".to_string(), |
45 | | - |
46 | | - Strict:true, |
47 | | - |
48 | | - EmitDecoratorsMetadata:true, |
| 33 | + Target: "es2022".to_string(), |
| 34 | + Module: "commonjs".to_string(), |
| 35 | + Strict: true, |
| 36 | + EmitDecoratorsMetadata: true, |
49 | 37 | } |
50 | 38 | } |
51 | 39 | } |
52 | 40 |
|
53 | 41 | #[derive(Debug)] |
54 | 42 | pub struct Compiler { |
55 | | - config:CompilerConfig, |
56 | | - |
57 | | - Outlook:Arc<Mutex<CompilerMetrics>>, |
| 43 | + pub config: CompilerConfig, |
| 44 | + pub Outlook: Arc<Mutex<CompilerMetrics>>, |
58 | 45 | } |
59 | 46 |
|
60 | 47 | impl Compiler { |
61 | | - pub fn new(config:CompilerConfig) -> Self { |
62 | | - 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())) } |
63 | 50 | } |
64 | 51 |
|
65 | 52 | #[tracing::instrument(skip(self, input))] |
66 | | - async fn compile_file(&self, File:&str, input:String) -> Result<String> { |
| 53 | + pub async fn compile_file(&self, File: &str, input: String) -> anyhow::Result<String> { |
67 | 54 | let Begin = Instant::now(); |
68 | 55 |
|
69 | | - let cm = SourceMap::new(FilePathMapping::empty()); |
| 56 | + let cm = Arc::new(SourceMap::new(FilePathMapping::empty())); |
70 | 57 |
|
71 | | - let source_file = cm.new_source_file(FileName::Real(File.into()), input); |
| 58 | + let source_file = cm.new_source_file(FileName::Real(File.into()).into(), input); |
72 | 59 |
|
73 | 60 | let mut parser = Parser::new_from(Lexer::new( |
74 | | - Syntax::Typescript(TsConfig { decorators:true, ..Default::default() }), |
| 61 | + Syntax::Typescript(swc_ecma_parser::TsConfig { |
| 62 | + decorators: true, |
| 63 | + ..Default::default() |
| 64 | + }), |
75 | 65 | EsVersion::Es2022, |
76 | 66 | StringInput::from(&*source_file), |
77 | 67 | None, |
78 | 68 | )); |
79 | 69 |
|
80 | | - let mut Parsed = parser.parse_module().expect("Failed to parse TypeScript module")?; |
| 70 | + let mut Parsed = parser.parse_module().map_err(|e| anyhow::anyhow!("Failed to parse TypeScript module: {:?}", e))?; |
81 | 71 |
|
82 | 72 | let Unresolved = Mark::new(); |
83 | | - |
84 | 73 | let Top = Mark::new(); |
85 | 74 |
|
86 | 75 | Parsed = Parsed.fold_with(&mut swc_ecma_transforms_base::resolver(Unresolved, Top, true)); |
87 | | - |
88 | 76 | Parsed = Parsed.fold_with(&mut swc_ecma_transforms_typescript::strip(Unresolved, Top)); |
89 | | - |
| 77 | + |
90 | 78 | Parsed = Parsed.fold_with(&mut decorators::decorators(decorators::Config { |
91 | | - legacy:false, |
92 | | - |
93 | | - emit_metadata:self.config.EmitDecoratorsMetadata, |
94 | | - |
95 | | - use_define_for_class_fields:true, |
96 | | - |
| 79 | + legacy: false, |
| 80 | + emit_metadata: self.config.EmitDecoratorsMetadata, |
| 81 | + use_define_for_class_fields: true, |
97 | 82 | ..Default::default() |
98 | 83 | })); |
99 | 84 |
|
100 | | - // Parsed = Parsed.fold_with(&mut InjectHelpers::default()); |
| 85 | + Parsed = Parsed.fold_with(&mut inject_helpers(Unresolved)); |
101 | 86 |
|
102 | 87 | let mut Output = vec![]; |
103 | 88 |
|
104 | 89 | let mut Emitter = Emitter { |
105 | | - cfg:swc_ecma_codegen::Config::default(), |
106 | | - |
107 | | - cm:cm.into().clone(), |
108 | | - |
109 | | - comments:None, |
110 | | - |
111 | | - wr:JsWriter::new(cm.into(), "\n", &mut Output, None), |
| 90 | + cfg: swc_ecma_codegen::Config::default(), |
| 91 | + cm: cm.clone(), |
| 92 | + comments: None, |
| 93 | + wr: JsWriter::new(cm.clone(), "\n", &mut Output, None), |
112 | 94 | }; |
113 | 95 |
|
114 | | - Emitter.emit_module(&Parsed).expect("Failed to emit JavaScript")?; |
| 96 | + Emitter.emit_module(&Parsed).map_err(|e| anyhow::anyhow!("Failed to emit JavaScript: {:?}", e))?; |
115 | 97 |
|
116 | 98 | let Path = Path::new(File).with_extension("js"); |
117 | 99 |
|
118 | | - tokio::fs::write(&Path, &Output).await.expect("Failed to write output file")?; |
| 100 | + tokio::fs::write(&Path, &Output).await?; |
119 | 101 |
|
120 | 102 | let Elapsed = Begin.elapsed(); |
121 | 103 |
|
122 | | - let mut Outlook = self.Outlook.lock().await; |
123 | | - |
124 | | - Outlook.Count += 1; |
125 | | - |
126 | | - Outlook.Elapsed += Elapsed; |
| 104 | + { |
| 105 | + let mut Outlook = self.Outlook.lock().unwrap(); |
| 106 | + Outlook.Count += 1; |
| 107 | + Outlook.Elapsed += Elapsed; |
| 108 | + } |
127 | 109 |
|
128 | 110 | debug!("Compiled {} in {:?}", File, Elapsed); |
129 | 111 |
|
130 | 112 | Ok(Path.to_string_lossy().to_string()) |
131 | 113 | } |
132 | 114 | } |
133 | 115 |
|
| 116 | +use std::path::{Path, PathBuf}; |
| 117 | +use std::sync::{Arc, Mutex}; |
| 118 | +use std::time::{Duration, Instant, SystemTime}; |
134 | 119 | use serde::{Deserialize, Serialize}; |
135 | 120 | use tracing::debug; |
| 121 | +use swc_common::{SourceMap, FilePathMapping, FileName, Mark}; |
| 122 | +use swc_ecma_ast::EsVersion; |
| 123 | +use swc_ecma_parser::{Parser, StringInput, Syntax, lexer::Lexer}; |
| 124 | +use swc_ecma_codegen::{Emitter, text_writer::JsWriter}; |
| 125 | +use swc_ecma_transforms_base::helpers::inject_helpers; |
| 126 | +use swc_ecma_transforms_proposal::decorators; |
| 127 | +use swc_ecma_visit::FoldWith; |
0 commit comments