@tailwindcss/vite plugin provides seamless integration with Vite, automatically processing your CSS files and enabling hot module replacement (HMR) for fast development.
Installation
Configuration
The Vite plugin accepts configuration options:vite.config.ts
Options
Enable CSS optimization and minification.
false- Disable optimization (default in development)true- Enable optimization with minification (default in build mode){ minify: false }- Optimize without minifying
How It Works
The Vite plugin operates in two modes:Development Mode (serve)
In development, the plugin:- Intercepts CSS files - Transforms files that import Tailwind CSS
- Scans for candidates - Automatically detects Tailwind classes in your source files
- Generates CSS - Builds utilities on-demand
- Enables HMR - Provides hot module replacement for instant updates
- Watches files - Monitors template files (HTML, JSX, Vue, etc.) for changes
Build Mode
During production builds, the plugin:- Processes CSS - Transforms all CSS files that use Tailwind
- Scans all sources - Detects all Tailwind classes across your entire project
- Generates optimized CSS - Builds the final CSS with all utilities
- Optimizes output - Minifies and optimizes using Lightning CSS (when enabled)
Source Maps
The plugin automatically generates source maps in development whencss.devSourcemap is enabled in your Vite config:
vite.config.ts
File Watching
The plugin automatically watches:- Template files - HTML, JSX, TSX, Vue, Svelte, Astro files
- CSS files - Your CSS files that import Tailwind
- Config files - Configuration and plugin files
- Custom sources - Files specified via
@sourcedirectives
Full Reload Triggers
Changes to files outside the module graph (like standalone HTML or PHP files) trigger a full page reload automatically.Content Detection
The plugin uses intelligent content detection:@source directive in your CSS:
Optimization
Development
By default, CSS is not optimized in development for faster builds:vite.config.ts
Production
In production, optimization is enabled automatically:vite.config.ts
Custom Optimization
Optimize without minification:vite.config.ts
Framework Integration
React
vite.config.ts
Vue
vite.config.ts
Svelte
vite.config.ts
Environment API Support
The plugin supports both the legacy and modern Vite Environment API (Vite 6+), automatically detecting which API is available.The plugin uses a Rust-based scanner for extremely fast candidate detection and supports Vite’s hot module replacement for instant updates during development.
Troubleshooting
CSS Not Updating
If your CSS isn’t updating in development:- Ensure you’ve imported your CSS file in your app entry point
- Check that the CSS file contains
@import 'tailwindcss' - Verify that your template files are being watched
Build Performance
For large projects, consider:- Disabling optimization in development:
optimize: false - Using specific content patterns with
@sourceto limit scanning - Ensuring your project has a
.gitignoreto excludenode_modules