LibSass not doing inplace text replacement for variables in unknown at rules
Closed this issue · 2 comments
I am using libsass in unusual circumstances. I am using custom at rules to add integration between the sass stylesheet and my style system. For example, @d-reserved
-- it takes input like this:
@d-reserved $reserved-use-dark-mode;
My style system does a pre-processing pass (before sass) to replace the text with a normal variable assignment from a list of values known beforehand, like this:
// @d-reserved $reserved-use-dark-mode;
$reserved-use-dark-mode: true; // true comes from outside of the stylesheet
// OR
// @d-reserved $reserved-system-accent-color;
reserved-system-accent-color: #e2241a;
Although communication the other way isn't usually as useful, I thought it was necessary to implement a generic palette for things that fall out of the scope of the stylesheet. This step happens after sass parses the stylesheet, so theoretically, inplace text replacement for variables should have been done already.
@d-palette _window: $d-base-color;
// Corresponds to setting the palette's "Window" color role to the value of $d-base-color.
There is no shortage of these statements.
If libsass behaved how I expected it to before implementation, this would be the end of it; the values are received & parsed & used and whatnot by my style system. However, these variable key instances are not assigned their values when used inside an unknown at rule! This means that after sass does its thing on the style sheet, the input data for the style system is not a color value or a primitive, but the (unwanted) name of a variable.
Instead of getting this after sass is done (INTENDED BEHAVIOR),
@d-palette _window: #e0e0e0; // $d-base-color happens to be #e0e0e0
I get this.
@d-palette _window: $d-base-color;
I'm not sure if this is a bug or a flaw in libsass, intended behavior, or just unspecified/undefined behavior. Why wouldn't it replace all instances of the variable name with its value? I can't think of any circumstances where that would be appropriate or wanted.
This is intended behavior. If you want to evaluate Sass variables as part of an unknown at rule, you will have to use interpolation. You can update your code like this:
@d-palette _window: #{$d-base-color};
Sass intentionally does not resolve such syntax in unknown at-rules because they are, well, unknown. This helps Sass to be future proof against CSS at-rules in the future that would use $
as a valid character.
You can find a repl demonstrating this behavior here.
Thanks for clarifying.