You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.9 KiB

4 years ago
  1. <img alt='CSS declaration sorter logo' src='https://cdn.rawgit.com/Siilwyn/css-declaration-sorter/master/logo.svg' height='260' align='right'>
  2. # CSS Declaration Sorter
  3. [![Travis Build Status][travis-icon]][travis]
  4. [![npm version][npm-icon]][npm]
  5. [![David Dependencies Status][david-icon]][david]
  6. [![David devDependencies Status][david-dev-icon]][david-dev]
  7. A Node.js module and [PostCSS] plugin to sort CSS declarations based on their property names. Ensuring the CSS is organized, more consistent and in order... Besides, sorted CSS is smaller when gzipped because there will be more similar strings. The intention of this module is to sort the source CSS code of a project in the build process. Check out [the Atom package](https://github.com/Siilwyn/css-declaration-sorter-atom) for individual usage.
  8. ## Alphabetical example
  9. Input:
  10. ```css
  11. body {
  12. display: block;
  13. animation: none;
  14. color: #C55;
  15. border: 0;
  16. }
  17. ```
  18. Output:
  19. ```css
  20. body {
  21. animation: none;
  22. border: 0;
  23. color: #C55;
  24. display: block;
  25. }
  26. ```
  27. ## Niceness
  28. - Up-to-date CSS properties fetched from the [MDN Web Platform](https://developer.mozilla.org/).
  29. - Sort using your own defined order.
  30. - Nested rules sorting support.
  31. - Less and SCSS support when combined with either [postcss-scss](https://github.com/postcss/postcss-scss) or [postcss-less](https://github.com/webschik/postcss-less).
  32. - Thought-out sorting orders out of the box, **approved by their authors**.
  33. ## Sorting orders
  34. - Alphabetically
  35. `alphabetically`
  36. *Default, ordering in a simple alphabetical manner from a - z.*
  37. - [SMACSS](https://smacss.com/book/formatting#grouping)
  38. `smacss`
  39. *Ordering from most important, flow affecting properties, to least important properties.*
  40. - Box
  41. - Border
  42. - Background
  43. - Text
  44. - Other
  45. - [Concentric CSS](https://github.com/brandon-rhodes/Concentric-CSS)
  46. `concentric-css`
  47. *Starts outside the box model, moves inward.*
  48. - Positioning
  49. - Visibility
  50. - Box model
  51. - Dimensions
  52. - Text
  53. - Custom order
  54. *Provide your own order by passing the location of a JSON file containing an array.*
  55. ## Usage
  56. `npm install css-declaration-sorter --save-dev`
  57. ### CLI
  58. This module does not include its own CLI but works with the official [PostCSS CLI](https://github.com/postcss/postcss-cli). To use the examples below, install `postcss-cli` or prefix with `npx`.
  59. Piping out result from file:
  60. `postcss input.css --use css-declaration-sorter | cat`
  61. Sorting multiple files by overwriting:
  62. `postcss *.css --use css-declaration-sorter --replace --no-map`
  63. ### Vanilla JS
  64. ```js
  65. const fs = require('fs');
  66. const postcss = require('postcss');
  67. const cssDeclarationSorter = require('css-declaration-sorter');
  68. postcss([cssDeclarationSorter({order: 'smacss'})])
  69. .process(fs.readFileSync('some.css'))
  70. .then(function (result) {
  71. fs.writeFileSync('some.css', result.css);
  72. });
  73. ```
  74. ### Gulp
  75. ```js
  76. const gulp = require('gulp');
  77. const gulpPostcss = require('gulp-postcss');
  78. const cssDeclarationSorter = require('css-declaration-sorter');
  79. gulp.task('css', function () {
  80. return gulp.src('some.css')
  81. .pipe(gulpPostcss([cssDeclarationSorter({order: 'smacss'})]))
  82. .pipe(gulp.dest('./'));
  83. });
  84. ```
  85. See [PostCSS] documentation for more examples and other environments.
  86. [PostCSS]: https://github.com/postcss/postcss
  87. [travis]: https://travis-ci.org/Siilwyn/css-declaration-sorter
  88. [travis-icon]: https://img.shields.io/travis/Siilwyn/css-declaration-sorter/master.svg?style=flat-square
  89. [npm]: https://npmjs.com/css-declaration-sorter
  90. [npm-icon]: https://img.shields.io/npm/v/css-declaration-sorter.svg?style=flat-square
  91. [david]: https://david-dm.org/Siilwyn/css-declaration-sorter
  92. [david-icon]: https://img.shields.io/david/Siilwyn/css-declaration-sorter.svg?style=flat-square
  93. [david-dev]: https://david-dm.org/Siilwyn/css-declaration-sorter?type=dev
  94. [david-dev-icon]: https://img.shields.io/david/dev/Siilwyn/css-declaration-sorter.svg?style=flat-square