Guide
Remove unused CSS with PurgeCSS
This guide is for the prebuilt file or a build with `$generate-utility-classes: true`, which contains every utility class. The default SCSS entry needs no purge: it emits only tokens and base styles (5.8 KB gzipped), and component styles add only the rules you write. If you do ship every class, keep them while you develop and ship only the classes your markup uses. Tested with React and Vite, with identical computed styles.
Install
Start from the React with Vite setup. @fullhuman/postcss-purgecss is a PostCSS plugin, and Vite loads it from postcss.config.js with no change to vite.config.ts.
npm install -D sass @wovenui/core @fullhuman/postcss-purgecssSet up
Write the configuration once
A partial that forwards
@wovenui/core/mixinswith your settings. The mixins entry has the same API as@wovenui/core(apply(), every mixin,token(),with()) and outputs no CSS.src/styles/_woven.scss@forward "@wovenui/core/mixins" with ($brand: #4361ee, $generate-utility-classes: true);Create the global stylesheet
It emits the tokens, the reset, the base styles and the utility classes with your configuration.
@use "woven"comes first, so core loads with the configured brand. Keep the utility classes in this one file, so the purge can target it alone.src/styles/global.scss@use "woven"; @use "@wovenui/core";Purge the global stylesheet in production builds
WovenUI class names contain
@(p-4@md),:(hover:bg-brand,group-hover:bg-blue) and.(p-0.5with a decimal spacing key), so the extractor keeps those characters inside a token.dynamicAttributeskeeps everydata-themeanddata-contrastblock, because the theme is often set from a script. Data attributes your scripts set need one more setting, covered below in Data attributes set from scripts.:targetmatches no element in the markup, so it is safelisted. The wrapper runs only invite buildand only onglobal.scss: CSS modules have hashed class names that never appear in the source, and they hold only your own rules.postcss.config.jsimport purgeCSSPlugin from '@fullhuman/postcss-purgecss' const purge = purgeCSSPlugin({ content: ['./index.html', './src/**/*.{js,jsx,ts,tsx}'], defaultExtractor: (content) => content.match(/[\w@:.-]+(?<![.:])/g) ?? [], dynamicAttributes: ['data-theme', 'data-contrast'], safelist: [':target', /^text-(sm|md|lg)$/], }) export default { plugins: [ { postcssPlugin: 'purge-global-stylesheet', OnceExit(root, helpers) { if (process.env.NODE_ENV !== 'production') return if (!/[\\/]global\.scss$/.test(root.source?.input.file ?? '')) return return purge.OnceExit(root, helpers) }, }, ], }
Classes built from strings
PurgeCSS reads the source files as text. A complete class name anywhere in a file is kept, in a ternary or an object of variants alike. A name assembled from parts, such as text-${size}, is never written out, so it is removed. Write complete names, or add them to safelist as strings or regular expressions.
// Kept: complete names
const tone = { paid: 'bg-success-subtle text-success', due: 'bg-warning-subtle text-warning' }
// Removed unless safelisted: text-lg is never written out
<p className={`text-muted text-${size}`}>Dark section</p>safelist: [':target', /^text-(sm|md|lg)$/],Data attributes set from scripts
A rule such as .status[data-state=error] is kept only when PurgeCSS finds the attribute name data-state in the scanned files. A script that sets it through dataset, as in el.dataset.state = 'error', writes state, never data-state, so PurgeCSS removes the rule and the style disappears in production with no error. There are two fixes. List each attribute in dynamicAttributes: precise, it keeps only the attributes you name, but each new attribute must be added by hand. Or add greedy: [/^data-/] to the safelist: every rule with a data- attribute is kept, so an attribute set from a script is never lost, at the cost of rules you may not use. PurgeCSS tests greedy patterns against the bare attribute name, without brackets, so /^\[data-/ matches nothing; write /^data-/.
dynamicAttributes: ['data-theme', 'data-contrast', 'data-state'],safelist: {
standard: [':target', /^text-(sm|md|lg)$/],
greedy: [/^data-/],
},How it was tested
A test page used responsive classes (p-8@lg, hide@md, cols-3@md), state classes (hover:bg-brand-subtle, focus:border-brand, hover:opacity-75@sm), group-hover:bg-blue, compound classes (flex col row@md, .flex > .grow, flex stretch, fill-6), decimal keys (p-0.5@xl, mt-0.5, with a 0.5 spacing key), tooltips, data-theme="dark" and "inverse" sections, animate-spin and a CSS module. All 113 rules that apply to the page were kept with the same declarations. The computed styles of every element, pseudo-elements included, were identical before and after the purge at 375, 800 and 1440 pixels wide, with no theme, data-theme="dark" and "light", in light and dark system modes, on hover and on focus. The keyframes, the @property registrations, the @layer order and every :root and [data-theme] block are kept.
npm run buildWhat gets emitted
In the sample (every utility class on, Vite production build) the purged stylesheet keeps only the classes the page uses, with computed styles identical to the unpurged one.
Reset and base rules for elements the page never uses, and tooltip positions it never sets, are removed too. Add an element or a class to the source and the next build keeps its rules. Development builds skip the purge, so every class is available while you work.