Skip to main content

Library/Struct/
CompilerConfig.rs

1//! Advanced compiler configuration for VSCode compatibility
2//!
3//! This module consolidates all configuration options for Phase 3 advanced
4//! features.
5
6use crate::Fn::{Bundle::BundleConfig, NLS::NLSConfig, Worker::WorkerConfig};
7
8/// Advanced compiler configuration
9///
10/// This extends the basic CompilerConfig with Phase 3 features:
11/// - Private field conversion
12/// - NLS/localization processing
13/// - Worker compilation
14/// - Bundling/esbuild integration
15#[derive(Debug, Clone)]
16pub struct CompilerConfig {
17	/// Basic compiler settings
18	pub target:String,
19
20	pub module:String,
21
22	pub strict:bool,
23
24	pub emit_decorators_metadata:bool,
25
26	// Phase 3: Advanced features
27	/// Enable private field conversion (#field -> __field)
28	pub convert_private_fields:bool,
29
30	/// Prefix to use for converted private fields
31	pub private_field_prefix:String,
32
33	/// NLS configuration
34	pub nls:Option<NLSConfig>,
35
36	/// Worker configuration
37	pub worker:Option<WorkerConfig>,
38
39	/// Bundling configuration
40	pub bundle:Option<BundleConfig>,
41
42	/// Compilation mode
43	pub mode:CompilationMode,
44}
45
46/// Compilation mode
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
48pub enum CompilationMode {
49	/// Simple single-file compilation (original behavior)
50	#[default]
51	SingleFile,
52
53	/// Multi-file bundling
54	Bundle,
55
56	/// Worker compilation
57	Worker,
58
59	/// Full VSCode build pipeline
60	VSCode,
61}
62
63impl CompilerConfig {
64	/// Create default config for simple compilation
65	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	/// Create config for VSCode build pipeline
90	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	/// Enable private field conversion
115	pub fn with_private_fields(mut self, enabled:bool) -> Self {
116		self.convert_private_fields = enabled;
117
118		self
119	}
120
121	/// Enable NLS processing
122	pub fn with_nls(mut self, config:NLSConfig) -> Self {
123		self.nls = Some(config);
124
125		self
126	}
127
128	/// Enable worker compilation
129	pub fn with_workers(mut self, config:WorkerConfig) -> Self {
130		self.worker = Some(config);
131
132		self
133	}
134
135	/// Enable bundling
136	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	/// Check if private field conversion is enabled
149	pub fn should_convert_private_fields(&self) -> bool { self.convert_private_fields }
150
151	/// Check if NLS is enabled
152	pub fn is_nls_enabled(&self) -> bool { self.nls.is_some() }
153
154	/// Check if workers are enabled
155	pub fn are_workers_enabled(&self) -> bool { self.worker.as_ref().map(|w| w.enabled).unwrap_or(false) }
156
157	/// Check if bundling is enabled
158	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}