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	pub module:String,
20	pub strict:bool,
21	pub emit_decorators_metadata:bool,
22
23	// Phase 3: Advanced features
24	/// Enable private field conversion (#field -> __field)
25	pub convert_private_fields:bool,
26	/// Prefix to use for converted private fields
27	pub private_field_prefix:String,
28
29	/// NLS configuration
30	pub nls:Option<NLSConfig>,
31
32	/// Worker configuration
33	pub worker:Option<WorkerConfig>,
34
35	/// Bundling configuration
36	pub bundle:Option<BundleConfig>,
37
38	/// Compilation mode
39	pub mode:CompilationMode,
40}
41
42/// Compilation mode
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44pub enum CompilationMode {
45	/// Simple single-file compilation (original behavior)
46	#[default]
47	SingleFile,
48	/// Multi-file bundling
49	Bundle,
50	/// Worker compilation
51	Worker,
52	/// Full VSCode build pipeline
53	VSCode,
54}
55
56impl CompilerConfig {
57	/// Create default config for simple compilation
58	pub fn simple() -> Self {
59		Self {
60			target:"es2024".to_string(),
61			module:"commonjs".to_string(),
62			strict:true,
63			emit_decorators_metadata:true,
64			convert_private_fields:false,
65			private_field_prefix:"__".to_string(),
66			nls:None,
67			worker:None,
68			bundle:None,
69			mode:CompilationMode::SingleFile,
70		}
71	}
72
73	/// Create config for VSCode build pipeline
74	pub fn vscode() -> Self {
75		Self {
76			target:"es2024".to_string(),
77			module:"esmodule".to_string(),
78			strict:true,
79			emit_decorators_metadata:true,
80			convert_private_fields:true,
81			private_field_prefix:"__".to_string(),
82			nls:Some(NLSConfig::new()),
83			worker:Some(WorkerConfig::new()),
84			bundle:Some(BundleConfig::bundle()),
85			mode:CompilationMode::VSCode,
86		}
87	}
88
89	/// Enable private field conversion
90	pub fn with_private_fields(mut self, enabled:bool) -> Self {
91		self.convert_private_fields = enabled;
92		self
93	}
94
95	/// Enable NLS processing
96	pub fn with_nls(mut self, config:NLSConfig) -> Self {
97		self.nls = Some(config);
98		self
99	}
100
101	/// Enable worker compilation
102	pub fn with_workers(mut self, config:WorkerConfig) -> Self {
103		self.worker = Some(config);
104		self
105	}
106
107	/// Enable bundling
108	pub fn with_bundling(mut self, config:BundleConfig) -> Self {
109		let requires_bundle = config.requires_bundle();
110		self.bundle = Some(config);
111		if requires_bundle {
112			self.mode = CompilationMode::Bundle;
113		}
114		self
115	}
116
117	/// Check if private field conversion is enabled
118	pub fn should_convert_private_fields(&self) -> bool { self.convert_private_fields }
119
120	/// Check if NLS is enabled
121	pub fn is_nls_enabled(&self) -> bool { self.nls.is_some() }
122
123	/// Check if workers are enabled
124	pub fn are_workers_enabled(&self) -> bool { self.worker.as_ref().map(|w| w.enabled).unwrap_or(false) }
125
126	/// Check if bundling is enabled
127	pub fn is_bundling_enabled(&self) -> bool { self.bundle.as_ref().map(|b| b.requires_bundle()).unwrap_or(false) }
128}
129
130impl Default for CompilerConfig {
131	fn default() -> Self { Self::simple() }
132}
133
134#[cfg(test)]
135mod tests {
136	use super::*;
137
138	#[test]
139	fn test_simple_config() {
140		let config = CompilerConfig::simple();
141		assert_eq!(config.mode, CompilationMode::SingleFile);
142		assert!(!config.convert_private_fields);
143	}
144
145	#[test]
146	fn test_vscode_config() {
147		let config = CompilerConfig::vscode();
148		assert_eq!(config.mode, CompilationMode::VSCode);
149		assert!(config.convert_private_fields);
150		assert!(config.is_nls_enabled());
151		assert!(config.are_workers_enabled());
152		assert!(config.is_bundling_enabled());
153	}
154
155	#[test]
156	fn test_builder_pattern() {
157		let config = CompilerConfig::simple()
158			.with_private_fields(true)
159			.with_nls(NLSConfig::new())
160			.with_workers(WorkerConfig::new());
161
162		assert!(config.convert_private_fields);
163		assert!(config.is_nls_enabled());
164		assert!(config.are_workers_enabled());
165	}
166}