Sub topic 11
Build for production
This chapter assumes you have a working Vite project with tailwindcss and @tailwindcss/vite installed, the plugin registered in vite.config.ts, and @import "tailwindcss"; in your main stylesheet. You should also know the basic npm run workflow.
What changes between dev and build
In development, Vite serves your CSS on demand and updates it as you edit. In a production build, Vite bundles your JavaScript and CSS into static files, adds a content hash to each file name, and writes them to the dist folder.
The Tailwind plugin takes part in both modes. During a build it scans your source files for class names, generates only the CSS those classes need, and minifies the result. You do not run a separate Tailwind command, and you do not configure any content purging.
Run the build
A project created with npm create vite@latest already defines the build script. You can confirm it in package.json:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}Run the build and then serve the output locally:
npm run build
npm run previewvite build writes the bundle to dist. vite preview starts a static server for that folder, so you see what users will receive. Do not treat the dev server as a test of the production output.
Inspect the output
After the build, the dist/assets folder contains a hashed CSS file, for example index-3f9a1c2b.css. The exact hash differs on every project and changes whenever the content changes.
Open that file. You should see the Tailwind base styles (the preflight reset), the theme variables, and rules only for the utilities you used. If your page uses text-3xl, font-bold, underline, and text-blue-600, those rules appear. A class you never wrote, such as bg-pink-200, does not.
The file is small because Tailwind generates CSS from the classes it finds. The result is minified, so it is a few long lines rather than readable blocks.
Common build problems
- Styles work in dev but are missing in the build. A class name is probably built from string pieces, such as
"bg-" + color. Tailwind reads files as plain text and cannot see the final value. Write complete class names in your source, for example"bg-red-500". - Classes from a package are missing. Tailwind skips files it does not detect on its own, such as some dependencies. Add the folder with
@source, as covered in the previous chapter. - The preview page is unstyled. Check that the stylesheet is imported from your entry file, for example
import './style.css'. If the import is missing, Vite does not emit a CSS file. - Assets 404 when deployed to a subfolder. This is a Vite setting, not a Tailwind one. Set
baseinvite.config.tsto match the deployment path.
Deploy the result
The dist folder holds plain static files. You can upload it to any static host. Because file names include content hashes, you can cache them for a long time. A change to your classes produces a new hash, and browsers then fetch the new file.
Exercise
You run npm run build and the output CSS lacks the rule for a class you wrote as ` text-${size} ` in a template string. Why is it missing, and how do you fix it?
Show answer
Tailwind scans source files as plain text and cannot evaluate the template string, so it never sees a complete class name such as text-xl. Write the full class names in the source, for example by mapping each size value to a literal string like "text-xl" or "text-2xl", then rebuild.
Exercise
Which command lets you check the production output locally, and which folder does it serve?
Show answer
npm run preview (which runs vite preview) serves the dist folder created by npm run build.