Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
elliotwaite opened this issue Jan 24, 2025 · 0 comments

Comments

@elliotwaite
Copy link

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant