maudit 0.12.0

Library for generating static websites.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::errors::AssetError;
use std::convert::Infallible;
use std::fs;
use std::path::{Path, PathBuf};

use lightningcss::bundler::{Bundler, FileProvider};
use lightningcss::printer::PrinterOptions;
use lightningcss::stylesheet::{MinifyOptions, ParserOptions, StyleSheet};
use lightningcss::values::url::Url;
use lightningcss::visit_types;
use lightningcss::visitor::{Visit, Visitor};
use log::debug;

use super::{calculate_hash, make_filename};

/// Result of bundling a CSS file, containing the output code and metadata
/// about referenced assets that were copied to the output directory.
pub struct BundleCssOutput {
    /// The final CSS code (bundled, url-rewritten, optionally minified).
    pub code: String,
    /// Fingerprinted filenames of assets copied to the output directory
    /// (e.g. `charter_regular.a1b2c.woff2`). Used for stale file cleanup.
    pub copied_asset_filenames: Vec<String>,
    /// Canonical paths of source files referenced via `url()` in the CSS.
    /// Used to detect when referenced assets change between builds.
    pub source_dependencies: Vec<PathBuf>,
}

/// Visitor that rewrites relative `url()` references in CSS.
///
/// For each relative URL, the referenced file is copied to `output_dir`
/// with a fingerprinted filename (e.g. `charter_regular.a1b2c.woff2`)
/// and the URL is rewritten to point to the copy.
struct AssetUrlVisitor {
    /// Directory containing the source CSS file (for resolving relative URLs)
    source_dir: PathBuf,
    /// Directory where output assets are written (e.g. `dist/_maudit`)
    output_dir: PathBuf,
    /// Fingerprinted filenames of assets that were copied to the output directory.
    copied_filenames: Vec<String>,
    /// Canonical source paths of referenced assets (for dependency tracking).
    source_deps: Vec<PathBuf>,
    /// Errors encountered during visitation (collected because the Visitor trait
    /// uses `Infallible` as its error type).
    errors: Vec<AssetError>,
}

impl<'i> Visitor<'i> for AssetUrlVisitor {
    type Error = Infallible;

    fn visit_types(&self) -> lightningcss::visitor::VisitTypes {
        visit_types!(URLS)
    }

    fn visit_url(&mut self, url: &mut Url<'i>) -> Result<(), Self::Error> {
        let url_str: &str = &url.url;

        // Skip data URLs and absolute URLs
        if url_str.starts_with("data:")
            || url_str.starts_with("http://")
            || url_str.starts_with("https://")
            || url_str.starts_with('/')
        {
            return Ok(());
        }

        // Resolve the referenced file relative to the source CSS directory
        let source_path = self.source_dir.join(url_str);

        if let Ok(source_path) = source_path.canonicalize()
            && source_path.is_file()
        {
            // Hash the file contents for fingerprinting
            let hash = match calculate_hash(&source_path, None) {
                Ok(h) => h,
                Err(e) => {
                    self.errors.push(e);
                    return Ok(());
                }
            };

            let extension = source_path.extension().and_then(|e| e.to_str());
            let fingerprinted = make_filename(&source_path, &hash, extension);
            let dest_path = self.output_dir.join(&fingerprinted);

            if let Err(e) = fs::copy(&source_path, &dest_path) {
                self.errors.push(AssetError::CopyFailed {
                    source_path,
                    dest_path,
                    source: e,
                });
                return Ok(());
            }

            debug!("Copied CSS asset {:?} -> {:?}", source_path, dest_path);

            let filename = fingerprinted.to_string_lossy().to_string();
            self.copied_filenames.push(filename.clone());
            self.source_deps.push(source_path);

            url.url = filename.into();
        }

        Ok(())
    }
}

/// Bundle a CSS file, resolving `@import` statements, copying referenced assets,
/// and optionally minifying.
///
/// If `source_css` is provided, it is used as the CSS content instead of reading from the file
/// (useful for tailwind-processed output). In that case, `@import` resolution is skipped.
///
/// Referenced assets (fonts, images) are copied to `output_dir` with fingerprinted
/// filenames and their URLs are rewritten.
pub fn bundle_css(
    entry: &Path,
    source_css: Option<&str>,
    minify: bool,
    output_dir: &Path,
) -> Result<BundleCssOutput, Box<dyn std::error::Error>> {
    let source_dir = entry
        .parent()
        .ok_or_else(|| format!("CSS entry has no parent directory: {}", entry.display()))?
        .to_path_buf();

    let mut url_visitor = AssetUrlVisitor {
        source_dir,
        output_dir: output_dir.to_path_buf(),
        copied_filenames: Vec::new(),
        source_deps: Vec::new(),
        errors: Vec::new(),
    };

    let code = if let Some(css) = source_css {
        let mut stylesheet = StyleSheet::parse(css, ParserOptions::default())
            .map_err(|e| format!("Failed to parse CSS: {}", e))?;

        stylesheet.visit(&mut url_visitor).unwrap();

        if minify {
            stylesheet
                .minify(MinifyOptions::default())
                .map_err(|e| format!("Failed to minify CSS: {}", e))?;
        }

        stylesheet
            .to_css(PrinterOptions {
                minify,
                ..Default::default()
            })
            .map_err(|e| format!("Failed to serialize CSS: {}", e))?
            .code
    } else {
        let provider = FileProvider::new();
        let mut bundler = Bundler::new(&provider, None, ParserOptions::default());

        let mut stylesheet = bundler
            .bundle(entry)
            .map_err(|e| format!("Failed to bundle CSS file {}: {}", entry.display(), e))?;

        stylesheet.visit(&mut url_visitor).unwrap();

        if minify {
            stylesheet
                .minify(MinifyOptions::default())
                .map_err(|e| format!("Failed to minify CSS: {}", e))?;
        }

        stylesheet
            .to_css(PrinterOptions {
                minify,
                ..Default::default()
            })
            .map_err(|e| format!("Failed to serialize CSS: {}", e))?
            .code
    };

    if let Some(err) = url_visitor.errors.into_iter().next() {
        return Err(err.into());
    }

    Ok(BundleCssOutput {
        code,
        copied_asset_filenames: url_visitor.copied_filenames,
        source_dependencies: url_visitor.source_deps,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    fn setup_css_dir() -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();

        // Create a simple CSS file
        fs::write(dir.path().join("style.css"), "body { color: red; }").unwrap();

        // Create a font file referenced by CSS
        fs::write(dir.path().join("font.woff2"), b"fake-font-data").unwrap();

        // Create a CSS file that references the font
        fs::write(
            dir.path().join("with_url.css"),
            "body { background: url(font.woff2); }",
        )
        .unwrap();

        // Create CSS files for @import testing
        fs::write(dir.path().join("_partial.css"), "h1 { font-size: 2em; }").unwrap();
        fs::write(
            dir.path().join("main.css"),
            "@import \"_partial.css\";\nbody { color: blue; }",
        )
        .unwrap();

        dir
    }

    #[test]
    fn test_bundle_basic_css() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let result = bundle_css(
            &dir.path().join("style.css"),
            None,
            false,
            output_dir.path(),
        )
        .unwrap();

        assert!(result.code.contains("color"));
        assert!(result.copied_asset_filenames.is_empty());
        assert!(result.source_dependencies.is_empty());
    }

    #[test]
    fn test_bundle_with_import() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let result =
            bundle_css(&dir.path().join("main.css"), None, false, output_dir.path()).unwrap();

        // The bundled output should contain both the partial and the main styles
        assert!(
            result.code.contains("font-size"),
            "should contain @imported partial"
        );
        assert!(result.code.contains("color"), "should contain main styles");
    }

    #[test]
    fn test_bundle_rewrites_url_and_copies_asset() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let result = bundle_css(
            &dir.path().join("with_url.css"),
            None,
            false,
            output_dir.path(),
        )
        .unwrap();

        // url() should have been rewritten to a fingerprinted filename
        assert!(
            !result.code.contains("url(font.woff2)"),
            "original url should be rewritten"
        );
        assert_eq!(result.copied_asset_filenames.len(), 1);
        assert!(result.copied_asset_filenames[0].contains("font"));
        assert!(result.copied_asset_filenames[0].ends_with(".woff2"));

        // The asset file should have been copied to the output directory
        let copied = output_dir.path().join(&result.copied_asset_filenames[0]);
        assert!(copied.exists(), "asset should be copied to output dir");
        assert_eq!(fs::read(&copied).unwrap(), b"fake-font-data");

        // Source dependencies should track the original font path
        assert_eq!(result.source_dependencies.len(), 1);
        assert!(result.source_dependencies[0].ends_with("font.woff2"));
    }

    #[test]
    fn test_bundle_minification() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let not_minified = bundle_css(
            &dir.path().join("style.css"),
            None,
            false,
            output_dir.path(),
        )
        .unwrap();

        let minified =
            bundle_css(&dir.path().join("style.css"), None, true, output_dir.path()).unwrap();

        assert!(
            minified.code.len() <= not_minified.code.len(),
            "minified output should not be longer"
        );
        // Minified should still contain the actual style value
        assert!(minified.code.contains("red"));
    }

    #[test]
    fn test_bundle_with_source_css() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        // Simulate Tailwind output being passed as source_css
        let tailwind_css = "body { background: url(font.woff2); margin: 0; }";

        let result = bundle_css(
            &dir.path().join("with_url.css"),
            Some(tailwind_css),
            false,
            output_dir.path(),
        )
        .unwrap();

        // Should contain the tailwind output, not the original file content
        assert!(
            result.code.contains("margin"),
            "should use source_css content"
        );
        // url() should still be rewritten and asset copied
        assert_eq!(result.copied_asset_filenames.len(), 1);
        assert!(
            output_dir
                .path()
                .join(&result.copied_asset_filenames[0])
                .exists()
        );
    }

    #[test]
    fn test_bundle_rehashes_when_referenced_asset_changes() {
        // Cache-busting invariant: when a CSS-referenced asset's bytes change,
        // the bundled CSS bytes must change too (via the rewritten url()), so
        // that hashing the CSS output yields a new filename downstream.
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let first = bundle_css(
            &dir.path().join("with_url.css"),
            None,
            false,
            output_dir.path(),
        )
        .unwrap();

        fs::write(dir.path().join("font.woff2"), b"different-font-data").unwrap();

        let second = bundle_css(
            &dir.path().join("with_url.css"),
            None,
            false,
            output_dir.path(),
        )
        .unwrap();

        assert_ne!(
            first.copied_asset_filenames[0], second.copied_asset_filenames[0],
            "asset filename should change when its bytes change"
        );
        assert_ne!(
            first.code, second.code,
            "CSS output should change when a referenced asset's hash changes"
        );
    }

    #[test]
    fn test_bundle_skips_absolute_and_data_urls() {
        let dir = setup_css_dir();
        let output_dir = tempfile::tempdir().unwrap();

        let css = r#"
            .a { background: url(data:image/png;base64,abc); }
            .b { background: url(https://example.com/img.png); }
            .c { background: url(http://example.com/img.png); }
            .d { background: url(/absolute/path.png); }
        "#;

        let result = bundle_css(
            &dir.path().join("style.css"),
            Some(css),
            false,
            output_dir.path(),
        )
        .unwrap();

        // None of these should trigger asset copying
        assert!(result.copied_asset_filenames.is_empty());
        assert!(result.source_dependencies.is_empty());
    }
}