Detalhes do pacote

css-declaration-sorter

Siilwyn46mISC7.2.0

Sorts CSS declarations fast and automatically in a certain order.

postcss, postcss-plugin, css, declaration

readme (leia-me)

CSS declaration sorter logo

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.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. The format is based on Keep a Changelog and follows Semantic Versioning.

7.2.0 - 2024-03-25

Added

  • New properties related to: offset, transition and text wrapping.

    Fixed

  • Gap properties column-gap and row-gap moving unsafely.

7.1.1 - 2023-10-01

Fixed

  • Regression with TypeScript types resolution for CommonJS.

7.1.0 - 2023-09-23

Added

  • New font and math related properties.

    Fixed

  • Keep unknown properties in the same position to prevent overrides.

7.0.3 - 2023-07-27

Fixed

  • TypeScript types resolution for Node.js 16 and up, thanks to @amiller-gh and @privatenumber!

7.0.2 - 2023-07-08

Fixed

  • Missing bubble sort code in npm package.

7.0.0 - 2023-05-29

Fixed

  • Border properties border-end and border-start radius moving unsafely.

    Removed

  • Node.js 12 support.

6.4.1 - 2023-07-08

Fixed

  • Backport: Missing bubble sort code in npm package.

6.4.0 - 2023-03-23

Added

  • New container, overflow and font related properties.

    Fixed

  • Order of shorthand properties between overrides that are already changing position with keepOverrides.

6.3.1 - 2022-09-02

Fixed

  • Logical border properties moving with keepOverrides.

6.3.0 - 2022-06-09

Added

  • New properties: content-visibility and image-orientation.

    Fixed

  • Logical properties like padding-block moved unsafely with keepOverrides.

6.2.2 - 2022-03-27

Fixed

  • Types export for CommonJS.
  • Expected plugin type, now using PostCSS PluginCreator type.

6.2.1 - 2022-03-26

Fixed

  • Include types in npm package files.

6.2.0 - 2022-03-26

Added

  • TypesScript types, special thanks to @peterblazejewicz.
  • Named exports.
  • New properties regarding font hyphens, print color and scrollbar gutter.

    Fixed

  • Dynamic imports in CommonJS output.

    Changed

  • No more dependencies, smaller ESM package.

6.1.4 - 2022-01-08

Fixed

  • Crash on missing paired comment node from invalid SCSS parsing.

6.1.3 - 2021-09-06

Fixed

  • Crash on missing node raws before content.

6.1.2 - 2021-09-06

Fixed

  • Crash on two comments in the same line.

6.1.1 - 2021-07-24

Fixed

  • Usage of built-in orders with CommonJS.

6.1.0 - 2021-07-24

Changed

  • Internal code to load built-in orders using dynamic import instead of using Node.js fs. Enables usage in other environments such as the browser.

    Added

  • New properties regarding mask border, forced color adjustment & font styling overrides.

6.0.3 - 2021-05-11

Fixed

  • Sorting padding and border shorthands with keepOverrides enabled.
  • Property reset with all, the property is moved to the top.

    Changed

  • Add back Node.js 10 support.

6.0.2 - 2020-11-04

Fixed

  • Loading built-in order using a relative path.

6.0.1 - 2020-10-26

Added

  • ES module export type, both CommonJS and ES module importing are supported.
  • Newer font and grid CSS properties like row-gap and font-display.

    Changed

  • Scraping of CSS properties now comes from MDN browser compatibility package.
  • PostCSS upgraded to version eight which changes PostCSS to a peer dependency.
  • Order declarations to be ordered in the precendece of their shorthand counterparts. Special thanks to @DiemenDesign.

    Removed

  • Node.js 10 support.

5.1.2 - 2020-02-21

Fixed

  • Experimental Node.js warning from showing up on some versions.

5.1.1 - 2020-02-07

Fixed

  • Handling vendor prefixed declarations such as -moz-animation when sorting with keepOverrides enabled.

5.1.0 - 2020-02-06

Changed

  • Sorting of unknown properties when sorting alphabetically now works the same as the other orders. Instead of sorting all properties only known properties will be sorted and unknown properties will retain their respective order.

5.0.0 - 2019-12-16

Added

  • Option keepOverrides to keep overrides in place, useful for legacy CSS where shorthand declarations override longhand declarations.

Changed

  • Default sorting order renamed to alphabetical.
  • Custom sorting order as a JSON file replaced in favor of the option to pass a custom sorting function.

Removed

  • Node.js 6 and 8 support.

4.0.1 - 2018-07-30

Fixed

  • Invalid package engines node version range.

4.0.0 - 2018-07-24

Added

  • New flex box shorthand properties which can conflict with existing flex box properties.
  • New ruby, transform and text related properties.

Removed

  • Node.js 4 support.
  • Deprecated grid properties.

3.0.1 - 2018-01-11

Fixed

  • Keep at-rules at the same position.

3.0.0 - 2017-12-14

Added

  • Flexbox justify self properties.

Changed

  • SMACSS order so it is more in line with Stylelint.

Removed

2.1.0 - 2017-08-25

Added

  • New text style and interaction related properties.

2.0.1 - 2017-06-19

Fixed

  • Prevent comments outside CSS selectors from being moved.

2.0.0 - 2017-03-16

Changed

  • Put declarations before nested declarations.