Skip to content

Commit 800cc74

Browse files
Fix Source/Struct/SWC.rs: imports, types, logic
1 parent 808c0de commit 800cc74

1 file changed

Lines changed: 55 additions & 63 deletions

File tree

Source/Struct/SWC.rs

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,127 @@
11
#[derive(Debug, Clone, Serialize, Deserialize)]
22
pub struct FileInfo {
3-
path:PathBuf,
4-
5-
last_modified:SystemTime,
3+
path: PathBuf,
4+
last_modified: SystemTime,
65
}
76

87
#[derive(Debug, Clone, Serialize, Deserialize)]
98
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,
1713
}
1814

1915
#[derive(Debug, Clone)]
2016
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,
2821
}
2922

3023
#[derive(Debug, Default)]
3124
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,
3728
}
3829

3930
impl Default for CompilerConfig {
4031
fn default() -> Self {
4132
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,
4937
}
5038
}
5139
}
5240

5341
#[derive(Debug)]
5442
pub struct Compiler {
55-
config:CompilerConfig,
56-
57-
Outlook:Arc<Mutex<CompilerMetrics>>,
43+
pub config: CompilerConfig,
44+
pub Outlook: Arc<Mutex<CompilerMetrics>>,
5845
}
5946

6047
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())) }
6350
}
6451

6552
#[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> {
6754
let Begin = Instant::now();
6855

69-
let cm = SourceMap::new(FilePathMapping::empty());
56+
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
7057

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);
7259

7360
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+
}),
7565
EsVersion::Es2022,
7666
StringInput::from(&*source_file),
7767
None,
7868
));
7969

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))?;
8171

8272
let Unresolved = Mark::new();
83-
8473
let Top = Mark::new();
8574

8675
Parsed = Parsed.fold_with(&mut swc_ecma_transforms_base::resolver(Unresolved, Top, true));
87-
8876
Parsed = Parsed.fold_with(&mut swc_ecma_transforms_typescript::strip(Unresolved, Top));
89-
77+
9078
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,
9782
..Default::default()
9883
}));
9984

100-
// Parsed = Parsed.fold_with(&mut InjectHelpers::default());
85+
Parsed = Parsed.fold_with(&mut inject_helpers(Unresolved));
10186

10287
let mut Output = vec![];
10388

10489
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),
11294
};
11395

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))?;
11597

11698
let Path = Path::new(File).with_extension("js");
11799

118-
tokio::fs::write(&Path, &Output).await.expect("Failed to write output file")?;
100+
tokio::fs::write(&Path, &Output).await?;
119101

120102
let Elapsed = Begin.elapsed();
121103

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+
}
127109

128110
debug!("Compiled {} in {:?}", File, Elapsed);
129111

130112
Ok(Path.to_string_lossy().to_string())
131113
}
132114
}
133115

116+
use std::path::{Path, PathBuf};
117+
use std::sync::{Arc, Mutex};
118+
use std::time::{Duration, Instant, SystemTime};
134119
use serde::{Deserialize, Serialize};
135120
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

Comments
 (0)