PurgeCSSPurgeCSS
API Reference
  • English
  • Français
GitHub
API Reference
  • English
  • Français
GitHub
  • PurgeCSS

    • About PurgeCSS
    • Getting Started
    • Configuration
    • Command Line Interface
    • Programmatic API
    • Safelisting
    • Extractors
  • Plugins

    • PostCSS
    • Webpack
    • Rollup
    • Gulp
    • Grunt
    • Gatsby
  • Guides

    • Vue
    • React
    • Angular
    • Next.js
    • Nuxt.js
    • Razzle
    • WordPress
    • Hugo
  • Comparison
  • Common Questions

    • How to use with CSS modules
    • How to use with Ant Design

Gulp

Installation

npm i -D gulp-purgecss
npm install --save-dev gulp-purgecss

Usage

By default, purgecss outputs the source CSS with unused selectors removed:

const gulp = require('gulp')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss', () => {
    return gulp.src('src/**/*.css')
        .pipe(purgecss({
            content: ['src/**/*.html']
        }))
        .pipe(gulp.dest('build/css'))
})

With gulp 5, write the task with pipeline to get the errors of the plugin reported, see Errors.

By setting the rejected option, you can 'invert' the output to list only the removed selectors:

const gulp = require('gulp')
const rename = require('gulp-rename')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss-rejected', () => {
    return gulp.src('src/**/*.css')
        .pipe(rename({
            suffix: '.rejected'
        }))
        .pipe(purgecss({
            content: ['src/**/*.html'],
            rejected: true
        }))
        .pipe(gulp.dest('build/css'))
})

Source maps

The plugin updates the source map of the files that have one, so that it still points to the original CSS. Create the source maps with the sourcemaps option of gulp:

const gulp = require('gulp')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss', () => {
    return gulp.src('src/**/*.css', { sourcemaps: true })
        .pipe(purgecss({
            content: ['src/**/*.html']
        }))
        .pipe(gulp.dest('build/css', { sourcemaps: '.' }))
})

or with gulp-sourcemaps:

const gulp = require('gulp')
const sourcemaps = require('gulp-sourcemaps')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss', () => {
    return gulp.src('src/**/*.css')
        .pipe(sourcemaps.init())
        .pipe(purgecss({
            content: ['src/**/*.html']
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('build/css'))
})

Errors

With gulp 5, an error of the plugin, e.g. for invalid CSS or a content path that cannot be read, is not always reported when the task is a chain of .pipe(). gulp may only print:

The following tasks did not complete: purgecss
Did you forget to signal async completion?

Every plugin that fails is affected, see gulpjs/gulp#2812. Write the task with pipeline of Node.js, which reports the error of any of its streams:

const { pipeline } = require('node:stream/promises')
const gulp = require('gulp')
const purgecss = require('gulp-purgecss')

gulp.task('purgecss', () => {
    return pipeline(
        gulp.src('src/**/*.css'),
        purgecss({
            content: ['src/**/*.html']
        }),
        gulp.dest('build/css')
    )
})

Options

The plugin accepts the PurgeCSS options, except css, which is replaced by the files of the stream. Some of them work differently:

  • content

An array of paths and globs of the content files, e.g. ['src/**/*.html', 'src/**/*.js']. They are relative to the current working directory, which the gulp CLI sets to the directory of the gulpfile. The files matching skippedContentGlobs are not used. When no file matches, PurgeCSS prints a warning and all the selectors are removed.

  • rejected

When true, the content of each file is replaced by the list of its removed selectors, one selector {} per line.

  • sourceMap

The option has no effect: the source maps are created with gulp, see Source maps.

The rejectedCss, output, stdin and stdout options have no effect either.

Tips

Each file of the stream is purged on its own. With fontFace, keyframes or variables, a @font-face, @keyframes or CSS variable only used by another file is removed. To keep it, concatenate the files before purging them, e.g. with gulp-concat.

Edit this page
Last Updated: 11/21/26, 9:46 AM
Prev
Rollup
Next
Grunt