Skip to content

Commit 6b1347d

Browse files
committed
Write readme
1 parent 122be7b commit 6b1347d

File tree

1 file changed

+290
-12
lines changed

1 file changed

+290
-12
lines changed

README.md

Lines changed: 290 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,305 @@
11
# PostCSS Sorting [![Build Status][ci-img]][ci]
22

3-
[PostCSS] plugin to sort rules content with specified order.
3+
[PostCSS] plugin to sort rules content with specified order. Heavily inspired by [CSSComb].
44

5-
[PostCSS]: https://github.com/postcss/postcss
6-
[ci-img]: https://travis-ci.org/hudochenkov/postcss-sorting.svg
7-
[ci]: https://travis-ci.org/hudochenkov/postcss-sorting
5+
## Features
6+
7+
* Plugin is sorting content for rules and at-rules.
8+
* Sorting nested rules.
9+
* Sorting at-rules, also by at-rule name and parameter.
10+
* Sorting variables.
11+
* Grouping content.
12+
* Currently support CSS, [PreCSS] and most likely any other syntax added by other PostCSS plugins.
13+
14+
## Installation
15+
16+
```bash
17+
$ npm install postcss-sorting
18+
```
19+
20+
## Usage
21+
22+
See [PostCSS] docs for examples for your environment.
23+
24+
#### Node
25+
26+
```js
27+
require('postcss-sorting').process(YOUR_CSS, { /* options */ });
28+
```
29+
30+
#### PostCSS
31+
32+
Add [PostCSS] to your build tool:
33+
34+
```bash
35+
npm install postcss --save-dev
36+
```
37+
38+
Load [PostCSS Sorting] as a PostCSS plugin:
39+
40+
```js
41+
postcss([
42+
require('postcss-sorting')({ /* options */ })
43+
]);
44+
```
45+
46+
#### Gulp
47+
48+
Add [Gulp PostCSS] to your build tool:
49+
50+
```bash
51+
npm install gulp-postcss --save-dev
52+
```
53+
54+
Enable [PostCSS Sorting] within your Gulpfile:
55+
56+
```js
57+
var postcss = require('gulp-postcss');
58+
59+
gulp.task('css', function () {
60+
return gulp.src('./css/src/*.css').pipe(
61+
postcss([
62+
require('postcss-sorting')({ /* options */ })
63+
])
64+
).pipe(
65+
gulp.dest('./css')
66+
);
67+
});
68+
```
69+
70+
#### Grunt
71+
72+
Add [Grunt PostCSS] to your build tool:
73+
74+
```bash
75+
npm install grunt-postcss --save-dev
76+
```
77+
78+
Enable [PostCSS Sorting] within your Gruntfile:
79+
80+
```js
81+
grunt.loadNpmTasks('grunt-postcss');
82+
83+
grunt.initConfig({
84+
postcss: {
85+
options: {
86+
processors: [
87+
require('postcss-sorting')({ /* options */ })
88+
]
89+
},
90+
dist: {
91+
src: 'css/*.css'
92+
}
93+
}
94+
});
95+
```
96+
97+
## Options
98+
99+
Currently there is only one option.
100+
101+
### `sort-order`
102+
103+
Set sort order. If no order is set, plugin uses default config.
104+
105+
**Note**: Use one of [predefined configs] as an example.
106+
107+
Acceptable values:
108+
109+
* `{Array}` of rules;
110+
* `{Array}` of arrays of rules for groups separation;
111+
* `{String}` with the name of predefined config.
112+
113+
#### Declarations
114+
115+
Example: `{ "sort-order": [ "margin", "padding" ] }`
8116

9117
```css
10-
.foo {
11-
/* Input example */
118+
/* before */
119+
p {
120+
padding: 0;
121+
margin: 0;
122+
}
123+
124+
/* after */
125+
p {
126+
margin: 0;
127+
padding: 0;
12128
}
13129
```
14130

131+
#### Grouping
132+
133+
Using array of arrays for `sort-order` separate content into groups by empty line.
134+
135+
Example: `{ "sort-order": [ [ "margin", "padding" ], [ "border", "background" ] ] }`
136+
15137
```css
16-
.foo {
17-
/* Output example */
138+
/* before */
139+
p {
140+
background: none;
141+
border: 0;
142+
margin: 0;
143+
padding: 0;
144+
}
145+
146+
/* after */
147+
p {
148+
margin: 0;
149+
padding: 0;
150+
151+
border: 0;
152+
background: none;
18153
}
19154
```
20155

21-
## Usage
156+
#### @at-rules
22157

23-
```js
24-
postcss([ require('postcss-sorting') ])
158+
Any @at-rule inside other rule can be sorted. There is some keywords:
159+
160+
* `@atrule` — any at-rule.
161+
* `@atrulename` — any at-rule with specific name. Ex., `@media` or `@mixin`.
162+
* `@atrulename parameter` — any at-rule with specific name and parameter. Ex., `@mixin clearfix`.
163+
164+
Example: `{ "sort-order": ["@atrule", "@mixin", "border", "@some-rule hello", "@mixin clearfix"] }`
165+
166+
```scss
167+
/* before */
168+
.block {
169+
@some-rule hello;
170+
border: none;
171+
@mixin clearfix;
172+
@media (min-width: 100px) {
173+
display: none;
174+
}
175+
@mixin island;
176+
}
177+
178+
/* after */
179+
.block {
180+
@media (min-width: 100px) {
181+
display: none;
182+
}
183+
@mixin island;
184+
border: none;
185+
@some-rule hello;
186+
@mixin clearfix;
187+
}
25188
```
26189

27-
See [PostCSS] docs for examples for your environment.
190+
#### Nested rules
191+
192+
`>child` keyword for nested rules.
193+
194+
Example: `{ "sort-order": [ ["position", "top", "width"], ['>child'] ] }`
195+
196+
```scss
197+
/* before */
198+
.block {
199+
position: absolute;
200+
201+
span {
202+
display: inline-block;
203+
}
204+
205+
width: 50%;
206+
207+
&__element {
208+
display: none;
209+
}
210+
211+
top: 0;
212+
}
213+
214+
/* after */
215+
.block {
216+
position: absolute;
217+
top: 0;
218+
width: 50%;
219+
220+
span {
221+
display: inline-block;
222+
}
223+
&__element {
224+
display: none;
225+
}
226+
}
227+
```
228+
229+
#### Variables
230+
231+
`$variable` keyword is using to sort variables like `$size`.
232+
233+
Example: `{ "sort-order": [ ["$variable"], ["position", "top", "width", "height"] ] }`
234+
235+
```scss
236+
/* before */
237+
.block {
238+
position: absolute;
239+
$width: 10px;
240+
top: 0;
241+
$height: 20px;
242+
height: $height;
243+
width: $width;
244+
}
245+
246+
/* after */
247+
.block {
248+
$width: 10px;
249+
$height: 20px;
250+
251+
position: absolute;
252+
top: 0;
253+
width: $width;
254+
height: $height;
255+
}
256+
```
257+
258+
#### Leftovers
259+
260+
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.
261+
262+
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`.
263+
264+
So, with this value:
265+
266+
``` json
267+
{
268+
"sort-order": [
269+
["$variable"],
270+
["position"],
271+
["...", "border"],
272+
["@mixin"],
273+
["font"]
274+
]
275+
}
276+
```
277+
278+
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`.
279+
280+
#### Predefined configs
281+
282+
[PostCSS Sorting] have [predefined configs]:
283+
284+
* `default`
285+
* `zen`
286+
* `csscomb`
287+
* `yandex`
288+
289+
Example: `{ "sort-order": "zen" }`
290+
291+
## Thanks
292+
293+
This plugin is heavily inspired by [CSSComb]. Some code logic, tests and documentation parts are taken from this tool.
294+
295+
[PostCSS]: https://github.com/postcss/postcss
296+
[ci-img]: https://travis-ci.org/hudochenkov/postcss-sorting.svg
297+
[ci]: https://travis-ci.org/hudochenkov/postcss-sorting
298+
[PostCSS Sorting]: https://github.com/hudochenkov/postcss-sorting
299+
[predefined configs]: https://github.com/hudochenkov/postcss-sorting/tree/master/configs
300+
[predefined config]: https://github.com/hudochenkov/postcss-sorting/tree/master/configs
301+
302+
[CSSComb]: https://github.com/csscomb/csscomb.js
303+
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
304+
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
305+
[PreCSS]: https://github.com/jonathantneal/precss

0 commit comments

Comments
 (0)