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

Commit 5acbc80

Browse files
committed
Add whitelistPatternsChildren to config options
See https://www.purgecss.com/whitelisting#patterns
1 parent 38bb2c0 commit 5acbc80

File tree

9 files changed

+81
-49
lines changed

9 files changed

+81
-49
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ new PurgecssPlugin({
157157
})
158158
```
159159

160-
* #### whitelist and whitelistPatterns
160+
* #### whitelist, whitelistPatterns and whitelistPatternsChildren
161161

162162
Similar as for the `paths` option, you also can define functions for the these options:
163163

@@ -171,10 +171,16 @@ function collectWhitelistPatterns() {
171171
return [/^whitelisted-/];
172172
}
173173

174+
function collectWhitelistPatternsChildren() {
175+
// do something to collect the whitelist
176+
return [/^whitelisted-/];
177+
}
178+
174179
// In the webpack configuration
175180
new PurgecssPlugin({
176181
whitelist: collectWhitelist,
177-
whitelistPatterns: collectWhitelistPatterns
182+
whitelistPatterns: collectWhitelistPatterns,
183+
whitelistPatternsChildren: collectWhitelistPatternsChildren
178184
})
179185
```
180186

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ exports[`Webpack Integration Tests path-and-whitelist-functions 1`] = `
1313
color: rebeccapurple;
1414
}
1515
16+
.whitelistedPatternChildren > p a {
17+
color: orange;
18+
}
19+
1620
md\\\\:w-2\\\\/3 {
1721
color: red;
1822
}

__tests__/cases/path-and-whitelist-functions/expected/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
color: rebeccapurple;
1111
}
1212

13+
.whitelistedPatternChildren > p a {
14+
color: orange;
15+
}
16+
1317
md\:w-2\/3 {
1418
color: red;
1519
}

__tests__/cases/path-and-whitelist-functions/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
color: rebeccapurple;
1515
}
1616

17+
.whitelistedPatternChildren > p a {
18+
color: orange;
19+
}
20+
1721
md\:w-2\/3 {
1822
color: red;
1923
}
Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
const path = require('path')
2-
const glob = require('glob')
3-
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
4-
const PurgecssPlugin = require('../../../src/').default
1+
const path = require("path");
2+
const glob = require("glob");
3+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
4+
const PurgecssPlugin = require("../../../src/").default;
55

66
class CustomExtractor {
7-
static extract(content) {
8-
return content.match(/[A-z0-9-:/]+/g)
9-
}
7+
static extract(content) {
8+
return content.match(/[A-z0-9-:/]+/g);
9+
}
1010
}
1111

1212
const PATHS = {
13-
src: path.join(__dirname, 'src')
14-
}
13+
src: path.join(__dirname, "src")
14+
};
1515

1616
module.exports = {
17-
mode: 'development',
18-
entry: './src/index.js',
19-
optimization: {
20-
splitChunks: {
21-
cacheGroups: {
22-
styles: {
23-
name: 'styles',
24-
test: /\.css$/,
25-
chunks: 'all',
26-
enforce: true
27-
}
28-
}
17+
mode: "development",
18+
entry: "./src/index.js",
19+
optimization: {
20+
splitChunks: {
21+
cacheGroups: {
22+
styles: {
23+
name: "styles",
24+
test: /\.css$/,
25+
chunks: "all",
26+
enforce: true
2927
}
30-
},
31-
module: {
32-
rules: [
33-
{
34-
test: /\.css$/,
35-
use: [MiniCssExtractPlugin.loader, 'css-loader']
36-
}
37-
]
38-
},
39-
plugins: [
40-
new MiniCssExtractPlugin({
41-
filename: '[name].css'
42-
}),
43-
new PurgecssPlugin({
44-
paths: () => glob.sync(`${PATHS.src}/*`),
45-
whitelist: () => ['whitelisted'],
46-
whitelistPatterns: () => [/^whitelistedPat/],
47-
extractors: [
48-
{
49-
extractor: CustomExtractor,
50-
extensions: ['html', 'js']
51-
}
52-
]
53-
})
28+
}
29+
}
30+
},
31+
module: {
32+
rules: [
33+
{
34+
test: /\.css$/,
35+
use: [MiniCssExtractPlugin.loader, "css-loader"]
36+
}
5437
]
55-
}
38+
},
39+
plugins: [
40+
new MiniCssExtractPlugin({
41+
filename: "[name].css"
42+
}),
43+
new PurgecssPlugin({
44+
paths: () => glob.sync(`${PATHS.src}/*`),
45+
whitelist: () => ["whitelisted"],
46+
whitelistPatterns: () => [/^whitelistedPat/],
47+
whitelistPatternsChildren: () => [/^whitelistedPatternChildren/],
48+
extractors: [
49+
{
50+
extractor: CustomExtractor,
51+
extensions: ["html", "js"]
52+
}
53+
]
54+
})
55+
]
56+
};

lib/purgecss-webpack-plugin.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ interface _Options extends Partial<PurgecssOptions> {
1111
content?: never
1212
whitelist?: any
1313
whitelistPatterns?: any
14+
whitelistPatternsChildren?: any
1415
}
1516

1617
declare namespace PurgecssWebpackPlugin {
1718
interface Options extends _Options {
1819
paths: string[] | (() => string[])
1920
only?: string[]
2021
whitelist?: string[] | (() => string[])
21-
whitelistPatterns?: RegExp[] | (() => RegExp[])
22+
whitelistPatterns?: RegExp[] | (() => RegExp[]),
23+
whitelistPatternsChildren?: RegExp[] | (() => RegExp[]),
2224
}
2325
}
2426

lib/purgecss-webpack-plugin.es.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ function () {
254254
options.whitelistPatterns = options.whitelistPatterns();
255255
}
256256

257+
if (typeof options.whitelistPatternsChildren === 'function') {
258+
options.whitelistPatternsChildren = options.whitelistPatternsChildren();
259+
}
260+
257261
var purgecss = new Purgecss(options);
258262
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css);
259263
});

lib/purgecss-webpack-plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ function () {
258258
options.whitelistPatterns = options.whitelistPatterns();
259259
}
260260

261+
if (typeof options.whitelistPatternsChildren === 'function') {
262+
options.whitelistPatternsChildren = options.whitelistPatternsChildren();
263+
}
264+
261265
var purgecss = new Purgecss(options);
262266
compilation.assets[name] = new webpackSources.ConcatSource(purgecss.purge()[0].css);
263267
});

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export default class PurgecssPlugin {
9797
if (typeof options.whitelistPatterns === 'function') {
9898
options.whitelistPatterns = options.whitelistPatterns()
9999
}
100+
if (typeof options.whitelistPatternsChildren === 'function') {
101+
options.whitelistPatternsChildren = options.whitelistPatternsChildren()
102+
}
100103
const purgecss = new Purgecss(options)
101104
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css)
102105
})

0 commit comments

Comments
 (0)