@tailwindcss/webpack loader provides direct integration with webpack, processing your CSS files and enabling efficient rebuilds with webpack’s caching system.
Installation
Configuration
The webpack loader accepts configuration options:webpack.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 falseHow It Works
The webpack loader integrates into webpack’s module pipeline:- Intercepts CSS files - Processes files that import Tailwind CSS
- Compiles Tailwind - Generates CSS for all detected utilities
- Scans content - Finds Tailwind classes in your source files
- Reports dependencies - Tells webpack about all relevant files
- Optimizes output - Optionally minifies using Lightning CSS
- Returns CSS - Passes the processed CSS to the next loader
Caching
The loader uses an LRU cache to store:- Compiler instances - Reused across rebuilds
- Scanner instances - Maintained between builds
- Candidate sets - Accumulated over time
- File modification times - For change detection
- Input file path
- Base directory
- Optimization settings
Dependency Tracking
The loader automatically reports dependencies to webpack:Content Detection
The loader scans your project for Tailwind classes:@source directive:
Rebuild Strategies
The loader uses intelligent rebuild strategies:Incremental Rebuild
When only template files change:- Scans changed files for new candidates
- Rebuilds CSS only if new classes are detected
- Very fast, leverages webpack’s incremental compilation
Full Rebuild
When CSS, config, or plugin files change:- Clears Node.js require cache for affected files
- Recreates the compiler instance
- Scans all content files again
- Rebuilds the complete CSS output
Optimization
Development Mode
Disable optimization for faster builds:webpack.config.js
Production Mode
Enable optimization with minification:webpack.config.js
Optimize Without Minification
webpack.config.js
CSS Modules
The loader automatically detects CSS Module files (*.module.css) and disables the @property polyfill to avoid build errors:
Button.module.css
webpack.config.js
Integration Examples
Basic Setup
webpack.config.js
With MiniCssExtractPlugin
webpack.config.js
With React
webpack.config.js
With TypeScript
webpack.config.js
Development Server
Use webpack-dev-server for live reloading:webpack.config.js
Error Handling
The loader handles errors gracefully:- Compilation errors are reported to webpack
- Cache is cleared on errors to force a full rebuild
- Build continues with other modules when possible
The loader uses the same Rust-based scanner as other Tailwind integrations for consistent performance and behavior.
Troubleshooting
Slow Builds
For large projects:- Use specific content patterns with
@source - Ensure
node_modulesis excluded from scanning - Disable optimization in development mode
- Use webpack’s caching options
Cache Issues
If you’re seeing stale CSS:- Clear webpack’s cache:
rm -rf node_modules/.cache - Restart webpack-dev-server
- Check that file watching is working correctly
Dependencies Not Tracked
If webpack isn’t rebuilding when files change:- Verify the
baseoption is correct - Check that files are within the base directory
- Ensure webpack’s watch options are configured
- Look for errors in the webpack output
CSS Not Generated
If Tailwind CSS isn’t being processed:- Ensure
@import 'tailwindcss'is in your CSS file - Check that the loader is configured for
.cssfiles - Verify the loader runs before
css-loader - Check for errors in the webpack console