Skip to content

Commit c6892b6

Browse files
committed
Fix whitelistPatterns bug
1 parent d1f4e10 commit c6892b6

File tree

1 file changed

+62
-37
lines changed

1 file changed

+62
-37
lines changed

src/index.js

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const defaultOptions = {
44
purgecss: {
55
enabled: true,
66

7-
/* Valid PurgeCss config options */
7+
/**
8+
* Valid PurgeCss config options
9+
*/
810
purgecssOptions: {}
911
},
1012
}
@@ -21,48 +23,71 @@ const plugin = (options = {}, context) => {
2123
/**
2224
* Only run purge css in production.
2325
*/
24-
if (isProd && purgecss.enabled) plugins.push(require("@fullhuman/postcss-purgecss")({
25-
content: [
26-
`${cwd}/.vuepress/theme/**/*.*`,
27-
`${cwd}/!(node_modules)/**/*.md`,
28-
],
29-
30-
extractors: [
31-
{
32-
/**
33-
* A fix for purge css to pick up class names with escaped chars
34-
* E.g. md:w-1/2.
35-
*
36-
* Solution from https://github.com/tailwindcss/tailwindcss/issues/391#issuecomment-366922730
37-
*/
38-
extractor: class TailwindExtractor {
39-
static extract(content) {
40-
return (
41-
content.match(/[A-z0-9-:\/]+/g) || []
42-
);
43-
}
44-
},
45-
extensions: [
46-
"css",
47-
"html",
48-
"js",
49-
"vue",
50-
"md",
51-
"styl"
52-
],
53-
54-
/* spread options */
55-
...purgecss.purgecssOptions,
56-
}
57-
],
26+
if (isProd && purgecss.enabled) {
5827

5928
/**
6029
* Ensure default resets and normalised classes are not removed by PurgeCSS
6130
*/
62-
whitelistPatterns: [
31+
const whitelistPatterns = [
6332
/^(h\d|p$|ul|li$|div|ol|table|td$|th$|thead|tbody|main|input|button|form|md-|hljs)/
6433
]
65-
}))
34+
35+
/**
36+
* check if whitelistPatterns already defined then merge it
37+
*/
38+
if (purgecss.purgecssOptions.hasOwnProperty('whitelistPatterns')) {
39+
40+
purgecss.purgecssOptions.whitelistPatterns = [
41+
...whitelistPatterns,
42+
...purgecss.purgecssOptions.whitelistPatterns
43+
]
44+
}
45+
else {
46+
purgecss.purgecssOptions.whitelistPatterns = whitelistPatterns
47+
}
48+
49+
50+
plugins.push(require("@fullhuman/postcss-purgecss")({
51+
content: [
52+
`${cwd}/.vuepress/theme/**/*.*`,
53+
`${cwd}/!(node_modules)/**/*.md`,
54+
],
55+
56+
extractors: [
57+
{
58+
/**
59+
* A fix for purge css to pick up class names with escaped chars
60+
* E.g. md:w-1/2.
61+
*
62+
* Solution from https://github.com/tailwindcss/tailwindcss/issues/391#issuecomment-366922730
63+
*/
64+
extractor: class TailwindExtractor {
65+
static extract(content) {
66+
return (
67+
content.match(/[A-z0-9-:\/]+/g) || []
68+
);
69+
}
70+
},
71+
extensions: [
72+
"css",
73+
"html",
74+
"js",
75+
"vue",
76+
"md",
77+
"styl"
78+
],
79+
80+
}
81+
],
82+
83+
/**
84+
* merge options
85+
*/
86+
...purgecss.purgecssOptions
87+
88+
}))
89+
}
90+
6691

6792
/**
6893
* Merge in the site's purgecss config

0 commit comments

Comments
 (0)