tailwind_rs_core/config/
build.rs

1//! Build configuration for tailwind-rs
2
3use serde::{Deserialize, Serialize};
4
5/// Build configuration for tailwind-rs
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct BuildConfig {
8    /// Input paths for source files
9    pub input: Vec<String>,
10    /// Output path for CSS file
11    pub output: String,
12    /// Watch mode for development
13    pub watch: bool,
14    /// Minify output CSS
15    pub minify: bool,
16    /// Source maps generation
17    pub source_maps: bool,
18    /// Purge unused CSS
19    pub purge: bool,
20    /// Additional CSS to include
21    pub additional_css: Vec<String>,
22    /// PostCSS plugins
23    pub postcss_plugins: Vec<String>,
24}
25
26impl BuildConfig {
27    /// Create a new build configuration
28    pub fn new() -> Self {
29        Self {
30            input: vec!["src/**/*.rs".to_string()],
31            output: "dist/styles.css".to_string(),
32            watch: false,
33            minify: false,
34            source_maps: false,
35            purge: true,
36            additional_css: Vec::new(),
37            postcss_plugins: Vec::new(),
38        }
39    }
40
41    /// Add an input path
42    pub fn add_input(&mut self, path: impl Into<String>) {
43        self.input.push(path.into());
44    }
45
46    /// Set output path
47    pub fn set_output(&mut self, path: impl Into<String>) {
48        self.output = path.into();
49    }
50
51    /// Enable watch mode
52    pub fn enable_watch(&mut self) {
53        self.watch = true;
54    }
55
56    /// Disable watch mode
57    pub fn disable_watch(&mut self) {
58        self.watch = false;
59    }
60
61    /// Enable minification
62    pub fn enable_minify(&mut self) {
63        self.minify = true;
64    }
65
66    /// Disable minification
67    pub fn disable_minify(&mut self) {
68        self.minify = false;
69    }
70
71    /// Enable source maps
72    pub fn enable_source_maps(&mut self) {
73        self.source_maps = true;
74    }
75
76    /// Disable source maps
77    pub fn disable_source_maps(&mut self) {
78        self.source_maps = false;
79    }
80
81    /// Enable CSS purging
82    pub fn enable_purge(&mut self) {
83        self.purge = true;
84    }
85
86    /// Disable CSS purging
87    pub fn disable_purge(&mut self) {
88        self.purge = false;
89    }
90
91    /// Add additional CSS file
92    pub fn add_css(&mut self, path: impl Into<String>) {
93        self.additional_css.push(path.into());
94    }
95
96    /// Add PostCSS plugin
97    pub fn add_postcss_plugin(&mut self, plugin: impl Into<String>) {
98        self.postcss_plugins.push(plugin.into());
99    }
100
101    /// Validate build configuration
102    pub fn validate(&self) -> Result<(), String> {
103        if self.output.is_empty() {
104            return Err("Output path cannot be empty".to_string());
105        }
106
107        if self.input.is_empty() {
108            return Err("Input paths cannot be empty".to_string());
109        }
110
111        Ok(())
112    }
113}
114
115impl Default for BuildConfig {
116    fn default() -> Self {
117        Self::new()
118    }
119}
120
121impl From<super::toml_config::BuildConfigToml> for BuildConfig {
122    fn from(toml_config: super::toml_config::BuildConfigToml) -> Self {
123        Self {
124            input: toml_config.input.unwrap_or_else(|| vec!["src/**/*.rs".to_string()]),
125            output: toml_config.output.unwrap_or_else(|| "dist/styles.css".to_string()),
126            watch: toml_config.watch.unwrap_or(false),
127            minify: toml_config.minify.unwrap_or(false),
128            source_maps: toml_config.source_maps.unwrap_or(false),
129            purge: toml_config.purge.unwrap_or(true),
130            additional_css: toml_config.additional_css.unwrap_or_default(),
131            postcss_plugins: toml_config.postcss_plugins.unwrap_or_default(),
132        }
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139
140    #[test]
141    fn test_build_config_creation() {
142        let config = BuildConfig::new();
143        assert!(!config.input.is_empty());
144        assert!(!config.output.is_empty());
145        assert!(!config.watch);
146        assert!(!config.minify);
147        assert!(config.purge);
148    }
149
150    #[test]
151    fn test_build_config_validation() {
152        let mut config = BuildConfig::new();
153        assert!(config.validate().is_ok());
154
155        config.output = "".to_string();
156        assert!(config.validate().is_err());
157
158        config.output = "dist/styles.css".to_string();
159        config.input.clear();
160        assert!(config.validate().is_err());
161    }
162
163    #[test]
164    fn test_build_config_methods() {
165        let mut config = BuildConfig::new();
166        
167        config.add_input("src/**/*.rsx");
168        assert!(config.input.contains(&"src/**/*.rsx".to_string()));
169
170        config.set_output("build/styles.css");
171        assert_eq!(config.output, "build/styles.css");
172
173        config.enable_watch();
174        assert!(config.watch);
175
176        config.enable_minify();
177        assert!(config.minify);
178
179        config.add_css("src/custom.css");
180        assert!(config.additional_css.contains(&"src/custom.css".to_string()));
181    }
182}