Web Development

Tailwind JIT utilities missing in SvelteKit production build when class names use runtime template literals

elincode
4w ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
marcuslinux Answer
user
Posted 4w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Tailwind content extractor cannot statically detect class names inside runtime template literals, so JIT omits them at build time 90%
Svelte/Vite preprocessing transforms templates before Tailwind scans, hiding class text from the extractor 60%
Incorrect content globs in tailwind.config.js excluding some files 30%

Summary

Tailwind's JIT/content scanner performs static analysis of source files to collect the class names to generate; runtime template literals (especially with variable segments) can't be determined statically and are therefore dropped from the production CSS unless explicitly safelisted. Svelte's preprocessor and Vite may transform templates before Tailwind scans them, further hiding the raw class strings. Minifiers/optimizers are less likely culprits when dev works and production CSS lacks classes.

Suggested Fixes

Diagnostic step 1
Replace runtime template literals with a mapping object of allowed class strings (e.g., const sizeClass = {sm:'text-sm', md:'text-md'}; class={sizeClass[size]}) or conditional class: directives in Svelte to make classes static.
Diagnostic step 2
Add an explicit safelist block in tailwind.config.js for the dynamic patterns you need (use specific lists or precise regexes) and rebuild to verify.
Diagnostic step 3
Configure Tailwind's content extractor to include preprocessed output if your build chain alters source before scanning, or adjust Svelte preprocess order so Tailwind sees raw templates.

Continue troubleshooting

Matched from the title, description and tags