postcss-sorting

PostCSS plugin to keep rules and at-rules content in order.

MIT 36 个版本
安装
npm install postcss-sorting
yarn add postcss-sorting
pnpm add postcss-sorting
bun add postcss-sorting
README

PostCSS Sorting

npm version npm downloads last month

PostCSS plugin to keep rules and at-rules content in order.

Lint and autofix stylesheet order with stylelint-order.

Features

  • Sorts rules and at-rules content.
  • Sorts properties.
  • Sorts at-rules by different options.
  • Groups properties, custom properties, dollar variables, nested rules, nested at-rules.
  • Supports CSS, SCSS (using postcss-scss), CSS-in-JS (with postcss-styled-syntax), HTML (with postcss-html), and most likely any other syntax added by other PostCSS plugins.

Installation

npm install --save-dev postcss postcss-sorting

Options

The plugin has no default options. Everything is disabled by default.

  • order: Specify the order of content within declaration blocks.
  • properties-order: Specify the order of properties within declaration blocks.
  • unspecified-properties-position: Specify position for properties not specified in properties-order.
  • throw-validate-errors: Throw config validation errors instead of showing and ignoring them. Defaults to false.

Caveats

Handling comments

Comments that are before node and on a separate line linked to that node. Shared-line comments are also linked to that node. Shared-line comments are comments which are located after a node and on the same line as a node.

a {
	top: 5px; /* shared-line comment belongs to `top` */
	/* comment belongs to `bottom` */
	/* comment belongs to `bottom` */
	bottom: 15px; /* shared-line comment belongs to `bottom` */
}

Ignored at-rules

Some at-rules, like control and function directives in Sass, are ignored. It means rules won't touch content inside these at-rules, as doing so could change or break functionality.

CSS-in-JS

To avoid breaking the logic if the rule has a template literal interpolation, properties will be sorted only among neighbouring properties before or after the interpolation:

const Component = styled.div`
	/* 'z-index' and 'top' will be sorted as a single group. 'position' and 'display' will be sorted as a second group. Interpolation separates properties into groups. */
	z-index: 1;
	top: 1px;
	${props => props.great && 'color: red'};
	position: absolute;
	display: block;

	div {
		/* The following properties WILL be sorted together, because interpolation is for property value only */
		z-index: 2;
		position: static;
		top: ${2 + 10}px;
		display: inline-block;
	}
`;

Usage

See PostCSS docs for more examples.

Command Line

Add postcss-cli and PostCSS Sorting to your project:

npm install postcss postcss-cli postcss-sorting --save-dev

Create a postcss.config.js with PostCSS Sorting configuration:

module.exports = {
	plugins: {
		'postcss-sorting': {
			order: [
				'custom-properties',
				'dollar-variables',
				'declarations',
				'at-rules',
				'rules',
			],

			'properties-order': 'alphabetical',

			'unspecified-properties-position': 'bottom',
		},
	},
};

Or, add the 'postcss-sorting' section to your existing postcss-cli configuration file.

Next execute:

npx postcss --no-map --replace your-css-file.css

For more information and options, please consult the postcss-cli docs.

Gulp

Add gulp-postcss and PostCSS Sorting to your build tool:

npm install postcss gulp-postcss postcss-sorting --save-dev

Enable PostCSS Sorting within your Gulpfile:

let gulp = require('gulp');
let postcss = require('gulp-postcss');
let sorting = require('postcss-sorting');

exports['sort-css'] = () => {
	return gulp
		.src('./css/src/*.css')
		.pipe(
			postcss([
				sorting({
					/* options */
				}),
			])
		)
		.pipe(gulp.dest('./css/src'));
};

Text editor

This plugin available as Sublime Text, Atom, VS Code, and Emacs plugin. Though, seems all these plugins are not maintained.

stylelint and stylelint-order help lint stylesheets and let you know if stylesheet order is correct. Also, they could autofix stylesheets.

I recommend Prettier for formatting stylesheets.

版本列表
10.0.0 2026-03-09
9.1.0 2024-10-20
9.0.0 2024-10-20
8.0.2 2023-03-07
8.0.1 2023-01-06
8.0.0 2023-01-05
7.0.1 2021-10-16
7.0.0 2021-10-12
6.0.0 2020-09-17
5.0.1 2019-07-11
5.0.0 2019-04-18
4.1.0 2019-03-03
4.0.1 2018-11-11
4.0.0 2018-07-17
3.1.0 2017-10-30
3.0.2 2017-09-24
3.0.1 2017-07-08
3.0.0 2017-06-09
2.1.0 2017-05-25
2.0.1 2017-01-12
2.0.0 2017-01-12
1.7.0 2016-09-29
1.6.1 2016-06-23
1.6.0 2016-06-11
1.5.0 2016-05-15
1.4.1 2016-05-11
1.4.0 2016-05-08
1.3.1 2016-04-29
1.3.0 2016-04-29
1.2.3 2016-04-02
1.2.2 2016-03-06
1.2.1 2016-02-22
1.2.0 2016-02-06
1.1.0 2015-12-17
1.0.1 2015-12-11
1.0.0 2015-12-11