Safelist regex patterns fail to generate utilities for arbitrary variant classes in Tailwind CSS JIT
0 reputation · 22 May 2021, 08:44 UTC
0 reputation · 22 May 2021, 08:44 UTC
I need to guarantee that utility classes containing arbitrary variants are emitted into the generated stylesheet when they are targeted by a regular expression in the safelist array of a Tailwind CSS 3.x configuration.
The documentation states that safelist accepts regex patterns, but it does not specify whether the matcher evaluates the entire class name—including arbitrary variant prefixes such as hover:[color:var(--color)]—or only the base utility portion. Because the JIT engine is the default compiler in Tailwind CSS 3.x, the exact parsing boundary between variant selectors and arbitrary values determines whether a pattern intended to capture those classes actually produces the expected CSS.
Since matching semantics may vary across minor releases, the supported behavior should be confirmed against the specific installed Tailwind CSS version rather than inferred from documentation alone.
35275 reputation · 22 May 2021, 12:49 UTC
In Tailwind CSS 3.x the safelist regex engine matches the entire class string as it appears in the source. This includes any variant prefixes (e.g., hover:) and the full arbitrary value syntax (e.g., [color:var(--primary)]). The engine does not strip variants before applying the regex.
hover:[color:var(--primary)], your pattern must include the hover: prefix or use a wildcard that covers all variants./^(hover|focus):\[[^\]]+\]$/ – matches hover: or focus: with any arbitrary value./^\w+:[^\]]+\]$/ – matches any single‑word variant followed by an arbitrary value./\[[^\]]+\]/ – matches any class that contains an arbitrary value, regardless of variant.tailwind.config.js:
module.exports = {
content: ['./src/**/*.{html,js}'],
safelist: [
{ pattern: /^(hover|focus):\[[^\]]+\]$/ }
]
}
<div class="hover:[color:var(--primary)]">…</div>
npx tailwindcss -i src/input.css -o dist/output.css --verbose
hover:[color:var(--primary)] appears in the verbose list and that the corresponding CSS rule is present in output.css.RegExp.test in Node to confirm.: inside the value). Adjust the pattern accordingly.purge settings or other safelist exclusions.To give more targeted help, could you share the exact regex pattern you’re using in the safelist and the Tailwind CSS version you have installed? That will help confirm whether the pattern needs to be tweaked for variant prefixes or arbitrary value syntax.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.