Skip to main content
The @tailwindcss/webpack loader integrates Tailwind CSS v4 into webpack projects with automatic CSS generation, caching, and optimization.

Installation

Basic Usage

webpack.config.js
Then create a CSS file that imports Tailwind:
src/index.css

Loader Options

The webpack loader accepts an options object to customize its behavior.

Type Definition

Configuration

string
The base directory to scan for class candidates. The scanner will look for source files relative to this path.Default: process.cwd()
boolean | { minify?: boolean }
Controls CSS optimization using Lightning CSS. By default, optimization is enabled in production (when NODE_ENV=production).
  • true - Enable optimization and minification
  • false - Disable optimization completely
  • { minify: false } - Enable optimization but disable minification
Default: 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

Parameters:
  • this - Webpack loader context with methods and properties
  • source - Raw CSS content as a string
Returns:
  • 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:
Cache Key Format:

Compilation Process

  1. Quick Bail Check:
    • Test for Tailwind at-rules using regex
    • Exit early if no Tailwind directives found
    • Improves performance for non-Tailwind CSS
  2. Context Retrieval:
    • Get or create cache entry for input file
    • Determine if this is initial build or rebuild
  3. 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
  4. Rebuild Detection:
    • Compare file modification times
    • Trigger full rebuild if dependencies changed
    • Otherwise perform incremental build
  5. Scanner Setup:
    • Initialize scanner with source patterns
    • Configure based on @source directives
    • Default to {base}/**/* if no root specified
  6. Candidate Scanning:
    • Scan source files for class names
    • Accumulate candidates in Set
    • Register files as dependencies with webpack
  7. 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:
Registers individual files (config, plugins, source files) as dependencies. Changes trigger rebuild. Context Dependencies:
Registers directories for glob pattern watching. Changes to files matching patterns trigger rebuild.

Quick Bail Optimization

Before processing, the loader checks for Tailwind-specific syntax:
If the pattern doesn’t match, the source is returned unchanged, avoiding compilation overhead.

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
Incremental Rebuild:
  • 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:
This prevents global * rules from @property polyfills that would cause build failures in CSS Modules.

Source Validation

The loader validates @source directive paths:
Ensures source paths are directories, not files.

Error Handling

The loader implements comprehensive error handling:

Compilation Errors

Error Recovery:
  • 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:
This ensures changes to config files trigger rebuilds even after errors.

Optimization Pipeline

When optimization is enabled:
Lightning CSS Processing:
  1. Vendor prefix addition
  2. Modern CSS syntax transformation
  3. Optional minification
  4. Dead code elimination

Loader Context

The loader uses these webpack context methods:
function
Gets async callback for returning results
function
Retrieves loader options from webpack config
string
Absolute path to the resource being loaded
function
Registers file dependency for watching
function
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

Clears Node.js require cache only for changed files, avoiding full cache invalidation.

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 the DEBUG environment variable:
Debug output includes timing for:
  • 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:

Missing Dependencies

The loader doesn’t install webpack or css-loader:

Cache Issues

If builds seem stale, clear webpack’s cache: