PurgeCSS & safelist
How to safely strip the CSS you don't use out of molique. A step-by-step guide - from installation to checking nothing broke.
0. Start here: you may not need this at all
PurgeCSS strips CSS rules it couldn't find in your files. It's effective, but it introduces risk. Before you turn it on, check a simpler option - molique is split into separate files, and it's enough not to load the ones you don't use. That's a reduction with zero risk.
| File | Size | When you need it |
|---|---|---|
molique-style.css |
~278 KB | Always The framework core. |
molique-style-admin.css |
~50 KB | Only when building an admin panel. |
molique-style-shop.css |
~10 KB | Only a shop (product cards, cart). |
molique-style-blog.css |
~7 KB | Only a blog and articles. |
molique-style-docs.css |
~8 KB | Only documentation pages. |
A simple rule
First drop the modules you don't use. Only if that's not enough, reach for PurgeCSS. Order matters: the first is free, the second costs attention.
1. Why molique needs a safelist
PurgeCSS reads your files and keeps only the classes it found in them. The problem: some molique classes never appear in any file - JavaScript adds them only in the browser (e.g. when you click an accordion or a notification appears). PurgeCSS can't see them, so it removes them - and the component stops working.
A safelist is a "don't touch this" list. molique ships one ready-made, so you don't have to write it yourself.
| What breaks without the safelist | Symptom |
|---|---|
.is-active and other states |
The active tab, step, or menu item doesn't highlight. |
toast-* |
Notifications appear with no styling (a plain white rectangle). |
lightbox-* |
Enlarging photos looks like bare HTML. |
@keyframes toastProgressAnim |
The countdown bar in a notification stays frozen. |
What it costs (measured)
2. Step-by-step guide
Step 1 Install PurgeCSS
In your terminal, inside your project directory:
npm install --save-dev purgecss
Step 2 Find the safelist file
In the molique package, next to css/ and
js/, there's a
purgecss.safelist.cjs file. Copy it to your
project's root directory - where your
package.json lives.
your-project/
├── package.json
├── purgecss.safelist.cjs ← here
├── css/
│ └── molique-style.css
└── index.html
Step 3 Create the config file
In the root directory, create purgecss.config.js
and paste the content below. This is a complete, working
configuration - nothing to add.
const molique = require('./purgecss.safelist.cjs');
module.exports = {
// Where PurgeCSS should look for class names.
content: [
'./**/*.html',
'./js/**/*.js',
],
// Which CSS file we're trimming.
css: ['./css/molique-style.css'],
// Where to save the result.
output: './css/molique-style.purged.css',
// The "don't touch this" list - without it, components stop working.
safelist: molique.runtime,
// Remove unused animations (safe thanks to the safelist).
keyframes: true,
// Do NOT change to true - the whole theme rests on CSS variables.
variables: false,
};
Step 4 Run it
npx purgecss --config purgecss.config.js
This creates css/molique-style.purged.css. Swap
the reference in your HTML to point at it:
<link rel="stylesheet" href="css/molique-style.purged.css">
Step 5 Check that nothing broke
Open the page and click through everything that uses JavaScript. This list catches practically every problem:
3. Ready-made configs
Choose your environment. They mostly differ in where PurgeCSS
looks for class names (content).
const molique = require('./purgecss.safelist.cjs');
module.exports = {
content: ['./**/*.html', './js/**/*.js'],
css: ['./css/molique-style.css'],
output: './css/molique-style.purged.css',
safelist: molique.runtime,
keyframes: true,
variables: false,
};
Install @fullhuman/postcss-purgecss and add
it as a PostCSS plugin. Purge will only run on
production builds.
// postcss.config.cjs
const purgecss = require('@fullhuman/postcss-purgecss').default;
const molique = require('./purgecss.safelist.cjs');
module.exports = {
plugins: [
...(process.env.NODE_ENV === 'production'
? [
purgecss({
content: ['./index.html', './src/**/*.{html,js}'],
safelist: molique.runtime,
keyframes: true,
variables: false,
}),
]
: []),
],
};
Note: in WordPress a large share of classes live in the database (post content, ACF fields, blocks), not in files. PurgeCSS won't see them. So scan your PHP templates and make sure to read section 4 below.
const molique = require('./purgecss.safelist.cjs');
module.exports = {
content: [
'./wp-content/themes/my-theme/**/*.php',
'./wp-content/plugins/my-plugin/**/*.php',
'./wp-content/themes/my-theme/js/**/*.js',
],
css: ['./wp-content/themes/my-theme/css/molique-style.css'],
output: './wp-content/themes/my-theme/css/molique-style.purged.css',
// Classes assembled in PHP are invisible - see section 4.
safelist: molique.merge('status', 'grid'),
keyframes: true,
variables: false,
};
4. When the backend concatenates class names
This is the most common cause of "half my styles vanished for some reason." If you build a class name from fragments, it never appears in your file whole - PurgeCSS only sees the pieces and strips the rule.
This is what the problem looks like
<span class="badge badge-<?= $status ?>">
Only badge- appears in the file. The name
badge-success is nowhere to be found, so the
rule disappears.
The fix
safelist: molique.merge('status')
Keeps the entire badge-* family, so every
value coming from the database works.
Turn on only the groups you actually use - each one keeps more CSS:
| Group | Covers | Turn on if you concatenate… |
|---|---|---|
'status' |
badge-, status-, stock-bar-, overlay-, opacity- |
statuses from the database, opacity levels |
'grid' |
col-, offset-, grid-cols- |
column widths from a CMS field |
'colors' |
bg-, text-, border- |
colors chosen in a panel |
'spacing' |
m-, p-, gap- |
spacing generated in a loop |
safelist: molique.runtime // minimum
safelist: molique.merge('status') // + statuses from the DB
safelist: molique.merge('status', 'grid') // + columns from the CMS
safelist: molique.all // everything (safest)
A better fix, if you can
Instead of concatenating the name, spell out the full
classes in your code and pick one - then PurgeCSS sees them
and you don't need any extra group:
$map = ['ok' => 'badge-success', 'err' => 'badge-danger'];
5. What not to do
6. Something broke - what to check
Expand the symptom you're seeing.
A component looks like bare HTML (no styling)
Most likely the safelist is missing or not wired up.
Check your config for
safelist: molique.runtime and that the path
to purgecss.safelist.cjs is correct.
A quick test: search the resulting CSS for the phrase
lightbox-overlay. If it's not there - the
safelist didn't take effect.
Colors and the theme fell apart
You have variables: true. Change it to
false and rebuild. molique's theme rests on
CSS variables - removing them takes the whole palette
with them.
Statuses from the database have no colors
The backend is concatenating the class name. Add the
group: safelist: molique.merge('status').
Details in section 4.
The active menu item doesn't highlight
The .is-* state classes dropped out.
They're covered by molique.runtime - check
that the safelist is wired up and that you're not
overwriting it with your own list.
The gain is negligible (the file barely shrank)
You're probably using molique.all or too
many groups. Drop down to
molique.runtime and add groups one at a
time, checking after each that everything still works.