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 variable declaration calc minification #506

Open
MoritzLoewenstein opened this issue Jun 7, 2023 · 2 comments
Open

css variable declaration calc minification #506

MoritzLoewenstein opened this issue Jun 7, 2023 · 2 comments

Comments

@MoritzLoewenstein
Copy link

MoritzLoewenstein commented Jun 7, 2023

Currently, the following calc expression will not be minified:

:root {
    --foo: calc(10px + 20px);
}

I know that it would break js code that relies on the computed style value for that property.

I will consider trying to implement this if there is a chance that it will get merged (as an opt-in option),
otherwise feel free to close :)

@devongovett
Copy link
Member

devongovett commented Jun 25, 2023

It is sorta hard if there are any unknown parameters like variables in there (e.g. calc(10px + var(--foo))). Even simplifying things like calc(10px + 20px + var(--bar)) might be challenging to do right. But for simple cases like the one you posted where all terms are known it could be done. If you're interested in contributing that, you could start in here and add a new handler for "calc" functions:

Ok(&cssparser::Token::Function(ref f)) => {

From there, it would need to try parsing each possible type that supports calc, e.g. lengths, angles, times, etc. similar to this:

let value = if let Ok(length) = LengthValue::try_from(token) {
TokenOrValue::Length(length)
} else if let Ok(angle) = Angle::try_from(token) {
TokenOrValue::Angle(angle)
} else if let Ok(time) = Time::try_from(token) {
TokenOrValue::Time(time)
} else if let Ok(resolution) = Resolution::try_from(token) {
TokenOrValue::Resolution(resolution)
} else {
TokenOrValue::Token(token.into())
};

The difference would be using Length instead of LengthValue since Length supports calc but LengthValue does not. Then the result would just be a simplified value that could be stored as a TokenOrValue::Length etc.

Let me know if you have questions!

@MoritzLoewenstein
Copy link
Author

Thanks for the explanation, started the implementation in #526. I will let you know when its ready or if I have questions!

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

2 participants