# Programmatic API

Start by installing PurgeCSS as a dev dependency.

npm i -D purgecss
1

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']
})
1
2
3
4
5

# CommonJS Syntax

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

The format of purgeCSSResult is

[
    {
        file: 'main.css',
        css: '/* purged css for main.css */'
    },
    {
        file: 'animate.css',
        css: '/* purged css for animate.css */'
    }
]
1
2
3
4
5
6
7
8
9
10

The type of the result is

interface ResultPurge {
  css: string;
  file?: string;
  rejected?: string[];
}
1
2
3
4
5