Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
postcss-combine-duplicated-selectors
Advanced tools
Readme
Automatically detects and combines duplicated css selectors so you don't have to :smile:
In order to use this you will need to have postcss installed. Depending on whether or not you want to use the CLI you need to install postcss-cli.
npm install --save-dev postcss postcss-combine-duplicated-selectors
# or
yarn add --dev postcss postcss-combine-duplicated-selectors
'use strict';
const fs = require('fs');
const postcss = require('postcss');
const css = fs.readFileSync('src/app.css');
postcss([require('postcss-combine-duplicated-selectors')])
.process(css, {from: 'src/app.css', to: 'app.css'})
.then((result) => {
fs.writeFileSync('app.css', result.css);
if (result.map) fs.writeFileSync('app.css.map', result.map);
});
postcss style.css --use postcss-combine-duplicated-selectors --output newcss.css
Input
.module {
color: green;
}
.another-module {
color: blue;
}
.module {
background: red;
}
.another-module {
background: yellow;
}
Output
.module {
color: green;
background: red;
}
.another-module {
color: blue;
background: yellow;
}
Duplicated properties can optionally be combined.
Set the removeDuplicatedProperties
option to true
to enable.
const postcss = require('postcss');
const combineSelectors = require('postcss-combine-duplicated-selectors');
postcss([combineSelectors({removeDuplicatedProperties: true})]);
When enabled the following css
.a {
height: 10px;
background: orange;
background: rgba(255, 165, 0, 0.5);
}
will combine into
.a {
height: 10px;
background: rgba(255, 165, 0, 0.5);
}
In order to limit this to only combining properties when the values are equal, set the removeDuplicatedValues
option to true
instead. This could clean up duplicated properties, but allow for conscious duplicates such as fallbacks for custom properties.
const postcss = require('postcss');
const combineSelectors = require('postcss-combine-duplicated-selectors');
postcss([combineSelectors({removeDuplicatedValues: true})]);
This will transform the following css
.a {
height: 10px;
}
.a {
width: 20px;
background: var(--custom-color);
background: rgba(255, 165, 0, 0.5);
}
into
.a {
height: 10px;
width: 20px;
background: var(--custom-color);
background: rgba(255, 165, 0, 0.5);
}
If you have code with media queries, pass code through postcss-combine-media-query or css-mquery-packer before postcss-combine-duplicated-selectors to ensure optimal results.
FAQs
automatically keep css selectors unique
The npm package postcss-combine-duplicated-selectors receives a total of 66,939 weekly downloads. As such, postcss-combine-duplicated-selectors popularity was classified as popular.
We found that postcss-combine-duplicated-selectors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.