Skip to content

CSS modules scoped and global class name selectors aren't combined when minified #900

Open
@elliotwaite

Description

@elliotwaite

This:

.a {
    color: red;
}

.b {
    color: red;
}

:global(.c) {
    color: red;
}

:global(.d) {
    color: red;
}

minifies to this:

._8Z4fiW_a, ._8Z4fiW_b {
  color: red;
}

.c {
  color: red;
}

.d {
  color: red;
}

But I expected it to minify to this:

._8Z4fiW_a, ._8Z4fiW_b, .c, .d {
  color: red;
}

Demo:

use lightningcss::{
    css_modules,
    stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet},
};

fn main() {
    let mut stylesheet = StyleSheet::parse(
        r#"
.a {
  color: red;
}

.b {
  color: red;
}

:global(.c) {
  color: red;
}

:global(.d) {
  color: red;
}
"#,
        ParserOptions {
            css_modules: Some(css_modules::Config::default()),
            ..ParserOptions::default()
        },
    )
    .unwrap();

    stylesheet.minify(MinifyOptions::default()).unwrap();
    let css = stylesheet.to_css(PrinterOptions::default()).unwrap();

    // The scoped class name selectors get combined, but the global class name
    // ones don't.
    let expected = r"._8Z4fiW_a, ._8Z4fiW_b {
  color: red;
}

.c {
  color: red;
}

.d {
  color: red;
}
";
    assert_eq!(css.code, expected);

    // As a workaround, I can create a separate style sheet and minify that to
    // combine the scoped and global class name selectors into a single rule.
    let mut stylesheet_2 = StyleSheet::parse(&css.code, ParserOptions::default()).unwrap();
    stylesheet_2.minify(MinifyOptions::default()).unwrap();
    let css_2 = stylesheet_2.to_css(PrinterOptions::default()).unwrap();
    let expected_2 = r"._8Z4fiW_a, ._8Z4fiW_b, .c, .d {
  color: red;
}
";
    assert_eq!(css_2.code, expected_2);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions