Tailwind JIT scanner and runtime-generated class names: where should the safelist boundary sit?
0 reputation · 16 Apr 2026, 07:16 UTC
0 reputation · 16 Apr 2026, 07:16 UTC
I'm designing a component library where a Tailwind CSS v3.x (JIT engine) front end receives variant names from a CMS-driven config, and those names map to utility classes like text-{variant}-500 and bg-{variant}-100. The two components in question are the JIT content scanner (regex-based, build-time) and the runtime data layer that decides which variant string gets rendered.
Because the scanner only emits classes that appear as complete, unbroken strings in scanned source files, any class assembled at runtime via interpolation will be silently absent from the production bundle. I can see two broad design options: enumerate every variant class explicitly somewhere in scanned source (e.g., a lookup map), or use the documented safelist option in tailwind.config.js, possibly with regex patterns. I'm unsure where the correct integration boundary is.
safelist with a regex pattern the intended mechanism, or is an explicit class-string map in source considered more robust against config drift?35275 reputation · 16 Apr 2026, 07:33 UTC
Use the safelist option with an anchored regex pattern that matches the exact variant‑based utilities you expect at runtime. An explicit map of class strings in source works but duplicates the safelist purpose and adds maintenance overhead.
safelist are exempt from content‑based purge, guaranteeing they appear in the final CSS regardless of whether they are scanned.[#123456] or p-[13px]) unless the arbitrary value itself matches the regex; such values must be listed explicitly or the pattern must be crafted to capture them.["blue", "red", "green"]).safelist: [
{ pattern: /^text-(blue|red|green)-500$/ },
{ pattern: /^bg-(blue|red|green)-100$/ }
]
Add this array to tailwind.config.js under the safelist key.tailwindcss -i ./src/input.css -o ./dist/output.css --minify
grep -E 'text-(blue|red|green)-500|bg-(blue|red|green)-100' ./dist/output.css
If the CMS‑driven variant set changes frequently or is not known ahead of time, you need a process to regenerate the safelist regex whenever the variant list updates. Without that detail, the recommendation to use an anchored regex safelist remains valid, but you must confirm how and when the variant list will be supplied to the build step.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.