From 145b1a87d6c93cfb7a0fba5c039f9ffe825a1b50 Mon Sep 17 00:00:00 2001 From: Vasilii A <3757319+vsn4ik@users.noreply.github.com> Date: Sun, 18 Feb 2024 06:14:56 +0000 Subject: [PATCH] fixed collect of used variables for changed selector --- packages/purgecss/__tests__/css-variables.test.ts | 3 +++ .../__tests__/test_examples/css-variables/variables.css | 5 +++++ packages/purgecss/src/index.ts | 5 ++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/purgecss/__tests__/css-variables.test.ts b/packages/purgecss/__tests__/css-variables.test.ts index 87af6a09..4de7603c 100644 --- a/packages/purgecss/__tests__/css-variables.test.ts +++ b/packages/purgecss/__tests__/css-variables.test.ts @@ -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); + }); }); diff --git a/packages/purgecss/__tests__/test_examples/css-variables/variables.css b/packages/purgecss/__tests__/test_examples/css-variables/variables.css index c5d0cce5..a54134de 100644 --- a/packages/purgecss/__tests__/test_examples/css-variables/variables.css +++ b/packages/purgecss/__tests__/test_examples/css-variables/variables.css @@ -7,6 +7,7 @@ --used-color: rebeccapurple; --accent-color: orange; --wrong-order: yellow; + --outline-color: coral; --random: var(--not-existing); } @@ -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); diff --git a/packages/purgecss/src/index.ts b/packages/purgecss/src/index.ts index 9e46ebe4..2c9d23dc 100644 --- a/packages/purgecss/src/index.ts +++ b/packages/purgecss/src/index.ts @@ -525,7 +525,6 @@ class PurgeCSS { return; } - let keepSelector = true; const selectorsRemovedFromRule: string[] = []; // selector transformer, walk over the list of the parsed selectors twice. @@ -540,7 +539,7 @@ class PurgeCSS { return; } - keepSelector = this.shouldKeepSelector(selector, selectors); + const keepSelector = this.shouldKeepSelector(selector, selectors); if (!keepSelector) { if (this.options.rejected) { @@ -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);