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.

126 lines
4.4 KiB

3 months ago
  1. <img alt='CSS declaration sorter logo' src='https://raw.githubusercontent.com/Siilwyn/css-declaration-sorter/master/logo.svg?sanitize=true' height='260' align='right'>
  2. # CSS Declaration Sorter
  3. [![npm][npm-badge]][npm]
  4. 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.
  5. Check out [the Prettier plugin](https://github.com/Siilwyn/prettier-plugin-css-order) for usage with a variety of file formats.
  6. ## Niceness
  7. - Up-to-date CSS properties fetched from the [MDN Compatibility Data](https://github.com/mdn/browser-compat-data/) project.
  8. - Choose your wanted order or provide your own.
  9. - Nested rules sorting support.
  10. - SCSS and Less support when combined with either [postcss-scss](https://github.com/postcss/postcss-scss) or [postcss-less](https://github.com/webschik/postcss-less).
  11. - Thought-out sorting orders out of the box, **approved by their authors**.
  12. ## Alphabetical example
  13. Input:
  14. ```css
  15. body {
  16. display: block;
  17. animation: none;
  18. color: #C55;
  19. border: 0;
  20. }
  21. ```
  22. Output:
  23. ```css
  24. body {
  25. animation: none;
  26. border: 0;
  27. color: #C55;
  28. display: block;
  29. }
  30. ```
  31. ## Built-in sorting orders
  32. - Alphabetical
  33. `alphabetical`
  34. *Default, order in a simple alphabetical manner from a - z.*
  35. - [SMACSS](http://smacss.com/book/formatting#grouping)
  36. `smacss`
  37. *Order from most important, flow affecting properties, to least important properties.*
  38. 1. Box
  39. 2. Border
  40. 3. Background
  41. 4. Text
  42. 5. Other
  43. - [Concentric CSS](https://github.com/brandon-rhodes/Concentric-CSS)
  44. `concentric-css`
  45. *Order properties applying outside the box model, moving inward to intrinsic changes.*
  46. 1. Positioning
  47. 2. Visibility
  48. 3. Box model
  49. 4. Dimensions
  50. 5. Text
  51. ## Usage
  52. Following the PostCSS plugin guidelines, this package depends on PostCSS as a peer dependency:
  53. `npm install postcss css-declaration-sorter --save-dev`
  54. ### CLI
  55. 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, the `postcss-cli` package is a required dependency.
  56. Piping out result from file:
  57. `postcss input.css --use css-declaration-sorter | cat`
  58. Sorting multiple files by overwriting:
  59. `postcss *.css --use css-declaration-sorter --replace --no-map`
  60. Sorting all files in a directory with SCSS syntax using [postcss-scss](https://github.com/postcss/postcss-scss) by overwriting:
  61. `postcss ./src/**/*.scss --syntax postcss-scss --use css-declaration-sorter --replace --no-map`
  62. Sorting all files in the directory with SCSS syntax and SMACSS order by overwriting, using `package.json` configuration:
  63. ```json
  64. "postcss": {
  65. "syntax": "postcss-scss",
  66. "map": false,
  67. "plugins": {
  68. "css-declaration-sorter": { "order": "smacss" }
  69. }
  70. }
  71. ```
  72. `postcss ./src/**/*.scss --replace --config package.json`
  73. ### Vanilla JS
  74. ```js
  75. import postcss from 'postcss';
  76. import { cssDeclarationSorter } from 'css-declaration-sorter';
  77. postcss([cssDeclarationSorter({ order: 'smacss' })])
  78. .process('a { color: hyperblue; display: block; }', { from: undefined })
  79. .then(result => console.log(
  80. result.css === 'a { display: block; color: hyperblue; }'
  81. ));
  82. ```
  83. ___
  84. **[View more usage examples](/examples) in combination with other tools.**
  85. ___
  86. ## API
  87. ### cssDeclarationSorter({ order, keepOverrides })
  88. #### order
  89. Type: `string` or `function`
  90. Default: `alphabetical`
  91. Options: `alphabetical`, `smacss`, `concentric-css`
  92. Provide the name of one of the built-in sort orders or a comparison function that is passed to ([`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)). This function receives two declaration names and is expected to return `-1`, `0` or `1` depending on the wanted order.
  93. #### keepOverrides
  94. Type: `Boolean`
  95. Default: `false`
  96. 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`.
  97. [PostCSS]: https://github.com/postcss/postcss
  98. [npm]: https://www.npmjs.com/package/css-declaration-sorter
  99. [npm-badge]: https://tinyshields.dev/npm/css-declaration-sorter.svg