Library/Struct/
CompilerConfig.rs1use crate::Fn::{Bundle::BundleConfig, NLS::NLSConfig, Worker::WorkerConfig};
7
8#[derive(Debug, Clone)]
16pub struct CompilerConfig {
17 pub target:String,
19
20 pub module:String,
21
22 pub strict:bool,
23
24 pub emit_decorators_metadata:bool,
25
26 pub convert_private_fields:bool,
29
30 pub private_field_prefix:String,
32
33 pub nls:Option<NLSConfig>,
35
36 pub worker:Option<WorkerConfig>,
38
39 pub bundle:Option<BundleConfig>,
41
42 pub mode:CompilationMode,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
48pub enum CompilationMode {
49 #[default]
51 SingleFile,
52
53 Bundle,
55
56 Worker,
58
59 VSCode,
61}
62
63impl CompilerConfig {
64 pub fn simple() -> Self {
66 Self {
67 target:"es2024".to_string(),
68
69 module:"commonjs".to_string(),
70
71 strict:true,
72
73 emit_decorators_metadata:true,
74
75 convert_private_fields:false,
76
77 private_field_prefix:"__".to_string(),
78
79 nls:None,
80
81 worker:None,
82
83 bundle:None,
84
85 mode:CompilationMode::SingleFile,
86 }
87 }
88
89 pub fn vscode() -> Self {
91 Self {
92 target:"es2024".to_string(),
93
94 module:"esmodule".to_string(),
95
96 strict:true,
97
98 emit_decorators_metadata:true,
99
100 convert_private_fields:true,
101
102 private_field_prefix:"__".to_string(),
103
104 nls:Some(NLSConfig::new()),
105
106 worker:Some(WorkerConfig::new()),
107
108 bundle:Some(BundleConfig::bundle()),
109
110 mode:CompilationMode::VSCode,
111 }
112 }
113
114 pub fn with_private_fields(mut self, enabled:bool) -> Self {
116 self.convert_private_fields = enabled;
117
118 self
119 }
120
121 pub fn with_nls(mut self, config:NLSConfig) -> Self {
123 self.nls = Some(config);
124
125 self
126 }
127
128 pub fn with_workers(mut self, config:WorkerConfig) -> Self {
130 self.worker = Some(config);
131
132 self
133 }
134
135 pub fn with_bundling(mut self, config:BundleConfig) -> Self {
137 let requires_bundle = config.requires_bundle();
138
139 self.bundle = Some(config);
140
141 if requires_bundle {
142 self.mode = CompilationMode::Bundle;
143 }
144
145 self
146 }
147
148 pub fn should_convert_private_fields(&self) -> bool { self.convert_private_fields }
150
151 pub fn is_nls_enabled(&self) -> bool { self.nls.is_some() }
153
154 pub fn are_workers_enabled(&self) -> bool { self.worker.as_ref().map(|w| w.enabled).unwrap_or(false) }
156
157 pub fn is_bundling_enabled(&self) -> bool { self.bundle.as_ref().map(|b| b.requires_bundle()).unwrap_or(false) }
159}
160
161impl Default for CompilerConfig {
162 fn default() -> Self { Self::simple() }
163}
164
165#[cfg(test)]
166mod tests {
167
168 use super::*;
169
170 #[test]
171 fn test_simple_config() {
172 let config = CompilerConfig::simple();
173
174 assert_eq!(config.mode, CompilationMode::SingleFile);
175
176 assert!(!config.convert_private_fields);
177 }
178
179 #[test]
180 fn test_vscode_config() {
181 let config = CompilerConfig::vscode();
182
183 assert_eq!(config.mode, CompilationMode::VSCode);
184
185 assert!(config.convert_private_fields);
186
187 assert!(config.is_nls_enabled());
188
189 assert!(config.are_workers_enabled());
190
191 assert!(config.is_bundling_enabled());
192 }
193
194 #[test]
195 fn test_builder_pattern() {
196 let config = CompilerConfig::simple()
197 .with_private_fields(true)
198 .with_nls(NLSConfig::new())
199 .with_workers(WorkerConfig::new());
200
201 assert!(config.convert_private_fields);
202
203 assert!(config.is_nls_enabled());
204
205 assert!(config.are_workers_enabled());
206 }
207}