Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/compiler/compiler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ test(":root CSS variables with media queries", () => {
});
});

test("every read of a stylesheet lists each variable's most specific value first", () => {
const compiled = compile(
`:root { --tone: red; }
@media (prefers-color-scheme: dark) { :root { --tone: blue; } }
* { --gap: 1px; }
@media (min-width: 100px) { * { --gap: 2px; } }`,
{ inlineVariables: false },
);
const expected = {
vr: [
["tone", [["blue", [["=", "prefers-color-scheme", "dark"]]], ["red"]]],
],
vu: [["gap", [[2, [[">=", "width", 100]]], [1]]]],
};

const first = compiled.stylesheet();
expect({ vr: first.vr, vu: first.vu }).toStrictEqual(expected);

const second = compiled.stylesheet();
expect({ vr: second.vr, vu: second.vu }).toStrictEqual(expected);
expect({ vr: first.vr, vu: first.vu }).toStrictEqual(expected);
});

test("removes unused CSS variables while preserving the resolved value", () => {
const result = compile(
`.test { --blue: blue; --green: green; --red: red; color: var(--red, var(--blue)); }`,
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ export class StylesheetBuilder {

if (this.shared.rootVariables) {
stylesheetOptions.vr = Object.entries(this.shared.rootVariables).map(
// Reverse these so the most specific variables are first
([key, value]) => [key, value.reverse()] as const,
// Most specific first, on a copy: the builder can be read again
([key, value]) => [key, value.toReversed()] as const,
);
}

if (this.shared.universalVariables) {
stylesheetOptions.vu = Object.entries(this.shared.universalVariables).map(
// Reverse these so the most specific variables are first
([key, value]) => [key, value.reverse()] as const,
// Most specific first, on a copy: the builder can be read again
([key, value]) => [key, value.toReversed()] as const,
);
}

Expand Down