@tailwindcss/webpack loader integrates Tailwind CSS v4 into webpack projects with automatic CSS generation, caching, and optimization.
Installation
Basic Usage
webpack.config.js
src/index.css
Loader Options
The webpack loader 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'Examples
Custom Base Directory
Change where the loader searches for source files:webpack.config.js
Disable Optimization
Keep CSS unoptimized for easier debugging:webpack.config.js
Optimization Without Minification
Use Lightning CSS for vendor prefixing but keep output readable:webpack.config.js
Loader Architecture
The webpack loader is implemented as an asynchronous loader function that processes CSS files.Function Signature
this- Webpack loader context with methods and propertiessource- Raw CSS content as a string
- Calls
callback(error, result)asynchronously - Never returns a value directly
Internal Behavior
Caching Strategy
The loader uses an LRU cache (max 50 entries) to store compilation state across builds:Compilation Process
-
Quick Bail Check:
- Test for Tailwind at-rules using regex
- Exit early if no Tailwind directives found
- Improves performance for non-Tailwind CSS
-
Context Retrieval:
- Get or create cache entry for input file
- Determine if this is initial build or rebuild
-
Compiler Setup:
- Create compiler if needed
- Clear require cache for changed dependencies
- Configure with file path and base directory
- Enable URL rewriting for asset paths
-
Rebuild Detection:
- Compare file modification times
- Trigger full rebuild if dependencies changed
- Otherwise perform incremental build
-
Scanner Setup:
- Initialize scanner with source patterns
- Configure based on
@sourcedirectives - Default to
{base}/**/*if no root specified
-
Candidate Scanning:
- Scan source files for class names
- Accumulate candidates in Set
- Register files as dependencies with webpack
-
CSS Building:
- Build CSS from accumulated candidates
- Apply optimizations if enabled
- Return generated CSS via callback
Webpack Integration
The loader integrates with webpack’s dependency tracking: File Dependencies:Quick Bail Optimization
Before processing, the loader checks for Tailwind-specific syntax:Rebuild Strategy
The loader determines whether a full rebuild is needed: Full Rebuild Triggers:- Config file changes (detected via mtime)
- Plugin file changes
- Imported CSS changes
- Input CSS file changes
- Previous build errors
- Only new candidates are added
- Compiler and scanner are reused
- Significantly faster for large projects
CSS Module Support
The loader automatically detects CSS Module files and adjusts polyfills:* rules from @property polyfills that would cause build failures in CSS Modules.
Source Validation
The loader validates@source directive paths:
Error Handling
The loader implements comprehensive error handling:Compilation Errors
- Cache entry is deleted to force full rebuild
- Error is passed to webpack for reporting
- Next build will start fresh
Dependency Tracking on Error
Even when compilation fails, dependencies are still registered:Optimization Pipeline
When optimization is enabled:- Vendor prefix addition
- Modern CSS syntax transformation
- Optional minification
- Dead code elimination
Loader Context
The loader uses these webpack context methods:Gets async callback for returning results
Retrieves loader options from webpack config
Absolute path to the resource being loaded
Registers file dependency for watching
Registers directory dependency for watching
TypeScript Support
The loader includes full TypeScript definitions:Performance Optimizations
Incremental Scanning
- Candidates accumulate across rebuilds
- Scanner state persisted between builds
- Only changed files trigger rescanning
Require Cache Management
Lazy Initialization
- Compiler created on first CSS file
- Scanner initialized when needed
- Candidates accumulated incrementally
Compatibility
- webpack: 5.0.0 or higher
- Node.js: 18.0.0 or higher
- Tailwind CSS: v4.0.0 or higher
Framework Integration
Next.js
Next.js uses webpack internally:next.config.js
Create React App
With CRACO or eject:craco.config.js
Vue CLI
vue.config.js
Debugging
Enable debug logging with theDEBUG environment variable:
- Quick bail checks
- Compiler setup
- Scanner initialization
- Candidate scanning
- CSS building
- Optimization passes
- Dependency registration
Common Issues
Loader Order
Ensure@tailwindcss/webpack runs before css-loader: