parcel-bundler / lightningcss

An extremely fast CSS parser, transformer, bundler, and minifier written in Rust.
https://lightningcss.dev
Mozilla Public License 2.0
6.16k stars 167 forks source link

Optimize media query rulesets #378

Open jfbrennan opened 1 year ago

jfbrennan commented 1 year ago

It seems like there's an optimization to be had by grouping rules from the same media queries. For example, I'm working on dark mode for a project and the minified stylesheet has six instances of @media screen and (prefers-color-scheme:dark){}. Five of those could have been removed from the output (that's 235 characters, and I'm still creating more). I can see other media queries where this should be the case as well.

Perhaps there's an edge case(s) that prevent this from being doable, but if I manually merge my six instances into one everything still works.

devongovett commented 1 year ago

We already merge adjacent media queries, but non-adjacent rules cannot be merged safely without changing the order of the rules and thus potentially changing the behavior.

jfbrennan commented 1 year ago

True. If the order mattered that wouldn't work. I haven't written cascading rules for maybe 6-7 years. Make it opt-in?

replete commented 2 weeks ago

Would like to see this feature as an option. Ordering can impact the specificity but I've not had this problem in the past when using. More complex when CSS Nesting is involved. Understandable to want to skip this however, so I'm just expressing an interest. Great project by the way, thanks