PurgeCSSPurgeCSS
API Reference
GitHub
API Reference
GitHub
  • PurgeCSS

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

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

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

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

Programmatic API

Start by installing PurgeCSS as a dev dependency.

:::: code-group ::: code-group-item NPM

npm install purgecss --save-dev

::: ::: code-group-item YARN

yarn add purgecss --dev

::: ::::

You can now use PurgeCSS inside a JavaScript file.

In the following examples, the options passed to PurgeCSS are the same as the ones here. The result purgecssResult is an array of an object containing the name of the files with the purged CSS.

Usage

ES Module Import Syntax

import { PurgeCSS } from 'purgecss'
const purgeCSSResult = await new PurgeCSS().purge({
  content: ['**/*.html'],
  css: ['**/*.css']
})

CommonJS Syntax

const { PurgeCSS } = require('purgecss')
const purgeCSSResult = await new PurgeCSS().purge({
  content: ['**/*.html'],
  css: ['**/*.css']
})

The format of purgeCSSResult is

[
    {
        file: 'main.css',
        css: '/* purged css for main.css */'
    },
    {
        file: 'animate.css',
        css: '/* purged css for animate.css */'
    }
]

The type of the result is

interface ResultPurge {
  css: string;
  file?: string;
  rejected?: string[];
  rejectedCss?: string;
}
Edit this page
Last Updated:
Contributors: Floriel, Kevin Ramharak, Ffloriel, Sepp Wikramaratna-Clarke, Ant Stanley
Prev
Command Line Interface
Next
Safelisting