1. postcss-combine-duplicated-selectors
automatically keep css selectors unique
postcss-combine-duplicated-selectors
Package: postcss-combine-duplicated-selectors
Created by: ChristianMurphy
Last modified: Fri, 24 Jun 2022 09:41:22 GMT
Version: 10.0.3
License: MIT
Downloads: 363,499
Repository: https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors

Install

npm install postcss-combine-duplicated-selectors
yarn add postcss-combine-duplicated-selectors

Postcss combine duplicated selectors

npm
build status
dependency status
devDependency status

Automatically detects and combines duplicated css selectors so you don't have to
:smile:

Usage

Requirements

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

Using PostCSS JS API

 '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);
    });

Using PostCSS CLI

 postcss style.css --use postcss-combine-duplicated-selectors --output newcss.css

Example

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

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);
}

Media Queries

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.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code