@adoratorio/postcss-prune-vars
v0.1.0
Published
PostCSS plugin to remove unused CSS custom properties, with content file scanning support
Downloads
41
Readme
postcss-prune-vars
A PostCSS plugin that removes unused CSS custom properties, with content file scanning support for Vue, React, and plain HTML projects.
Installation
npm install @adoratorio/postcss-prune-varsUsage
import pruneVars from '@adoratorio/postcss-prune-vars';
// In your PostCSS config
const plugins = [
pruneVars({ dirs: ['./src'] }),
];Why
Tools like PurgeCSS and postcss-prune-var only analyze CSS to determine which custom properties are used. Variables referenced in template inline styles (style="color: var(--color-primary)") are invisible to them and get incorrectly removed.
This plugin scans both CSS declarations and content files (Vue templates, JSX, HTML) to build a complete picture of variable usage before pruning.
Configuration
PluginOptions
| Parameter | Type | Default | Description |
| :-------: | :--: | :-----: | :---------- |
| dirs | string[] | ['./src'] | Directories to scan for content files |
| extensions | string[] | ['.vue', '.js', '.ts', '.jsx', '.tsx', '.html'] | File extensions to scan |
How It Works
- Scans content files in the configured directories for
var(--name)references - Scans CSS declarations for
var(--name)in non-custom-property rules (e.g.color: var(--text)) - Resolves dependency chains — if
--color-primary: var(--color-dark)is used, both variables are preserved - Removes unused declarations — any
--variablenot referenced directly or through a chain is removed - Cleans up empty nodes — empty
:root,@layer, and@mediablocks are removed
Integration
Vite + Vue
import { defineConfig } from 'vite';
import purgecss from '@fullhuman/postcss-purgecss';
import pruneVars from '@adoratorio/postcss-prune-vars';
export default defineConfig({
css: {
postcss: {
plugins: [
...(process.env.NODE_ENV === 'production'
? [
purgecss({ /* ... */ }),
pruneVars({ dirs: ['./src'] }),
]
: []),
],
},
},
});Nuxt
export default defineNuxtConfig({
postcss: {
plugins: {
'@adoratorio/postcss-prune-vars': process.env.NODE_ENV === 'production'
? { dirs: ['./components', './layouts', './pages'] }
: false,
},
},
});TypeScript Support
The plugin is written in TypeScript and includes full type definitions:
import type { PluginOptions } from '@adoratorio/postcss-prune-vars';