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

Commit fbeef9e

Browse files
committed
Consider rejected option and add to stats data if true
1 parent 1d6aaae commit fbeef9e

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ new PurgecssPlugin({
184184
})
185185
```
186186

187+
* #### rejected
188+
189+
If `true` all removed selectors are added to the [Stats Data](https://webpack.js.org/api/stats/) as `purged`.
190+
187191
## Contributing
188192

189193
Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

lib/purgecss-webpack-plugin.es.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ var files = function files(chunk) {
155155
var webpackVersion = 4;
156156
var styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less'];
157157
var pluginName = 'PurgeCSS';
158+
var purgedStats = {};
158159

159160
var PurgecssPlugin =
160161
/*#__PURE__*/
@@ -178,10 +179,25 @@ function () {
178179
compiler.hooks.compilation.tap(pluginName, function (compilation) {
179180
_this.initializePlugin(compilation);
180181
});
182+
compiler.hooks.done.tapAsync(pluginName, function (stats, cb) {
183+
_this.addStats(stats);
184+
185+
cb();
186+
});
181187
} else {
182188
compiler.plugin('this-compilation', function (compilation) {
183189
_this.initializePlugin(compilation);
184190
});
191+
compiler.plugin('done', function (stats) {
192+
_this.addStats(stats);
193+
});
194+
}
195+
}
196+
}, {
197+
key: "addStats",
198+
value: function addStats(stats) {
199+
if (this.options.rejected) {
200+
stats.purged = purgedStats;
185201
}
186202
}
187203
}, {
@@ -259,7 +275,13 @@ function () {
259275
}
260276

261277
var purgecss = new Purgecss(options);
262-
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css);
278+
var purged = purgecss.purge()[0];
279+
280+
if (purged.rejected) {
281+
purgedStats[name] = purged.rejected;
282+
}
283+
284+
compilation.assets[name] = new ConcatSource(purged.css);
263285
});
264286
});
265287
callback();

lib/purgecss-webpack-plugin.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ var files = function files(chunk) {
159159
var webpackVersion = 4;
160160
var styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less'];
161161
var pluginName = 'PurgeCSS';
162+
var purgedStats = {};
162163

163164
var PurgecssPlugin =
164165
/*#__PURE__*/
@@ -182,10 +183,25 @@ function () {
182183
compiler.hooks.compilation.tap(pluginName, function (compilation) {
183184
_this.initializePlugin(compilation);
184185
});
186+
compiler.hooks.done.tapAsync(pluginName, function (stats, cb) {
187+
_this.addStats(stats);
188+
189+
cb();
190+
});
185191
} else {
186192
compiler.plugin('this-compilation', function (compilation) {
187193
_this.initializePlugin(compilation);
188194
});
195+
compiler.plugin('done', function (stats) {
196+
_this.addStats(stats);
197+
});
198+
}
199+
}
200+
}, {
201+
key: "addStats",
202+
value: function addStats(stats) {
203+
if (this.options.rejected) {
204+
stats.purged = purgedStats;
189205
}
190206
}
191207
}, {
@@ -263,7 +279,13 @@ function () {
263279
}
264280

265281
var purgecss = new Purgecss(options);
266-
compilation.assets[name] = new webpackSources.ConcatSource(purgecss.purge()[0].css);
282+
var purged = purgecss.purge()[0];
283+
284+
if (purged.rejected) {
285+
purgedStats[name] = purged.rejected;
286+
}
287+
288+
compilation.assets[name] = new webpackSources.ConcatSource(purged.css);
267289
});
268290
});
269291
callback();

src/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let webpackVersion = 4
88

99
const styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less']
1010
const pluginName = 'PurgeCSS'
11+
const purgedStats = {}
1112

1213
export default class PurgecssPlugin {
1314
constructor(options) {
@@ -23,10 +24,23 @@ export default class PurgecssPlugin {
2324
compiler.hooks.compilation.tap(pluginName, compilation => {
2425
this.initializePlugin(compilation)
2526
})
27+
compiler.hooks.done.tapAsync(pluginName, (stats, cb) => {
28+
this.addStats(stats);
29+
cb();
30+
})
2631
} else {
2732
compiler.plugin('this-compilation', compilation => {
2833
this.initializePlugin(compilation)
2934
})
35+
compiler.plugin('done', stats => {
36+
this.addStats(stats)
37+
})
38+
}
39+
}
40+
41+
addStats(stats) {
42+
if (this.options.rejected) {
43+
stats.purged = purgedStats
3044
}
3145
}
3246

@@ -100,8 +114,15 @@ export default class PurgecssPlugin {
100114
if (typeof options.whitelistPatternsChildren === 'function') {
101115
options.whitelistPatternsChildren = options.whitelistPatternsChildren()
102116
}
117+
103118
const purgecss = new Purgecss(options)
104-
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css)
119+
const purged = purgecss.purge()[0];
120+
121+
if (purged.rejected) {
122+
purgedStats[name] = purged.rejected;
123+
}
124+
125+
compilation.assets[name] = new ConcatSource(purged.css)
105126
})
106127
})
107128

0 commit comments

Comments
 (0)