PostCSS Sorting
PostCSS plugin to sort rules content with specified order. Heavily inspired by CSSComb.
Also available as Sublime Text plugin and unofficial Atom plugin.
Features
- Plugin is sorting content for rules and at-rules.
- Sorting nested rules.
- Sorting at-rules, also by at-rule name and parameter.
- Sorting variables.
- Grouping content.
- Support CSS, SCSS (if postcss-scss parser is used), PreCSS and most likely any other syntax added by other PostCSS plugins.
Installation
$ npm install postcss-sorting
Options
Currently, there are only two options.
sort-order
Set sort order. If no order is set, the plugin uses default config.
Note: Use one of predefined configs as an example.
Acceptable values:
{Array}
of rules;{Array}
of arrays of rules for groups separation;{String}
with the name of predefined config.
Declarations
Example: { "sort-order": [ "margin", "padding" ] }
p {
padding: 0;
margin: 0;
}
p {
margin: 0;
padding: 0;
}
Prefixed properties
Prefixed properties may not be in sort order. Plugin will look for unprefixed property and if it find one it will use that property order for the prefixed property. It would be better not to write prefixed properties in CSS at all and delegate this job to Autoprefixer.
Example: { "sort-order": [ "position", "-webkit-box-sizing", "box-sizing", "width" ] }
div {
-moz-box-sizing: border-box;
width: 100%;
box-sizing: border-box;
position: absolute;
-webkit-box-sizing: border-box;
}
div {
position: absolute;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
}
Grouping
Using an array of arrays for sort-order
separate content into groups by an empty line.
Example: { "sort-order": [ [ "margin", "padding" ], [ "border", "background" ] ] }
p {
background: none;
border: 0;
margin: 0;
padding: 0;
}
p {
margin: 0;
padding: 0;
border: 0;
background: none;
}
@at-rules
Any @at-rule inside another rule can be sorted. There is some keywords:
@atrule
— any at-rule.@atrulename
— any at-rule with a specific name. Ex., @media
or @mixin
.@atrulename parameter
— any at-rule with specific name and parameter. Ex., @mixin clearfix
.
Example: { "sort-order": ["@atrule", "@mixin", "border", "@some-rule hello", "@mixin clearfix"] }
.block {
@some-rule hello;
border: none;
@mixin clearfix;
@media (min-width: 100px) {
display: none;
}
@mixin island;
}
.block {
@media (min-width: 100px) {
display: none;
}
@mixin island;
border: none;
@some-rule hello;
@mixin clearfix;
}
Nested rules
>child
keyword for nested rules.
Example: { "sort-order": [ ["position", "top", "width"], ['>child'] ] }
.block {
position: absolute;
span {
display: inline-block;
}
width: 50%;
&__element {
display: none;
}
top: 0;
}
.block {
position: absolute;
top: 0;
width: 50%;
span {
display: inline-block;
}
&__element {
display: none;
}
}
Variables
$variable
keyword is using to sort variables like $size
.
Example: { "sort-order": [ ["$variable"], ["position", "top", "width", "height"] ] }
.block {
position: absolute;
$width: 10px;
top: 0;
$height: 20px;
height: $height;
width: $width;
}
.block {
$width: 10px;
$height: 20px;
position: absolute;
top: 0;
width: $width;
height: $height;
}
Leftovers
When there are properties that are not mentioned in the sort-order
option, they are inserted after all the sorted properties in the new group in the same order they were in the source stylesheet.
You can override this by using a “leftovers” token: ...
— just place it either in its own group or near other properties in any other group and CSSComb would place all the properties that were not sorted where the ...
was in sort-order
.
So, with this value:
{
"sort-order": [
["$variable"],
["position"],
["...", "border"],
["@mixin"],
["font"]
]
}
everything would go into five groups: variables, then group with position
, then group containing all the leftovers plus the border
, then group with all mixins and then the font
.
Predefined configs
PostCSS Sorting have predefined configs:
Example: { "sort-order": "zen" }
empty-lines-between-children-rules
Set number of empty lines between nested children rules. By default there is no empty lines between '>child' rules.
Acceptable value: {Number}
of empty lines
Example: { "empty-lines-between-children-rules": 1, "sort-order": [ ["..."], [">child"] ] }
.block {
position: absolute;
span {
display: inline-block;
}
&__element {
display: none;
}
&:hover {
top: 0;
}
}
.block {
position: absolute;
span {
display: inline-block;
}
&__element {
display: none;
}
&:hover {
top: 0;
}
}
Usage
See PostCSS docs for examples for your environment.
Sublime Text
See Sublime Text plugin repository.
Gulp
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Enable PostCSS Sorting within your Gulpfile:
var postcss = require('gulp-postcss');
gulp.task('css', function () {
return gulp.src('./css/src/*.css').pipe(
postcss([
require('postcss-sorting')({ })
])
).pipe(
gulp.dest('./css')
);
});
Grunt
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Enable PostCSS Sorting within your Gruntfile:
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
processors: [
require('postcss-sorting')({ })
]
},
dist: {
src: 'css/*.css'
}
}
});
Thanks
This plugin is heavily inspired by CSSComb. Some code logic, tests, and documentation parts are taken from this tool.