Preventing Purge CSS from removing "hover:text-{color}" classes #1659
-
With the release of 1.4.0, I was excited, because on a recent project of ours our custom PurgeCSS configuration was removing some styles that were in fact included in our components. Unfortunately, I am finding the same result after upgrading to 1.4.0. Would be great to see if anyone can repro this issue, as our CSS bundle size goes from 940kb! to 22Kb with this update... Wow! In the example below, the hover:text-blue selector is applied and works up until PurgeCSS gets involved. Literally nothing else in the entire project is erroneously removed besides this. Other hover styles seems to work as well, so not sure why this would be the case. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 1 reply
-
What is your PurgeCSS config inside
|
Beta Was this translation helpful? Give feedback.
-
@srhise be sure that you're not generating that class dynamically (ie |
Beta Was this translation helpful? Give feedback.
-
Yeah, that’s it. Our button component have all kinds of props that translate into dynamic classes. Things like colors, outlines, icons etc. Looks like we won’t be able to utilize PurgeCSS effectively without a workaround. Thank you 👍 |
Beta Was this translation helpful? Give feedback.
-
Looking into the whitelist feature |
Beta Was this translation helpful? Give feedback.
-
@srhise my advise would be to ditch string concatenation or interpolation and just use full strings, ie: function Button({propsColor}) {
let hoverClass;
let mainColor;
switch (propsColor) {
case "blue":
hoverClass = "hover:bg-blue-500";
mainColor = "bg-blue-600";
...
} it is super verbose but guarantees that there will be no case when whitelist becomes outdated and you have a broken production deploy because of it |
Beta Was this translation helpful? Give feedback.
-
I decided instead to run purge with postcss after the site builds. It's a shame not to use the built-in feature, but I generate quite a few classes in my templates and it seems a bit much to go and change it all just for this. |
Beta Was this translation helpful? Give feedback.
-
That's a cool idea. Any examples of running after build? |
Beta Was this translation helpful? Give feedback.
-
Well I'm using Jekyll, and a good automated setup is well described here In short, jekyll outputs to a |
Beta Was this translation helpful? Give feedback.
@srhise be sure that you're not generating that class dynamically (ie
hover:bg-${className}
) or purge will not be able to detect the usage