@tailwindcss/postcss plugin allows you to integrate Tailwind CSS into any build tool that supports PostCSS, including Next.js, webpack, Parcel, and more.
Installation
Configure PostCSS
Add the plugin to your Or using ES modules:
postcss.config.js:postcss.config.js
postcss.config.mjs
Configuration
The PostCSS plugin accepts several configuration options:postcss.config.js
Options
The base directory to scan for class candidates.Default:
process.cwd() (current working directory)Enable CSS optimization and minification.
false- No optimizationtrue- Full optimization with minification{ minify: false }- Optimize without minifying
true when NODE_ENV === 'production', otherwise falseEnable or disable asset URL rewriting for relative paths.Default:
trueHow It Works
The PostCSS plugin processes your CSS in the PostCSS pipeline:- Detects Tailwind CSS - Identifies files that use Tailwind directives
- Parses CSS - Converts PostCSS AST to Tailwind’s internal AST
- Compiles utilities - Generates CSS for detected Tailwind classes
- Scans content - Finds all Tailwind classes in your source files
- Optimizes - Optionally minifies the output using Lightning CSS
- Returns CSS - Converts back to PostCSS AST for further processing
Caching
The plugin uses intelligent caching to speed up rebuilds:- Compiler cache - Reuses the compiler instance when possible
- Scanner cache - Maintains a list of scanned files
- CSS cache - Avoids regenerating unchanged CSS
- Optimization cache - Caches optimized output
- CSS files are modified
- Configuration files change
- Plugin files are updated
- Template files are added or removed
Dependency Tracking
The plugin automatically tracks dependencies and reports them to PostCSS:Content Detection
The plugin automatically scans your project for Tailwind classes:@source directive:
Integration Examples
Next.js
postcss.config.js
_app.js or layout.js:
app/layout.js
Webpack
Withcss-loader and postcss-loader:
webpack.config.js
Parcel
Parcel automatically detectspostcss.config.js. Just import your CSS:
src/index.js
Laravel Mix
webpack.mix.js
PostCSS Import
The plugin works seamlessly withpostcss-import:
postcss.config.js
styles.css
Optimization
Development
Disable optimization for faster builds:postcss.config.js
Production
Enable optimization with minification:postcss.config.js
Optimize Without Minification
postcss.config.js
CSS Modules
The plugin automatically detects CSS Module files (*.module.css) and disables the @property polyfill to avoid build errors with non-pure selectors.
Button.module.css
Rebuild Strategies
The plugin uses two rebuild strategies:Incremental Rebuild
When only template files change:- Scans changed files for new candidates
- Rebuilds CSS only if new classes are found
- Very fast, typically under 10ms
Full Rebuild
When CSS, config, or plugin files change:- Clears the require cache
- Recreates the compiler
- Scans all content files
- Rebuilds the entire CSS output
The plugin uses an AST-to-AST transformation between PostCSS and Tailwind’s internal representation for optimal performance.
Troubleshooting
Changes Not Detected
If your build tool isn’t detecting changes:- Ensure your build tool is configured to watch CSS files
- Check that dependency messages are being processed
- Verify the
baseoption points to the correct directory
Slow Builds
For large projects:- Use specific content patterns with
@source - Exclude unnecessary directories (like
node_modules) - Disable optimization in development
- Consider using the Vite plugin for better performance
Import Errors
If you see import-related errors:- Ensure
postcss-importruns before@tailwindcss/postcss - Check that all imported files exist
- Verify relative paths are correct