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

fixed collect used variables for the changed selector #1217

Merged
merged 1 commit into from
Mar 29, 2024
Merged
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
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