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

Commit 2d1b108

Browse files
committed
function for path to evaluate on each compilation
Useful when running webpack with `--watch`
1 parent e934b93 commit 2d1b108

File tree

8 files changed

+109
-2
lines changed

8 files changed

+109
-2
lines changed

README.md

Lines changed: 7 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:

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ exports[`Webpack Integration Tests simple-with-exclusion 2`] = `
4444
}
4545
"
4646
`;
47+
48+
exports[`Webpack Integration Tests simple-with-paths-function 1`] = `
49+
".hello {
50+
color: red;
51+
}
52+
53+
.whitelisted {
54+
color: green;
55+
}
56+
57+
md\\\\:w-2\\\\/3 {
58+
color: red;
59+
}
60+
"
61+
`;
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/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)