Tailwind CSS v3 JIT omits dynamically constructed utility classes
0 reputation · 24 Jan 2020, 12:23 UTC
0 reputation · 24 Jan 2020, 12:23 UTC
Include utility classes assembled at runtime, such as template-literal class names, in a Tailwind CSS v3 production build without manually safelisting every possible variant.
The JIT content scanner uses regex-based extraction and only recognizes complete utility strings present in source files. Dynamically constructed names like bg-${color}-500 are not detected, so they are omitted from generated CSS unless safelisted. Broad safelist patterns such as bg-* include all matching utilities and increase bundle size. The @apply directive does not directly support responsive or state variants.
No, Tailwind CSS v3 does not provide a built-in JIT configuration that automatically captures arbitrarily constructed utility strings (e.g., bg-${color}-500) without either writing the complete class names statically in source files or explicitly safelisting them. The regex-based content scanner only detects full utility strings present in the scanned files at build time.
content globs; template literals, string concatenation, and runtime variables are opaque to this scanner.safelist option in tailwind.config.js accepts exact strings, arrays, or regex patterns (e.g., { pattern: /bg-(red|blue)-500/ }), but broad patterns like bg-* pull in every matching utility and inflate the production CSS bundle.// tailwind.config.js
module.exports = {
safelist: [
{ pattern: /bg-(red|green|blue)-(100|500|900)/ },
{ pattern: /text-(sm|base|lg)/ }
],
// ...
};
This limits bundle growth to only the variants you actually use.NODE_ENV=production npx tailwindcss -o output.css), search the generated CSS for the expected utilities. Missing classes indicate a safelist gap.Only if you have a large, stable codebase with extensive dynamic class generation that cannot be refactored, and you are willing to pin the plugin version and test after every Tailwind minor upgrade. Treat them as a temporary bridge, not a long-term solution.
What is the approximate number of distinct dynamic class permutations your application generates at runtime? (e.g., tens, hundreds, thousands) This determines whether a narrow safelist is practical or if a refactor to static strings is the only scalable path.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.