Skip to main content

Library/Fn/SWC/Watch/
Compile.rs

1//! OXC-based watch compile module
2//!
3//! This module provides watch-mode compilation using the OXC compiler.
4
5use std::io::Write;
6
7#[tracing::instrument(skip(options))]
8pub async fn Fn(options:crate::Struct::SWC::Option) -> anyhow::Result<()> {
9	let compiler = std::sync::Arc::new(crate::Fn::OXC::Compiler::Compiler::new(options.config.clone()));
10
11	// Get the input base path
12	let input_base = options.entry[0][0].clone();
13
14	let output_base = options.output.clone();
15
16	let pattern = options.pattern.clone();
17
18	println!("Starting watch compilation from {} to {}", input_base, output_base);
19
20	// Use walkdir to find all TypeScript files in the input directory
21	let ts_files:Vec<String> = walkdir::WalkDir::new(&input_base)
22		.follow_links(true)
23		.into_iter()
24		.filter_map(|e| {
25			let entry = e.ok()?;
26
27			let path = entry.path();
28
29			if path.is_file() && path.to_string_lossy().ends_with(&pattern) {
30				Some(path.to_string_lossy().to_string())
31			} else {
32				None
33			}
34		})
35		.collect();
36
37	println!("Found {} TypeScript files in {}", ts_files.len(), input_base);
38
39	// Process files sequentially
40	let mut count = 0;
41
42	let mut error = 0;
43
44	for file_path in ts_files {
45		print!(".");
46
47		std::io::stdout().flush().unwrap();
48
49		match tokio::fs::read_to_string(&file_path).await {
50			Ok(input) => {
51				// Calculate relative path from input base
52				let input_path = std::path::Path::new(&file_path);
53
54				let base_path = std::path::Path::new(&input_base);
55
56				let relative_path = input_path.strip_prefix(base_path).unwrap_or(input_path);
57
58				// Create output path preserving directory structure
59				let output_path = std::path::Path::new(&output_base).join(relative_path).with_extension("js");
60
61				match compiler.compile_file_to(&file_path, input, &output_path, options.use_define_for_class_fields) {
62					Ok(output) => {
63						debug!("Compiled: {} -> {}", file_path, output);
64
65						count += 1;
66					},
67
68					Err(e) => {
69						error!("Compilation error for {}: {}", file_path, e);
70
71						error += 1;
72					},
73				}
74			},
75
76			Err(e) => {
77				error!("Failed to read file {}: {}", file_path, e);
78
79				error += 1;
80			},
81		}
82	}
83
84	println!();
85
86	let outlook = compiler.outlook.lock().unwrap();
87
88	info!(
89		"Watch compilation complete. Processed {} files in {:?}. {} successful, {} failed.",
90		outlook.count, outlook.elapsed, count, error
91	);
92
93	Ok(())
94}
95
96use tracing::{debug, error, info};