parcel-bundler / parcel-bundler/lightningcss
CSS modules scoped and global class name selectors aren't combined when minified
Open
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 7.7k
- Forks
- 302
- PR merge metrics
- No merged PRs in 30d
Description
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);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Rust demo with StyleSheet::parse, css_modules::Config, minify, and to_css to reproduce the selector output. Trace how CSS Modules and global selectors are represented during minification. Done means equivalent scoped and global class selectors combine into one rule, matching expected_2, with regression coverage for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- css, rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100