Skip to main content

Library/Struct/
SWC.rs

1#[derive(Debug, Clone, Serialize, Deserialize)]
2pub struct FileInfo {
3	path:PathBuf,
4
5	last_modified:SystemTime,
6}
7
8/// Module format options for code generation
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
10#[serde(rename_all = "lowercase")]
11pub enum ModuleFormat {
12	/// CommonJS module format (default)
13	#[default]
14	CommonJs,
15
16	/// ECMAScript Modules (ESM)
17	EsModule,
18
19	/// Asynchronous Module Definition (AMD)
20	Amd,
21
22	/// UMD (Universal Module Definition)
23	Umd,
24
25	/// No modules - preserve original imports/exports
26	None,
27}
28
29impl ModuleFormat {
30	pub fn from_str(s:&str) -> Self {
31		match s.to_lowercase().as_str() {
32			"esmodule" | "esm" | "esnext" | "es" => ModuleFormat::EsModule,
33
34			"amd" => ModuleFormat::Amd,
35
36			"umd" => ModuleFormat::Umd,
37
38			"none" => ModuleFormat::None,
39
40			_ => ModuleFormat::CommonJs,
41		}
42	}
43
44	pub fn as_str(&self) -> &'static str {
45		match self {
46			ModuleFormat::CommonJs => "commonjs",
47
48			ModuleFormat::EsModule => "esmodule",
49
50			ModuleFormat::Amd => "amd",
51
52			ModuleFormat::Umd => "umd",
53
54			ModuleFormat::None => "none",
55		}
56	}
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct CompilerConfig {
61	pub Target:String,
62
63	pub Module:String,
64
65	pub Strict:bool,
66
67	pub EmitDecoratorsMetadata:bool,
68
69	/// Enable tree-shaking to remove unused code
70	pub TreeShaking:bool,
71
72	/// Enable minification to reduce output size
73	pub Minify:bool,
74
75	/// Module format (commonjs, esmodule, amd, umd, none)
76	pub ModuleFormat:ModuleFormat,
77}
78
79#[derive(Debug, Clone)]
80pub struct Option {
81	pub entry:Vec<Vec<String>>,
82
83	pub separator:char,
84
85	pub pattern:String,
86
87	pub config:CompilerConfig,
88
89	/// Output directory for compiled files
90	pub output:String,
91
92	/// VSCode compatibility: use defineForClassFields (false by default for
93	/// VSCode)
94	pub use_define_for_class_fields:bool,
95}
96
97impl Default for Option {
98	fn default() -> Self {
99		Self {
100			entry:vec![],
101
102			separator:'/',
103
104			pattern:"**/*.ts".to_string(),
105
106			config:CompilerConfig::default(),
107
108			output:"out".to_string(),
109
110			// VSCode compatibility: false ensures class fields use define pattern
111			use_define_for_class_fields:false,
112		}
113	}
114}
115
116#[derive(Debug, Default)]
117pub struct CompilerMetrics {
118	pub Count:usize,
119
120	pub Elapsed:Duration,
121
122	pub Error:usize,
123}
124
125impl Default for CompilerConfig {
126	fn default() -> Self {
127		Self {
128			Target:"es2024".to_string(),
129
130			Module:"commonjs".to_string(),
131
132			Strict:true,
133
134			EmitDecoratorsMetadata:true,
135
136			TreeShaking:false,
137
138			Minify:false,
139
140			ModuleFormat:ModuleFormat::CommonJs,
141		}
142	}
143}
144
145use std::{
146	path::PathBuf,
147	time::{Duration, SystemTime},
148};
149
150use serde::{Deserialize, Serialize};
151
152impl CompilerConfig {
153	/// Check if JSX is enabled
154	pub fn jsx(&self) -> bool {
155		false // JSX is detected at parse time from file extension
156	}
157
158	/// Get module format as string
159	pub fn module_format(&self) -> String {
160		match self.ModuleFormat {
161			ModuleFormat::CommonJs => "commonjs".to_string(),
162
163			ModuleFormat::EsModule => "esmodule".to_string(),
164
165			ModuleFormat::Amd => "amd".to_string(),
166
167			ModuleFormat::Umd => "umd".to_string(),
168
169			ModuleFormat::None => "none".to_string(),
170		}
171	}
172}