Skip to main content

@roots/bud-purgecss

PurgeCSS support can be added by installing the @roots/bud-purgecss extension.

Styles are only purged when running in production mode.

Installation​

npm install @roots/bud-purgecss --save-dev

Configuration​

You can configure PurgeCSS using the API provided by bud.purge.

See the PurgeCSS documentation to learn how to configure PurgeCSS.

For every option detailed in the PurgeCSS docs there is a corresponding method in bud.purge.

PurgeCSS optionConfiguration method
blocklistbud.purge.setBlocklist
contentbud.purge.setContent
cssbud.purge.setCss
defaultExtractorbud.purge.setDefaultExtractor
dynamicAttributesbud.purge.setDynamicAttributes
extractorsbud.purge.setExtractors
fontFacebud.purge.setFontFace
keyframesbud.purge.setKeyframes
outputbud.purge.setOutput
rejectedbud.purge.setRejected
rejectedCssbud.purge.setRejectedCss
sourceMapbud.purge.setSourceMap
variablesbud.purge.setVariables
safelistbud.purge.setSafelist
skippedContentGlobsbud.purge.setSkippedContentGlobs
stdinbud.purge.setStdin
stdoutbud.purge.setStdout
variablesbud.purge.setVariables

Some of the most common options are demonstrated below.

Set blocklist​

bud.config.ts
import type {Bud} from '@roots/bud'

export default async (bud: Bud) => {
bud.purge.setBlocklist(['random', 'yep'])
}

Set safelist​

bud.config.ts
import type {Bud} from '@roots/bud'

export default async (bud: Bud) => {
bud.purge.setSafelist(['.example', '.example-2'])
}

Set content​

bud.config.ts
import type {Bud} from '@roots/bud'

export default async (bud: Bud) => {
bud.purge.setContent(['index.html', '**/*.js', '**/*.html', '**/*.vue'])
}

Set CSS​

bud.config.ts
import type {Bud} from '@roots/bud'

export default async (bud: Bud) => {
bud.purge.setCss(['**/*.css'])
}

WordPress configuration​

Install the purgecss-with-wordpress package to use the PurgeCSS managed safelist.

npm install purgecss-with-wordpress --save-dev

See the PurgeCSS guide for more informationon how to configure PurgeCSS for use in WordPress themes and plugins.

An example implementation is provided below:

Example​

import purgecssWordPress from 'purgecss with wordpress'

bud.purge
.setContent([`views/**/*.php`])
.setCss([`assets/styles/**/*.css`])
.setSafelist(purgecssWordPress.safelist)

Defaults​

By default, this extension uses the following configuration:

bud.purge.setContent([
bud.path(`@src`, `*.{html,js,jsx,php,pug,rb,ts,tsx,vue}`),
bud.path(`@src`, `**`, `*.{html,js,jsx,php,pug,rb,ts,tsx,vue}`),
])