Rest/Struct/
CompilerConfig.rs1use crate::Fn::{Bundle::BundleConfig, NLS::NLSConfig, Worker::WorkerConfig};
7
8#[derive(Debug, Clone)]
16pub struct CompilerConfig {
17 pub target:String,
19 pub module:String,
20 pub strict:bool,
21 pub emit_decorators_metadata:bool,
22
23 pub convert_private_fields:bool,
26 pub private_field_prefix:String,
28
29 pub nls:Option<NLSConfig>,
31
32 pub worker:Option<WorkerConfig>,
34
35 pub bundle:Option<BundleConfig>,
37
38 pub mode:CompilationMode,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44pub enum CompilationMode {
45 #[default]
47 SingleFile,
48 Bundle,
50 Worker,
52 VSCode,
54}
55
56impl CompilerConfig {
57 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 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 pub fn with_private_fields(mut self, enabled:bool) -> Self {
91 self.convert_private_fields = enabled;
92 self
93 }
94
95 pub fn with_nls(mut self, config:NLSConfig) -> Self {
97 self.nls = Some(config);
98 self
99 }
100
101 pub fn with_workers(mut self, config:WorkerConfig) -> Self {
103 self.worker = Some(config);
104 self
105 }
106
107 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 pub fn should_convert_private_fields(&self) -> bool { self.convert_private_fields }
119
120 pub fn is_nls_enabled(&self) -> bool { self.nls.is_some() }
122
123 pub fn are_workers_enabled(&self) -> bool { self.worker.as_ref().map(|w| w.enabled).unwrap_or(false) }
125
126 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}