Library/Fn/SWC/
Compile.rs1#[tracing::instrument(skip(options))]
7pub async fn Fn(options:crate::Struct::SWC::Option, _parallel:bool) -> anyhow::Result<()> {
16 tracing_subscriber::fmt::init();
17
18 let compiler = std::sync::Arc::new(crate::Fn::OXC::Compiler::Compiler::new(options.config.clone()));
20
21 let input_base = options.entry[0][0].clone();
23
24 let output_base = options.output.clone();
25
26 let pattern = options.pattern.clone();
27
28 println!("Starting compilation from {} to {}", input_base, output_base);
29
30 let ts_files:Vec<String> = walkdir::WalkDir::new(&input_base)
33 .follow_links(true)
34 .into_iter()
35 .filter_map(|e| {
36 let entry = e.ok()?;
37 let path = entry.path();
38 let path_str = path.to_string_lossy();
39 if path_str.ends_with(".d.ts") {
41 None
42 } else if path.is_file() && path_str.ends_with(&pattern) {
43 Some(path_str.to_string())
44 } else {
45 None
46 }
47 })
48 .collect();
49
50 println!("Found {} TypeScript files in {}", ts_files.len(), input_base);
51
52 let mut count = 0;
54
55 let mut error = 0;
56
57 for file_path in ts_files {
58 print!(".");
59
60 std::io::stdout().flush().unwrap();
61
62 match tokio::fs::read_to_string(&file_path).await {
63 Ok(input) => {
64 let input_path = std::path::Path::new(&file_path);
66
67 let base_path = std::path::Path::new(&input_base);
68
69 let relative_path = input_path.strip_prefix(base_path).unwrap_or(input_path);
70
71 let output_path = std::path::Path::new(&output_base).join(relative_path).with_extension("js");
73
74 match compiler.compile_file_to(&file_path, input, &output_path, options.use_define_for_class_fields) {
75 Ok(output) => {
76 debug!("Compiled: {} -> {}", file_path, output);
77
78 count += 1;
79 },
80
81 Err(e) => {
82 error!("Compilation error for {}: {}", file_path, e);
83
84 error += 1;
85 },
86 }
87 },
88
89 Err(e) => {
90 error!("Failed to read file {}: {}", file_path, e);
91
92 error += 1;
93 },
94 }
95 }
96
97 println!();
98
99 let outlook = compiler.outlook.lock().unwrap();
100
101 info!(
102 "Compilation complete. Processed {} files in {:?}. {} successful, {} failed.",
103 outlook.count, outlook.elapsed, count, error
104 );
105
106 println!("\n=== Compilation Summary ===");
108
109 println!("Total files processed: {}", outlook.count);
110
111 println!("Successful: {}", count);
112
113 println!("Failed: {}", error);
114
115 println!("Time elapsed: {:?}\n", outlook.elapsed);
116
117 Ok(())
118}
119
120use std::io::Write;
121
122use tracing::{debug, error, info};