oj_css 0.2.22

CSS pipeline: Lightning CSS transforms, CSS Modules scoping
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! The lightningcss compile: parse with recovery, lower to targets, print,
//! then rebase urls, append the source map and build the module exports.

use std::sync::{Arc, RwLock};

use base64::Engine as _;
use lightningcss::css_modules;
use lightningcss::dependencies::DependencyOptions;
use lightningcss::error::{Error as CssError, ParserError};
use lightningcss::printer::PrinterOptions;
use lightningcss::stylesheet::{MinifyOptions, ParserOptions, StyleSheet};

use crate::modules::{is_css_module, module_exports, module_is_scoped, scoped_name_pattern};
use crate::rebase::{css_base_dir, dev_url_of, rewrite_dependencies};
use crate::resolve::CssResolve;
use crate::targets::browser_targets;

#[derive(Debug)]
pub struct CssOutput {
    pub css: String,
    /// A CSS module's class map, sorted by name; None for a plain stylesheet.
    pub exports: Option<Vec<(String, String)>>,
}

/// What a compile does beyond parse-and-print.
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct Mode {
    pub minify: bool,
    /// Rewrite relative urls to server-absolute ones (dev).
    pub rebase: bool,
    /// Append an inline source map (`css.devSourcemap`).
    pub source_map: bool,
}

pub fn compile_css(url: &str, source: &str, minify: bool) -> Result<CssOutput, String> {
    let mode = Mode {
        minify,
        ..Mode::default()
    };
    compile_css_depth(url, source, mode, &CssResolve::default(), 0)
}

/// The build compile: `build.cssMinify` and `build.cssTarget` from `resolve`.
pub fn compile_css_with(
    url: &str,
    source: &str,
    resolve: &CssResolve<'_>,
) -> Result<CssOutput, String> {
    let mode = Mode {
        minify: resolve.minify,
        ..Mode::default()
    };
    compile_css_depth(url, source, mode, resolve, 0)
}

pub fn compile_css_rebased(url: &str, source: &str, minify: bool) -> Result<CssOutput, String> {
    let mode = Mode {
        minify,
        rebase: true,
        source_map: false,
    };
    compile_css_depth(url, source, mode, &CssResolve::default(), 0)
}

/// `compile_css_rebased` plus an inline source map back to `url`.
pub fn compile_css_rebased_with_map(
    url: &str,
    source: &str,
    minify: bool,
) -> Result<CssOutput, String> {
    let mode = Mode {
        minify,
        rebase: true,
        source_map: true,
    };
    compile_css_depth(url, source, mode, &CssResolve::default(), 0)
}

/// The dev-server compile: urls rebased to server-absolute ones, aliased ones
/// to the url of the file they name, as Vite's url rewriter does.
pub fn compile_css_dev(
    url: &str,
    source: &str,
    source_map: bool,
    resolve: &CssResolve<'_>,
) -> Result<CssOutput, String> {
    let mode = Mode {
        minify: false,
        rebase: true,
        source_map,
    };
    compile_css_depth(url, source, mode, resolve, 0)
}

/// Parse with error recovery: like Vite's postcss pipeline, a legacy hack
/// (`*zoom: 1`, IE `filter: progid:...`) or a stray invalid rule is dropped
/// with a warning instead of failing the file.
pub(crate) fn parse_stylesheet<'i>(
    source: &'i str,
    filename: &str,
    css_modules: Option<css_modules::Config>,
) -> Result<StyleSheet<'i>, String> {
    let warnings = Arc::new(RwLock::new(Vec::new()));
    let options = ParserOptions {
        filename: filename.to_string(),
        css_modules,
        error_recovery: true,
        warnings: Some(Arc::clone(&warnings)),
        ..ParserOptions::default()
    };
    let sheet = StyleSheet::parse(source, options)
        .map_err(|err| format!("css parse error in {filename}: {err}"))?;
    report_warnings(filename, &warnings);
    Ok(sheet)
}

/// Recovered errors, printed like PostCSS plugin warnings.
fn report_warnings(name: &str, warnings: &RwLock<Vec<CssError<ParserError<'_>>>>) {
    const SHOWN: usize = 5;
    let Ok(list) = warnings.read() else {
        return;
    };
    for w in list.iter().take(SHOWN) {
        eprintln!("oj: css warning in {name}: {w}");
    }
    if list.len() > SHOWN {
        eprintln!("oj: css warning in {name}: {} more", list.len() - SHOWN);
    }
}

pub(crate) fn compile_css_depth(
    url: &str,
    source: &str,
    mode: Mode,
    resolve: &CssResolve<'_>,
    depth: u8,
) -> Result<CssOutput, String> {
    let is_module_file = is_css_module(url);
    let modules = (is_module_file && module_is_scoped(url, resolve)).then(|| css_modules::Config {
        pattern: scoped_name_pattern(resolve.modules),
        ..css_modules::Config::default()
    });
    let mut stylesheet = parse_stylesheet(source, url, modules)?;

    let targets = browser_targets(resolve.targets);
    stylesheet
        .minify(MinifyOptions {
            targets,
            ..MinifyOptions::default()
        })
        .map_err(|err| format!("css transform error in {url}: {err}"))?;

    let base = if mode.rebase { css_base_dir(url) } else { None };
    let mut sm = parcel_sourcemap::SourceMap::new("");
    if mode.source_map {
        let idx = sm.add_source(url);
        let _ = sm.set_source_content(idx as usize, source);
    }
    let result = stylesheet
        .to_css(PrinterOptions {
            minify: mode.minify,
            targets,
            analyze_dependencies: base.as_ref().map(|_| DependencyOptions::default()),
            source_map: mode.source_map.then_some(&mut sm),
            ..PrinterOptions::default()
        })
        .map_err(|err| format!("css print error in {url}: {err}"))?;

    let mut css = match (base, result.dependencies) {
        (Some(base), Some(deps)) => {
            rewrite_dependencies(result.code, deps, |spec| dev_url_of(&spec, &base, resolve))
        }
        _ => result.code,
    };
    if mode.source_map {
        // `sourceRoot: "/"` makes devtools resolve `src/app.css` to the served url.
        let json = sm
            .to_json(Some("/"))
            .map_err(|err| format!("css sourcemap error in {url}: {err}"))?;
        let encoded = base64::engine::general_purpose::STANDARD.encode(json);
        css.push_str(&format!(
            "\n/*# sourceMappingURL=data:application/json;base64,{encoded} */\n"
        ));
    }

    let exports = match result.exports {
        Some(map) => Some(module_exports(map, url, resolve, depth)),
        // A module compiled in global mode still exports an (empty) map.
        None if is_module_file => Some(Vec::new()),
        None => None,
    };
    Ok(CssOutput { css, exports })
}

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

    #[test]
    fn plain_css_passes_through_and_minifies() {
        let out = compile_css("/styles.css", "body {\n  color: red;\n}\n", true).unwrap();
        assert_eq!(out.css, "body{color:red}");
        assert!(out.exports.is_none());
    }

    #[test]
    fn css_target_and_minify_settings_drive_the_build_compile() {
        let src = ".a {\n  .b { color: red }\n}\n";
        let modern = CssResolveConfig {
            targets: vec!["chrome120".into(), "safari17.2".into(), "firefox117".into()],
            minify: true,
            ..Default::default()
        };
        let out = compile_css_with("/a.css", src, &modern.as_ref())
            .unwrap()
            .css;
        assert!(
            out.contains(".a{") && out.contains(".b{"),
            "nesting kept for targets that support it: {out}"
        );
        assert!(!out.contains(".a .b"), "{out}");

        let baseline = CssResolveConfig {
            minify: true,
            ..Default::default()
        };
        let out = compile_css_with("/a.css", src, &baseline.as_ref())
            .unwrap()
            .css;
        assert_eq!(
            out, ".a .b{color:red}",
            "baseline (safari16.4) lowers nesting"
        );

        let unminified = CssResolveConfig {
            minify: false,
            ..Default::default()
        };
        let out = compile_css_with("/a.css", src, &unminified.as_ref())
            .unwrap()
            .css;
        assert!(
            out.contains('\n') && out.contains("color: red"),
            "cssMinify false keeps whitespace: {out}"
        );
    }

    #[test]
    fn rebases_relative_url_and_import_to_server_root() {
        // A stylesheet served at /src/app.css: its relative @import and url()
        // must become server-absolute so an injected <style> resolves them
        // against the server root, not the page URL.
        let src = "@import \"./base.css\";\n.a { background: url(./img/bg.png); }";
        let out = compile_css_rebased("/src/app.css", src, true).unwrap();
        assert!(
            out.css.contains("/src/base.css"),
            "import rebased: {}",
            out.css
        );
        assert!(
            out.css.contains("/src/img/bg.png"),
            "url rebased: {}",
            out.css
        );
        assert!(
            !out.css.contains("./"),
            "no relative refs remain: {}",
            out.css
        );
    }

    #[test]
    fn rebase_resolves_parent_segments_and_skips_external_urls() {
        let src = ".a { background: url(../assets/x.png); }\n\
                   .b { background: url(https://cdn.test/y.png); }\n\
                   .c { background: url(data:image/png;base64,AAAA); }";
        let out = compile_css_rebased("/src/ui/card.css", src, true).unwrap();
        assert!(
            out.css.contains("/src/assets/x.png"),
            "parent rebased: {}",
            out.css
        );
        assert!(
            out.css.contains("https://cdn.test/y.png"),
            "external kept: {}",
            out.css
        );
        assert!(
            out.css.contains("data:image/png;base64,AAAA"),
            "data URI kept: {}",
            out.css
        );
    }

    #[test]
    fn plain_compile_does_not_rebase_urls() {
        // The build/SSR path must be untouched: url() stays relative.
        let src = ".a { background: url(./bg.png); }";
        let out = compile_css("/src/app.css", src, true).unwrap();
        assert!(
            out.css.contains("./bg.png"),
            "non-rebased keeps the relative url: {}",
            out.css
        );
    }

    #[test]
    fn dev_sourcemap_is_appended_inline_and_names_the_source() {
        let out = compile_css_rebased_with_map(
            "/src/app.css",
            ".a {\n  color: red;\n}\n.b { color: blue; }\n",
            false,
        )
        .unwrap();
        let marker = "/*# sourceMappingURL=data:application/json;base64,";
        let at = out.css.find(marker).expect("sourceMappingURL comment");
        let b64_json = out.css[at + marker.len()..]
            .trim_end()
            .trim_end_matches("*/")
            .trim();
        // Decode the base64 back and check the map shape.
        let decoded = {
            let t = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            let mut bits = 0u32;
            let mut n = 0;
            let mut bytes = Vec::new();
            for c in b64_json.bytes().filter(|c| *c != b'=') {
                let v = t.iter().position(|x| *x == c).unwrap() as u32;
                bits = bits << 6 | v;
                n += 6;
                if n >= 8 {
                    n -= 8;
                    bytes.push((bits >> n) as u8);
                    bits &= (1 << n) - 1;
                }
            }
            String::from_utf8(bytes).unwrap()
        };
        assert!(
            decoded.contains("\"sources\":[\"src/app.css\"]")
                && decoded.contains("\"sourceRoot\":\"/\""),
            "{decoded}"
        );
        assert!(
            decoded.contains("sourcesContent"),
            "source embedded: {decoded}"
        );
        assert!(
            decoded.contains("\"mappings\":\"") && !decoded.contains("\"mappings\":\"\""),
            "non-empty mappings: {decoded}"
        );
        // Without the flag nothing is appended.
        assert!(
            !compile_css_rebased("/src/app.css", ".a { color: red; }", false)
                .unwrap()
                .css
                .contains("sourceMappingURL")
        );
    }

    #[test]
    fn dev_compile_rewrites_aliased_urls_and_keeps_root_absolute_ones() {
        let alias = vec![("@".to_string(), "./src".to_string())];
        let r = CssResolve {
            root: Some(Path::new("/proj")),
            public_dir: None,
            alias: &alias,
            ..CssResolve::default()
        };
        let src = ".a { background: url(@/img/bg.png?v=1); }\n\
                   .b { background: url(/src/x.png); }\n\
                   .c { background: url(./y.png); }\n\
                   .d { background: url(/logo.svg#id); }";
        let out = compile_css_dev("/src/ui/card.css", src, false, &r)
            .unwrap()
            .css;
        assert!(
            out.contains("url(\"/src/img/bg.png?v=1\")")
                || out.contains("url(/src/img/bg.png?v=1)"),
            "aliased url -> served url of the file: {out}"
        );
        assert!(out.contains("/src/x.png"), "root-absolute kept: {out}");
        assert!(
            !out.contains("/src/@/"),
            "alias must not be treated as a relative segment: {out}"
        );
        assert!(
            out.contains("/src/ui/y.png"),
            "relative still rebased: {out}"
        );
        assert!(out.contains("/logo.svg#id"), "public url kept: {out}");
        // An alias to a file outside the root is served through /@fs.
        let outside = vec![("~ui".to_string(), "/elsewhere/ui".to_string())];
        let r2 = CssResolve {
            root: Some(Path::new("/proj")),
            public_dir: None,
            alias: &outside,
            ..CssResolve::default()
        };
        let out = compile_css_dev(
            "/src/a.css",
            ".a { background: url(~ui/i.png) }",
            false,
            &r2,
        )
        .unwrap()
        .css;
        assert!(out.contains("/@fs/elsewhere/ui/i.png"), "{out}");
    }

    #[test]
    fn parse_errors_are_recovered_not_panicked() {
        // Vite's postcss pipeline never fails a stylesheet over garbage it
        // cannot parse; the invalid rule is dropped and nothing else is lost.
        let out = compile_css("/x.css", "!!not-css!! {}\n.ok { color: red }", true).unwrap();
        assert_eq!(out.css, ".ok{color:red}");
    }

    #[test]
    fn legacy_hacks_do_not_fail_the_stylesheet() {
        // Bootstrap-3 era vendor CSS: `*zoom`, `_height` and IE `filter:
        // progid:` are declaration-level hacks postcss keeps and Vite serves.
        // lightningcss keeps `_height` and `progid:` verbatim but cannot parse
        // the star hack; with error recovery it drops just that declaration
        // and the rest of the rule (and file) survives.
        let src = ".clearfix { *zoom: 1; _height: 1px; color: red; }\n\
                   .g { filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fff', endColorstr='#000'); background: blue; }\n\
                   .b { color: green; }";
        let out = compile_css("/vendor.css", src, true).unwrap();
        assert!(
            out.css.contains(".clearfix{_height:1px;color:red}"),
            "{}",
            out.css
        );
        assert!(
            out.css.contains("progid:DXImageTransform") && out.css.contains("background:#00f"),
            "{}",
            out.css
        );
        assert!(out.css.contains(".b{color:green}"), "{}", out.css);
        // The rebased (dev) path parses the same way.
        assert!(compile_css_rebased("/src/vendor.css", src, false).is_ok());
    }
}