Skip to content
This repository was archived by the owner on Feb 3, 2020. It is now read-only.

Commit 3e358f8

Browse files
committed
Add support for rejected option.
Push rejected selectors onto result.messages for downstream consumption.
1 parent f9c8bad commit 3e358f8

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ Type: `Array<RegExp>`
5858

5959
You can whitelist selectors based on a regular expression with whitelistPatterns.
6060

61+
### `rejected`
62+
Type: `boolean`
63+
Default value: `false`
64+
65+
If true, purged selectors will be captured and rendered as PostCSS messages.
66+
Use with a PostCSS reporter plugin like [`postcss-reporter`](https://github.com/postcss/postcss-reporter)
67+
to print the purged selectors to the console as they are processed.
68+
6169
### `keyframes`
6270
Type: `boolean`
6371
Default value: `false`

lib/postcss-purgecss.es.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var loadConfigFile = function loadConfigFile(configFile) {
5454
};
5555

5656
var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
57-
return function (root) {
57+
return function (root, result) {
5858
if (typeof opts === 'string' || typeof opts === 'undefined') opts = loadConfigFile(opts);
5959

6060
if (!opts.css || !opts.css.length) {
@@ -83,6 +83,17 @@ var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
8383
if (purgecss.options.keyframes) purgecss.removeUnusedKeyframes(); // purge font face
8484

8585
if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces();
86+
87+
if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
88+
result.messages.push({
89+
type: 'purgecss',
90+
plugin: 'postcss-purgecss',
91+
text: "purging ".concat(purgecss.selectorsRemoved.size, " selectors:\n ").concat(Array.from(purgecss.selectorsRemoved).map(function (selector) {
92+
return selector.trim();
93+
}).join('\n '))
94+
});
95+
purgecss.selectorsRemoved.clear();
96+
}
8697
};
8798
});
8899

lib/postcss-purgecss.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var loadConfigFile = function loadConfigFile(configFile) {
5858
};
5959

6060
var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
61-
return function (root) {
61+
return function (root, result) {
6262
if (typeof opts === 'string' || typeof opts === 'undefined') opts = loadConfigFile(opts);
6363

6464
if (!opts.css || !opts.css.length) {
@@ -87,6 +87,17 @@ var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
8787
if (purgecss.options.keyframes) purgecss.removeUnusedKeyframes(); // purge font face
8888

8989
if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces();
90+
91+
if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
92+
result.messages.push({
93+
type: 'purgecss',
94+
plugin: 'postcss-purgecss',
95+
text: "purging ".concat(purgecss.selectorsRemoved.size, " selectors:\n ").concat(Array.from(purgecss.selectorsRemoved).map(function (selector) {
96+
return selector.trim();
97+
}).join('\n '))
98+
});
99+
purgecss.selectorsRemoved.clear();
100+
}
90101
};
91102
});
92103

src/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const loadConfigFile = configFile => {
2121
}
2222

2323
export default postcss.plugin('postcss-plugin-purgecss', function(opts) {
24-
return function(root) {
24+
return function(root, result) {
2525
if (typeof opts === 'string' || typeof opts === 'undefined')
2626
opts = loadConfigFile(opts)
2727

@@ -62,5 +62,15 @@ export default postcss.plugin('postcss-plugin-purgecss', function(opts) {
6262

6363
// purge font face
6464
if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces()
65+
66+
if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
67+
result.messages.push({
68+
type: 'purgecss',
69+
plugin: 'postcss-purgecss',
70+
text: `purging ${purgecss.selectorsRemoved.size} selectors:
71+
${Array.from(purgecss.selectorsRemoved).map(selector => selector.trim()).join('\n ')}`,
72+
})
73+
purgecss.selectorsRemoved.clear()
74+
}
6575
}
6676
})

0 commit comments

Comments
 (0)