npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-declaration-sorter

v7.2.0

Published

Sorts CSS declarations fast and automatically in a certain order.

Downloads

41,367,955

Readme

CSS Declaration Sorter

npm

A Node.js module and PostCSS plugin to sort CSS, SCSS or Less declarations based on their property names. Ensuring styling is organized, more consistent and in order... The goal of this package is to sort the source code of a project in the build process or to decrease the distributed CSS gzipped size.

Check out the Prettier plugin for usage with a variety of file formats.

Niceness

  • Up-to-date CSS properties fetched from the MDN Compatibility Data project.
  • Choose your wanted order or provide your own.
  • Nested rules sorting support.
  • SCSS and Less support when combined with either postcss-scss or postcss-less.
  • Thought-out sorting orders out of the box, approved by their authors.

Alphabetical example

Input:

body {
    display: block;
    animation: none;
    color: #C55;
    border: 0;
}

Output:

body {
    animation: none;
    border: 0;
    color: #C55;
    display: block;
}

Built-in sorting orders

  • Alphabetical
    alphabetical
    Default, order in a simple alphabetical manner from a - z.

  • SMACSS
    smacss
    Order from most important, flow affecting properties, to least important properties.

    1. Box
    2. Border
    3. Background
    4. Text
    5. Other
  • Concentric CSS
    concentric-css
    Order properties applying outside the box model, moving inward to intrinsic changes.

    1. Positioning
    2. Visibility
    3. Box model
    4. Dimensions
    5. Text

Usage

Following the PostCSS plugin guidelines, this package depends on PostCSS as a peer dependency:
npm install postcss css-declaration-sorter --save-dev

CLI

This module does not include its own CLI but works with the official PostCSS CLI. To use the examples below, the postcss-cli package is a required dependency.

Piping out result from file:
postcss input.css --use css-declaration-sorter | cat

Sorting multiple files by overwriting:
postcss *.css --use css-declaration-sorter --replace --no-map

Sorting all files in a directory with SCSS syntax using postcss-scss by overwriting:
postcss ./src/**/*.scss --syntax postcss-scss --use css-declaration-sorter --replace --no-map

Sorting all files in the directory with SCSS syntax and SMACSS order by overwriting, using package.json configuration:

"postcss": {
  "syntax": "postcss-scss",
  "map": false,
  "plugins": {
    "css-declaration-sorter": { "order": "smacss" }
  }
}

postcss ./src/**/*.scss --replace --config package.json

Vanilla JS

import postcss from 'postcss';
import { cssDeclarationSorter } from 'css-declaration-sorter';

postcss([cssDeclarationSorter({ order: 'smacss' })])
  .process('a { color: hyperblue; display: block; }', { from: undefined })
  .then(result => console.log(
    result.css === 'a { display: block; color: hyperblue; }'
  ));

View more usage examples in combination with other tools.


API

cssDeclarationSorter({ order, keepOverrides })

order

Type: string or function
Default: alphabetical
Options: alphabetical, smacss, concentric-css

Provide the name of one of the built-in sort orders or a comparison function that is passed to (Array.sort). This function receives two declaration names and is expected to return -1, 0 or 1 depending on the wanted order.

keepOverrides

Type: Boolean
Default: false

To prevent breaking legacy CSS where shorthand declarations override longhand declarations (also taking into account vendor prefixes) this option can enabled. For example animation-name: some; animation: greeting; will be kept in this order when keepOverrides is true.