Skip to main content

Library/Struct/
SWC.rs

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