Skip to content
automatically keep css selectors unique.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src feat: Handle selector groups Oct 16, 2018
test test: check nested selector groupings and partial overlap cases Oct 16, 2018
.editorconfig setup eslint configuration May 10, 2016
.eslintignore
.gitattributes initial commit May 9, 2016
.gitignore
.travis.yml
CONTRIBUTING.md docs(contributing): add contributing guide Nov 28, 2017
LICENSE add mit license May 10, 2016
README.md
appveyor.yml
package-lock.json chore: update dependency eslint to v5.15.3 Mar 18, 2019
package.json

README.md

Postcss combine duplicated selectors

npm Linux Build Status Windows Build Status Dependency Status devDependency Status

Built with PostCSS Semver Conventional Commits Google code style

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

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.

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

Media Queries

If you have code with media queries, pass code through mq-packer before postcss-combine-duplicated-selectors to ensure optimal results.

You can’t perform that action at this time.