@tailwindcss/postcss plugin integrates Tailwind CSS v4 into any PostCSS-based build pipeline with automatic CSS generation, caching, and optimization.
Installation
Basic Usage
postcss.config.js
src/index.css
Plugin Options
The PostCSS plugin accepts an options object to customize its behavior.Type Definition
Configuration
The base directory to scan for class candidates. The scanner will look for source files relative to this path.Default:
process.cwd()Controls CSS optimization using Lightning CSS. By default, optimization is enabled in production (when
NODE_ENV=production).true- Enable optimization and minificationfalse- Disable optimization completely{ minify: false }- Enable optimization but disable minification
process.env.NODE_ENV === 'production'Enable or disable asset URL rewriting for
url() functions in CSS. When enabled, the plugin handles @import resolution and rewrites relative URLs automatically.Disable this if your bundler or framework provides its own URL rewriting (e.g., webpack, Vite).Default: trueExamples
Custom Base Directory
Change where the plugin searches for source files:postcss.config.js
Disable Optimization
Keep CSS unoptimized for easier debugging:postcss.config.js
Optimization Without Minification
Use Lightning CSS for vendor prefixing but keep output readable:postcss.config.js
Disable URL Rewriting
Let your bundler handleurl() resolution:
postcss.config.js
Plugin Architecture
The PostCSS plugin is composed of multiple internal plugins that process your CSS:1. Fix Relative Paths Plugin
Runs before the main Tailwind plugin to handle cases wherepostcss-import has already processed @import statements. This ensures relative paths remain correct after imports are resolved.
2. Main Tailwind Plugin
Processes Tailwind directives and generates utility classes: Plugin Name:tailwindcss
Hook: Once (processes the entire stylesheet once)
Process:
- Quick bail check for non-Tailwind CSS files
- Retrieve or create compilation context from cache
- Determine rebuild strategy (full or incremental)
- Set up or reuse Tailwind compiler
- Scan source files for class candidates
- Build CSS from candidates
- Optionally optimize with Lightning CSS
- Update PostCSS AST with generated CSS
Internal Behavior
Caching Strategy
The plugin uses an LRU cache (max 50 entries) to store compilation state:Rebuild Strategy
The plugin determines whether a full rebuild is needed by:- Tracking modification times (
mtimeMs) of all dependencies - Comparing current mtimes with cached values
- Triggering full rebuild if any file has changed
- Otherwise, performing incremental build
- Config file changes
- Plugin file changes
- Imported CSS changes
- Input CSS file changes
- Build errors (forces next build to be full)
AST Transformation
The plugin converts between three AST formats:- PostCSS AST → Tailwind CSS AST
- Converts on initial compilation
- Extracts Tailwind directives
- Tailwind CSS AST → PostCSS AST
- Converts generated utilities back to PostCSS
- Preserves source positions for error reporting
- CSS String → PostCSS AST
- Used when optimization is enabled
- Lightning CSS outputs string, parsed back to PostCSS
Dependency Tracking
The plugin registers dependencies with PostCSS for proper rebuilds: Dependency Types:Standard file dependency
Directory glob dependency
Quick Bail Optimization
Before processing, the plugin checks for Tailwind-specific at-rules:CSS Module Support
The plugin automatically detects CSS Module files (.module.css) and disables the @property polyfill to prevent global * rules that would cause build failures.
Error Handling
The plugin implements robust error handling:-
Compilation Errors:
- Logged to console
- Context cache is cleared
- Dependencies still registered for rebuild
- Error converted to PostCSS error with position info
-
Recovery:
- Failed builds force next build to be full rebuild
- Require cache cleared for changed dependencies
- Scanner and compiler reset on error
Optimization Pipeline
Whenoptimize is enabled:
- AST to CSS: Convert Tailwind AST to CSS string
- Lightning CSS: Process CSS for vendor prefixes, minification
- CSS to PostCSS AST: Parse optimized CSS back to PostCSS AST
- Cache Result: Store optimized AST for reuse
- Direct AST Conversion: Convert Tailwind AST directly to PostCSS AST
- No String Conversion: Avoids parse/stringify overhead
- Preserves Formatting: Maintains original indentation (2 spaces)
PostCSS Messages
The plugin communicates with PostCSS and other plugins through messages:- Build tools for file watching
- Other PostCSS plugins for dependency tracking
- Frameworks for hot module replacement
TypeScript Support
The plugin includes full TypeScript definitions:Compatibility
- PostCSS: 8.5.6 or higher
- Node.js: 18.0.0 or higher
- Tailwind CSS: v4.0.0 or higher
Framework Integration
Next.js
next.config.js
Remix
postcss.config.js
Astro
astro.config.mjs
Debugging
Enable debug logging with theDEBUG environment variable:
- Quick bail checks
- Compiler setup
- Scanner initialization
- Candidate scanning
- CSS building
- AST transformations
- Optimization passes