Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PurgeCSS whitelist patterns with TailwindCSS

I am trying to preserve all TailwindCSS colour classes (i.e bg-green, bg-red, text-green, text-red) when it is processed via PurgeCSS. These colour classes are set in the CMS rather than code so we cannot search the code for them as they don't (all) exist here.

Therefore I want to use the whitelisting feature of PurgeCSS to retain all classes that beging with 'bg-' or 'text-'. However, the pattern I have below doesn't seem to be doing the trick? Any ideas how to tweak it?

whitelistPatterns: ['^bg\-', '^text\-'],
like image 540
dungey_140 Avatar asked Apr 02 '20 10:04

dungey_140


Video Answer


2 Answers

If you run newer versions of tailwind: whitelist and whitelistPatterns merged into safelist. This info cost me a day of research.

purge: {
  options: {
    safelist: ["bg-red-50"],
  },
  // ... or even
  options: {
    safelist: [/^bg-/, /^text-/]
  },

}
like image 153
Haukez Avatar answered Oct 10 '22 12:10

Haukez


The issue appears to be simply to use regexp, not a string.

whitelistPatterns: [/^bg-/, /^text-/], // Retain all classes starting with...
like image 14
dungey_140 Avatar answered Oct 10 '22 12:10

dungey_140