-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to ouput unused css (#763)
* naive implementation of output-unused-css * undo part of the changes, since i broke some tests * add missing change to types * setup basic test * attempt to fix test that is failing because of line ending drama * test if rejected and rejectedCss stay in sync * update the docs * add a test case to preserve empty parent nodes Co-authored-by: Ffloriel <[email protected]>
- Loading branch information
1 parent
d80600f
commit 3a3d958
Showing
12 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,5 +74,6 @@ interface ResultPurge { | |
css: string; | ||
file?: string; | ||
rejected?: string[]; | ||
rejectedCss?: string; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import PurgeCSS from "./../src/index"; | ||
import { ROOT_TEST_EXAMPLES } from "./utils"; | ||
|
||
describe("rejectedCss", () => { | ||
it("returns the rejected css as part of the result", async () => { | ||
expect.assertions(1); | ||
const resultsPurge = await new PurgeCSS().purge({ | ||
content: [`${ROOT_TEST_EXAMPLES}rejectedCss/simple.js`], | ||
css: [`${ROOT_TEST_EXAMPLES}rejectedCss/simple.css`], | ||
rejectedCss: true, | ||
}); | ||
const expected = ` | ||
.rejected { | ||
color: blue; | ||
}`; | ||
expect(resultsPurge[0].rejectedCss?.trim()).toBe(expected.trim()); | ||
}); | ||
it("contains the rejected selectors as part of the rejected css", async () => { | ||
expect.assertions(1); | ||
const resultsPurge = await new PurgeCSS().purge({ | ||
content: [`${ROOT_TEST_EXAMPLES}rejectedCss/simple.js`], | ||
css: [`${ROOT_TEST_EXAMPLES}rejectedCss/simple.css`], | ||
rejected: true, | ||
rejectedCss: true, | ||
}); | ||
expect(resultsPurge[0].rejectedCss?.trim()).toContain(resultsPurge[0].rejected?.[0]); | ||
}); | ||
/** | ||
* https://github.com/FullHuman/purgecss/pull/763#discussion_r754618902 | ||
*/ | ||
it("preserves the node correctly when having an empty parent node", async () => { | ||
expect.assertions(1); | ||
const resultsPurge = await new PurgeCSS().purge({ | ||
content: [`${ROOT_TEST_EXAMPLES}rejectedCss/empty-parent-node.js`], | ||
css: [`${ROOT_TEST_EXAMPLES}rejectedCss/empty-parent-node.css`], | ||
rejectedCss: true, | ||
}); | ||
const expected = `@media (max-width: 66666px) {\n .unused-class, .unused-class2 {\n color: black;\n }\n}`; | ||
expect(resultsPurge[0].rejectedCss?.trim()).toEqual(expected); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/purgecss/__tests__/test_examples/rejectedCss/empty-parent-node.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@media (max-width: 66666px) { | ||
.unused-class, .unused-class2 { | ||
color: black; | ||
} | ||
} |
Empty file.
7 changes: 7 additions & 0 deletions
7
packages/purgecss/__tests__/test_examples/rejectedCss/simple.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.critical { | ||
color: red; | ||
} | ||
|
||
.rejected { | ||
color: blue; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/purgecss/__tests__/test_examples/rejectedCss/simple.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
"critical" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters