css / csso

CSS minifier with structural optimizations
https://css.github.io/csso/csso.html
MIT License
3.74k stars 189 forks source link

Allow to enforce applying optimisations to custom property values #453

Open ygoe opened 2 years ago

ygoe commented 2 years ago

When a CSS declaration contains multiple spaces/tabs or even a line break, all of those is preserved in a function context (like calc()). I believe these could be reduced to a single space (if not removed entirely, I'm not sure about that, sometimes CSS requires spaces between operators/parameters).

Usually the minified file contains only a single line, but with such source code (writted for readability), all the line breaks in the functions are kept and the minified file contains all the line breaks including the following indentation.

lahmatiy commented 2 years ago

@ygoe Could you please provide an example? The single case I can imaging is a value of custom property.

ygoe commented 2 years ago

Indeed, I observed this with a custom property:

.something
{
  --link-hover-color-l: calc(var(--link-color-l)
    + max(0%, (100% - var(--link-color-l)) * var(--link-hover-lf))
    - max(0%, var(--link-color-l) * (0 - (var(--link-hover-lf)))));
}
lahmatiy commented 1 year ago

Custom properties can contain any content, that's not a regular value but a part of it when substituted. Removing anything from a custom property may break something. So CSSO do nothing with such values for now. Probably we need to add an option to enforce optimisation for such value in case you are sure it doesn't break anything in your case.

idoros commented 1 year ago

Custom properties currently get trimmed, which creates a different output for development/production modes:


.c {
  --x: aaa bbb ; 
  --y: zzz ; 
}
/* OUTPUT: removed whitespace before and after the custom properties values */
.c{--x:aaa bbb;--y:zzz}

Edit: Although this is the opposite of what the author is requesting, I think that the default behavior should preserve the value as is, untrimmed, and an option could be added to select between preserve/trim/min-whitespace

ygoe commented 1 year ago

What's the difference of these additional spaces? I'm not aware of any situation where it's relevant.

idoros commented 1 year ago

getComputedStyle(node).getPropertyValue('--x') returns the untrimmed value

ygoe commented 1 year ago

I still don't need those extra spaces and cannot imagine a scenario where I would ever need it. CSS usually doesn't preserve such spaces and any application that now does is highly unexpected and probably bad design.

idoros commented 1 year ago

I take it back, looking at the csswg it seems that custom properties should be trimmed. Browser implementation currently fail in my case.