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

Commit c9aaf06

Browse files
authored
Merge pull request #10 from SebastianS90/paths-function
Function for path to evaluate on each compilation
2 parents e934b93 + 64a641b commit c9aaf06

File tree

9 files changed

+139
-4
lines changed

9 files changed

+139
-4
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ new PurgecssPlugin({
7777
})
7878
```
7979

80+
If you want to regenerate the paths list on every compilation (e.g. with `--watch`), then you can also pass a function:
81+
```js
82+
new PurgecssPlugin({
83+
paths: () => glob.sync(`${PATHS.src}/*`)
84+
})
85+
```
86+
8087
* #### only
8188

8289
You can specify entrypoints to the purgecss-webpack-plugin with the option only:
@@ -88,6 +95,27 @@ new PurgecssPlugin({
8895
})
8996
```
9097

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+
91119
## Contributing
92120

93121
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Webpack Integration Tests path-and-whitelist-functions 1`] = `
4+
".hello {
5+
color: red;
6+
}
7+
8+
.whitelisted {
9+
color: green;
10+
}
11+
12+
md\\\\:w-2\\\\/3 {
13+
color: red;
14+
}
15+
"
16+
`;
17+
318
exports[`Webpack Integration Tests simple 1`] = `
419
".hello {
520
color: red;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.hello {
2+
color: red;
3+
}
4+
5+
.whitelisted {
6+
color: green;
7+
}
8+
9+
md\:w-2\/3 {
10+
color: red;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
3+
<head>
4+
<title>Purgecss webpack test</title>
5+
</head>
6+
7+
<body>
8+
<div class="md:w-2/3"></div>
9+
<div class="hello"></div>
10+
<div></div>
11+
</body>
12+
13+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './../style.css'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.hello {
2+
color: red;
3+
}
4+
5+
.unused {
6+
color: blue;
7+
}
8+
9+
.whitelisted {
10+
color: green;
11+
}
12+
13+
md\:w-2\/3 {
14+
color: red;
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const path = require('path')
2+
const glob = require('glob')
3+
const ExtractTextPlugin = require('extract-text-webpack-plugin')
4+
const PurgecssPlugin = require('../../../src/').default
5+
6+
class CustomExtractor {
7+
static extract(content) {
8+
return content.match(/[A-z0-9-:/]+/g)
9+
}
10+
}
11+
12+
const PATHS = {
13+
src: path.join(__dirname, 'src')
14+
}
15+
16+
module.exports = {
17+
entry: './src/index.js',
18+
module: {
19+
rules: [
20+
{
21+
test: /\.css$/,
22+
use: ExtractTextPlugin.extract({
23+
use: 'css-loader?sourceMap'
24+
})
25+
}
26+
]
27+
},
28+
plugins: [
29+
new ExtractTextPlugin('style.css'),
30+
new PurgecssPlugin({
31+
paths: () => glob.sync(`${PATHS.src}/*`),
32+
whitelist: () => ['whitelisted'],
33+
extractors: [
34+
{
35+
extractor: CustomExtractor,
36+
extensions: ['html', 'js']
37+
}
38+
]
39+
})
40+
]
41+
}

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
})

src/parse.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
export const entryPaths = paths => {
2-
const ret = paths || []
2+
let ret = paths || []
3+
4+
if (typeof ret === 'function') {
5+
ret = ret();
6+
}
37

48
// Convert possible string to an array
59
if (typeof ret === 'string') {
6-
return [ret]
10+
ret = [ret]
711
}
812

913
return ret

0 commit comments

Comments
 (0)