Tailwind JIT utilities missing in SvelteKit production build when class names use runtime template literals
Environment
SvelteKit app built with Vite and Tailwind CSS v3 JIT mode. Source uses runtime template literals like class={text-${size} p-${pad}} and class={bg-[#${hex}]} inside .svelte components. Production build run with NODE_ENV=production on Node 18.
Symptoms
In dev (npm run dev) UI renders correctly. After npm run build and preview, many utilities referenced only via runtime template literals are missing from the compiled CSS: text size classes, padding variants and some arbitrary color utilities. Pages appear broken in production; adding the explicit class string in markup makes the utility appear.
Observations
tailwind.config.js content globs include src/**/*.{svelte,js,ts}. Adding explicit class names to components causes the classes to be present in production CSS. Adding a safelist with regex partially mitigates some patterns but not arbitrary hex color bracket notation. Cleared caches, rebuilt, and upgraded tailwind; problem persisted.
Already attempted
Validated content paths; added safelist entries and regexes for some dynamic patterns; upgraded tailwind and rebuild; tried using bracket arbitrary values inline; none produced a reliable, maintainable fix for runtime template-based classes.
Question
Why are Tailwind utilities generated in development but stripped in production when classes are built with runtime template literals in Svelte components? What reliable patterns let me keep dynamic class composition without losing utilities in production builds?
Verified community troubleshooting
Start by removing runtime string concatenation for class names: map dynamic values to explicit Tailwind utilities or use Svelte's class: directives so class names are present at build time. If you must keep patterns, add a tight safelist in tailwind.config.js that matches only the variants you need (avoid overly broad regexes). Rebuild and verify by grepping the generated CSS for one of the expected classes or loading the preview build. Roll back the safelist when you convert components to static-class mappings.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development37% match
Tailwind classes generated from runtime template literals missing in production build (Next.js / React)
Open0 answers - Web Development35% match
Tailwind JIT classes stripped in Next.js production build when using dynamic template literals for class names
Open0 answers - Web Development29% match
Next.js production build omits Tailwind classes generated from className arrays joined at runtime
Solved1 answer