Skip to content

Commit

Permalink
fix: collect of used variables for changed selector (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsn4ik authored Mar 29, 2024
1 parent eef05cd commit 9fbb592
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/purgecss/__tests__/css-variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ describe("purge unused css variables", () => {
expect(purgedCSS.includes("--color-first:")).toBe(true);
expect(purgedCSS.includes("--wrong-order:")).toBe(true);
});
it("keeps '--outline-color'", () => {
expect(purgedCSS.includes("--outline-color:")).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
--used-color: rebeccapurple;
--accent-color: orange;
--wrong-order: yellow;
--outline-color: coral;
--random: var(--not-existing);
}

Expand All @@ -19,6 +20,10 @@
border-color: var(--border-color);
}

.button, .unused-class {
outline-color: var(--outline-color);
}

.button:focus {
background-color: var(--accent-color);
color: var(--primary-color);
Expand Down
5 changes: 2 additions & 3 deletions packages/purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ class PurgeCSS {
return;
}

let keepSelector = true;
const selectorsRemovedFromRule: string[] = [];

// selector transformer, walk over the list of the parsed selectors twice.
Expand All @@ -540,7 +539,7 @@ class PurgeCSS {
return;
}

keepSelector = this.shouldKeepSelector(selector, selectors);
const keepSelector = this.shouldKeepSelector(selector, selectors);

if (!keepSelector) {
if (this.options.rejected) {
Expand Down Expand Up @@ -575,7 +574,7 @@ class PurgeCSS {
}).processSync(node.selector);

// declarations
if (keepSelector && typeof node.nodes !== "undefined") {
if (node.selector && typeof node.nodes !== "undefined") {
for (const childNode of node.nodes) {
if (childNode.type !== "decl") continue;
this.collectDeclarationsData(childNode);
Expand Down

0 comments on commit 9fbb592

Please sign in to comment.