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

Commit 64a641b

Browse files
committed
functions for whitelist and whitelistPatterns
1 parent 2d1b108 commit 64a641b

File tree

8 files changed

+43
-15
lines changed

8 files changed

+43
-15
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ new PurgecssPlugin({
9595
})
9696
```
9797

98+
* #### whitelist and whitelistPatterns
99+
100+
Similar as for the `paths` option, you also can define functions for the these options:
101+
102+
```js
103+
function collectWhitelist() {
104+
// do something to collect the whitelist
105+
return ['whitelisted'];
106+
}
107+
function collectWhitelistPatterns() {
108+
// do something to collect the whitelist
109+
return [/^whitelisted-/];
110+
}
111+
112+
// In the webpack configuration
113+
new PurgecssPlugin({
114+
whitelist: collectWhitelist,
115+
whitelistPatterns: collectWhitelistPatterns
116+
})
117+
```
118+
98119
## Contributing
99120

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

__tests__/__snapshots__/webpack-integration.test.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Webpack Integration Tests simple 1`] = `
3+
exports[`Webpack Integration Tests path-and-whitelist-functions 1`] = `
44
".hello {
55
color: red;
66
}
@@ -15,7 +15,7 @@ md\\\\:w-2\\\\/3 {
1515
"
1616
`;
1717

18-
exports[`Webpack Integration Tests simple-with-exclusion 1`] = `
18+
exports[`Webpack Integration Tests simple 1`] = `
1919
".hello {
2020
color: red;
2121
}
@@ -30,31 +30,31 @@ md\\\\:w-2\\\\/3 {
3030
"
3131
`;
3232

33-
exports[`Webpack Integration Tests simple-with-exclusion 2`] = `
34-
".legacy-unused-class {
33+
exports[`Webpack Integration Tests simple-with-exclusion 1`] = `
34+
".hello {
3535
color: red;
3636
}
3737
38-
.unused-too {
39-
color: blue;
38+
.whitelisted {
39+
color: green;
4040
}
4141
42-
.hello {
42+
md\\\\:w-2\\\\/3 {
4343
color: red;
4444
}
4545
"
4646
`;
4747

48-
exports[`Webpack Integration Tests simple-with-paths-function 1`] = `
49-
".hello {
48+
exports[`Webpack Integration Tests simple-with-exclusion 2`] = `
49+
".legacy-unused-class {
5050
color: red;
5151
}
5252
53-
.whitelisted {
54-
color: green;
53+
.unused-too {
54+
color: blue;
5555
}
5656
57-
md\\\\:w-2\\\\/3 {
57+
.hello {
5858
color: red;
5959
}
6060
"

__tests__/cases/simple-with-paths-function/webpack.config.js renamed to __tests__/cases/path-and-whitelist-functions/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
new ExtractTextPlugin('style.css'),
3030
new PurgecssPlugin({
3131
paths: () => glob.sync(`${PATHS.src}/*`),
32-
whitelist: ['whitelisted'],
32+
whitelist: () => ['whitelisted'],
3333
extractors: [
3434
{
3535
extractor: CustomExtractor,

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ export default class PurgecssPlugin {
5555

5656
// Compile through Purgecss and attach to output.
5757
// This loses sourcemaps should there be any!
58-
const purgecss = new Purgecss({
58+
const options = {
5959
...this.options,
6060
content: filesToSearch,
6161
css: [asset.source()],
6262
stdin: true
63-
})
63+
};
64+
if (typeof options.whitelist === 'function') {
65+
options.whitelist = options.whitelist();
66+
}
67+
if (typeof options.whitelistPatterns === 'function') {
68+
options.whitelistPatterns = options.whitelistPatterns();
69+
}
70+
const purgecss = new Purgecss(options);
6471
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css)
6572
})
6673
})

0 commit comments

Comments
 (0)