PostCSS plugin to keep rules and at-rules content in order.
Installations
npm install postcss-sorting
Developer
hudochenkov
Developer Guide
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
No
Node Version
20.12.2
NPM Version
10.9.0
Statistics
518 Stars
320 Commits
31 Forks
8 Watching
2 Branches
13 Contributors
Updated on 22 Nov 2024
Bundle Size
18.01 kB
Minified
4.23 kB
Minified + Gzipped
Languages
JavaScript (71.2%)
CSS (28.18%)
HTML (0.61%)
Total Downloads
Cumulative downloads
Total Downloads
309,441,201
Last day
-0.5%
336,647
Compared to previous day
Last week
2.8%
1,694,703
Compared to previous week
Last month
8.8%
7,086,627
Compared to previous month
Last year
1.6%
77,529,071
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
PostCSS Sorting
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
1npm 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 inproperties-order
.throw-validate-errors
: Throw config validation errors instead of showing and ignoring them. Defaults tofalse
.
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.
1a { 2 top: 5px; /* shared-line comment belongs to `top` */ 3 /* comment belongs to `bottom` */ 4 /* comment belongs to `bottom` */ 5 bottom: 15px; /* shared-line comment belongs to `bottom` */ 6}
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
Plugin will ignore rules, which have template literal interpolation, to avoid breaking the logic:
1const Component = styled.div` 2 /* The following properties WILL NOT be sorted, because interpolation is on properties level */ 3 z-index: 1; 4 top: 1px; 5 ${props => props.great && 'color: red'}; 6 position: absolute; 7 display: block; 8 9 div { 10 /* The following properties WILL be sorted, because interpolation for property value only */ 11 z-index: 2; 12 position: static; 13 top: ${2 + 10}px; 14 display: inline-block; 15 } 16`;
Usage
See PostCSS docs for more examples.
Command Line
Add postcss-cli and PostCSS Sorting to your project:
1npm install postcss postcss-cli postcss-sorting --save-dev
Create a postcss.config.js
with PostCSS Sorting configuration:
1module.exports = { 2 plugins: { 3 'postcss-sorting': { 4 order: [ 5 'custom-properties', 6 'dollar-variables', 7 'declarations', 8 'at-rules', 9 'rules', 10 ], 11 12 'properties-order': 'alphabetical', 13 14 'unspecified-properties-position': 'bottom', 15 }, 16 }, 17};
Or, add the 'postcss-sorting'
section to your existing postcss-cli
configuration file.
Next execute:
1npx 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:
1npm install postcss gulp-postcss postcss-sorting --save-dev
Enable PostCSS Sorting within your Gulpfile:
1let gulp = require('gulp'); 2let postcss = require('gulp-postcss'); 3let sorting = require('postcss-sorting'); 4 5exports['sort-css'] = () => { 6 return gulp 7 .src('./css/src/*.css') 8 .pipe( 9 postcss([ 10 sorting({ 11 /* options */ 12 }), 13 ]) 14 ) 15 .pipe(gulp.dest('./css/src')); 16};
Text editor
This plugin available as Sublime Text, Atom, VS Code, and Emacs plugin. Though, seems all these plugins are not maintained.
Related tools
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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
9 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 7
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/hudochenkov/postcss-sorting/lint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/hudochenkov/postcss-sorting/lint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/hudochenkov/postcss-sorting/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/hudochenkov/postcss-sorting/test.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 2 out of 2 npmCommand dependencies pinned
Reason
Found 4/28 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/lint.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Score
4.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to postcss-sorting
@types/postcss-sorting
TypeScript definitions for postcss-sorting
css-declaration-sorter
Sorts CSS declarations fast and automatically in a certain order.
postcss-sort-media-queries
PostCSS plugin for sorting and combining CSS media queries with mobile first / **desktop first methodologies
sugarss
Indent-based CSS syntax for PostCSS