@tailwindcss/vite plugin integrates Tailwind CSS v4 into your Vite projects with automatic CSS generation, hot module replacement, and build optimization.
Installation
Basic Usage
vite.config.js
src/index.css
Plugin Options
The Vite plugin accepts an options object to customize its behavior.Type Definition
Configuration
Controls CSS optimization using Lightning CSS. By default, optimization is enabled during production builds and disabled in development.
true- Enable optimization and minificationfalse- Disable optimization completely{ minify: false }- Enable optimization but disable minification
config.build.cssMinifyExamples
Disable Optimization
Prevent Lightning CSS from processing your output:vite.config.js
Enable Optimization Without Minification
Use Lightning CSS for vendor prefixing and other optimizations, but keep the CSS readable:vite.config.js
Plugin Architecture
The Vite plugin is implemented as an array of three internal plugins that handle different stages of the build process:1. Scan Plugin (@tailwindcss/vite:scan)
- Enforce:
pre - Purpose: Initializes configuration and sets up the development server
- Key Features:
- Resolves Vite configuration
- Determines optimization settings based on build mode
- Configures server hooks for HMR
2. Generate Plugin (Serve Mode)
Name:@tailwindcss/vite:generate:serve
- Apply:
serve(development mode only) - Enforce:
pre - Purpose: Generates CSS during development with fast incremental builds
- Key Features:
- Hot module replacement support
- File watching for content changes
- Full page reload for external file changes (HTML, PHP, etc.)
- Per-environment CSS generation
3. Generate Plugin (Build Mode)
Name:@tailwindcss/vite:generate:build
- Apply:
build(production mode only) - Enforce:
pre - Purpose: Generates and optimizes CSS for production builds
- Key Features:
- Automatic optimization with Lightning CSS
- Source map generation
- Minification when enabled
Internal Behavior
CSS Root Detection
The plugin automatically detects Tailwind CSS root files by checking for:.cssfile extensions&lang.cssquery parameters- Inline style blocks with CSS
- Files containing Tailwind directives (
@tailwind,@apply,@theme, etc.)
- Are in the
.vite/directory - Have special query parameters (
?worker,?url,?raw, etc.) - Are CommonJS proxy modules
Scanning and Compilation
The plugin uses theRoot class to manage compilation state:
- Check if rebuild is required by comparing file modification times
- Clear Node.js require cache for changed dependencies
- Initialize or reuse Tailwind compiler with CSS resolvers
- Set up scanner based on
@sourcedirectives or default patterns - Scan source files for class candidates
- Build CSS from accumulated candidates
- Generate source maps if enabled
Hot Module Replacement
The plugin provides intelligent HMR behavior: For CSS Files:- Standard HMR updates through Vite’s module graph
- Detected via
addWatchFilewithout module entries - Triggers full page reload when changed
- Validates file is actually scanned by Tailwind roots
- Supports multi-environment updates (client + SSR)
Dependency Tracking
The plugin tracks three types of dependencies:- Build Dependencies: Config files, plugins, imported CSS
- Source Files: Individual files found by scanner
- Glob Patterns: Directory patterns for content scanning
Source Map Support
Source maps are generated in development whencss.devSourcemap is enabled in Vite config:
vite.config.js
Multi-Environment Support
The plugin supports Vite’s multi-environment API:- Separate compilation roots per environment (client, SSR, custom)
- Environment-specific module resolution
- Cross-environment HMR updates
- Backward compatibility with Vite < 7
Performance Optimizations
Incremental Builds
- Candidates are accumulated across rebuilds
- Compiler and scanner are reused when possible
- Only changed dependencies trigger full rebuilds
Caching Strategy
- Build dependencies tracked by modification time
- Scanner state persisted between builds
- Separate caches per environment
File Exclusions
The plugin automatically excludes certain files to avoid issues:- SVG files with
#or?in path (causes Vite crashes) - Input CSS file itself (already a dependency)
- Files in
.vite/directory
TypeScript Support
The plugin is written in TypeScript and includes full type definitions:Compatibility
- Vite: 5.2.0 or higher
- Node.js: 18.0.0 or higher
- Tailwind CSS: v4.0.0 or higher
Debugging
Enable debug logging by setting theDEBUG environment variable:
- CSS generation
- Candidate scanning
- Compiler setup
- Optimization passes
- Source map generation