postcss-remove-duplicate-values
Version:
๐ PostCSS plugin that intelligently removes duplicate CSS properties, reduces bundle size, and improves CSS maintainability. Handles !important declarations, vendor prefixes, and selector filtering with zero configuration.
262 lines (216 loc) โข 5.74 kB
Markdown
"https://npmjs.com/package/postcss-remove-duplicate-values?activeTab=readme"><img src="https://img.shields.io/npm/v/postcss-remove-duplicate-values?style=flat-square&colorA=000000&colorB=8338EC" alt="npm version" /></a>
<a href="https://github.com/xettri/postcss-remove-duplicate-values/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=000000&colorB=8338EC" alt="license" /></a>
</p>
> **Smart PostCSS plugin that removes duplicate CSS properties, reduces bundle size, and improves CSS maintainability.**
Automatically removes duplicate CSS properties from your stylesheets while keeping the most important ones. Perfect for cleaning up CSS and improving performance.
- **๐งน Removes duplicate properties** (keeps the last one)
- **โก Handles `!important` declarations** intelligently
- **๐จ Supports vendor prefixes** and modern CSS
- **๐ฏ Filters specific selectors** (optional)
- **๐๏ธ Cleans empty rules** (configurable)
- **๐ Zero configuration** needed
```bash
npm install postcss-remove-duplicate-values --save-dev
pnpm add postcss-remove-duplicate-values -D
yarn add postcss-remove-duplicate-values -D
```
```js
// postcss.config.js
module.exports = {
plugins: [
require('postcss-remove-duplicate-values')
]
}
```
The plugin automatically removes duplicates from your CSS.
```css
/* Before */
.button {
color: red;
color: blue;
margin: 10px;
margin: 20px;
}
/* After */
.button {
color: blue;
margin: 20px;
}
```
```css
/* Before */
.button {
color: red !important;
color: blue;
font-weight: normal;
font-weight: bold !important;
}
/* After */
.button {
color: red !important;
font-weight: bold !important;
}
```
```css
/* Before */
.button {
transform: translateX(40px);
-webkit-transform: translateX(10px);
-moz-transform: translateX(10px);
transform: translateX(10px);
}
/* After */
.button {
/* Plugin removes duplicate 'transform' properties, keeping the last one */
/* Vendor prefixes are preserved */
-webkit-transform: translateX(10px);
-moz-transform: translateX(10px);
transform: translateX(10px);
}
```
Before applying the plugin, you can configure the following options:
| Option | Type | Default |
| --------------------------------- | --------------------------------------------------- | ----------- |
| [`selector`](
| [`preserveEmpty`](
Filter which CSS selectors to process.
```js
// Only process .button selectors
removeDuplicateValues({
selector: '.button'
})
// Process selectors matching regex
removeDuplicateValues({
selector: /^\.btn-/
})
// Custom function
removeDuplicateValues({
selector: (selector) => selector.includes('button')
})
```
Keep or remove empty CSS rules.
```js
// Remove empty rules (default)
removeDuplicateValues({
preserveEmpty: false
})
// Keep empty rules
removeDuplicateValues({
preserveEmpty: true
})
```
```js
const postcss = require('postcss')
const removeDuplicateValues = require('postcss-remove-duplicate-values')
const css = `
.button {
color: red;
color: blue;
}`
postcss([removeDuplicateValues()])
.process(css)
.then(result => {
console.log(result.css)
// Output: .button { color: blue; }
})
```
```js
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [
require('postcss-remove-duplicate-values')
]
}
}
]
}
]
}
}
```
```css
/* Input CSS */
.container {
color: red;
color: blue;
}
.button {
margin: 10px;
margin: 20px;
}
/* With selector: '.container' */
.container {
color: blue;
}
.button {
margin: 10px;
margin: 20px; /* Not processed */
}
```
```css
/* Input CSS */
.empty-rule {
}
.button {
color: blue;
}
/* With preserveEmpty: false */
.button {
color: blue;
}
/* .empty-rule removed */
/* With preserveEmpty: true */
.empty-rule {
}
.button {
color: blue;
}
```
**Test the plugin in real-time with our interactive playground:**
[๐ฎ **Try the Playground** โ](https://xettri.github.io/postcss-remove-duplicate-values)
- โจ **Test CSS processing** in real-time
- ๐ฏ **Experiment with options** (selector filtering, empty rule preservation)
- ๐ **Try pre-built examples** for common scenarios
- ๐ **See live statistics** of duplicate removal results
- ๐จ **Understand plugin behavior** through interactive examples
<br>
**Made with โค๏ธ by [Bharat Rawat](https://bharatrawat.com)**
[ ]: https://github.com/xettri/postcss-remove-duplicate-values
[ ]: https://www.npmjs.com/package/postcss-remove-duplicate-values
[ ]: https://github.com/xettri/postcss-remove-duplicate-values
[ ]: https://github.com/postcss/postcss
<p>
<a href=