This project is a Node.js script that sorts CSS files and organizes their rules based on specified criteria. It can also convert font sizes from pixels to rem units and merge duplicate selectors.
- Node.js installed on your machine.
- Clone the repository or download the script files.
- Navigate to the project directory in your terminal.
Before running the script, you need to configure the settings in the config.js file. Here are the key options you can set:
-
inputPath: The target file or directory where your input CSS files are located. -
outputPath: The directory where the sorted CSS files will be saved. -
clearOutputDir: Clear output directory before running the script. -
changeFontPxToRem: Set totrueto convert font sizes from pixels to rem units. -
addPxFontSizeToHtmlTag: If greater than 0, font-size: var px; will be added to the existing HTML tag. If set to 0 or false, no changes will be made. -
logAddPxFontSizeToHtmlTag: Set totrueto log when it added a fontt-size rule to the HTML tag. -
logFileNames: Set totrueto log the names of the files being processed. -
logDirNames: Set totrueto log the names of the directories being processed. -
logMergedSelectors: Set totrueto log when selectors are merged. -
mergeDuplicateSelectors: Set totrueto enable merging of duplicate selectors. -
addMergedComments: Set totrueto add/* merged */between the values of the merged selectors. -
tagsOrder: An array that defines the order of CSS selectors. -
processAtsAndOrder: An array that defines the order of at-rules to process.
The CSS selectors are sorted in the following order to maintain consistency and organization:
tagsOrder: [
// CSS Variables
'root',
// Universal Selector
'*',
// Document Structure
'html', 'body', '#root', 'header', 'nav', 'main', 'section', 'article', 'aside', 'footer',
// Headings
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
// Text Elements
'p', 'div', 'span',
// Media Elements
'img',
// List Elements
'ul', 'ol', 'li',
// Form Elements
'form', 'input', 'select', 'option', 'button', 'label', 'textarea',
// Table Elements
'table', 'tr', 'td', 'th',
// Other Elements
'otherTags', 'class', 'id', '@'
];The following at-rules are sorted alphabetically at the '@' place in the tagsOrder:
- @font-face:
- @viewport:
- @page:
The following special at-rules are processed separately by the project and sorted according to processAtsAndOrder. Each at-rule can contain CSS rule blocks inside:
- @supports:
- @keyframes:
- @media:
These at-rules are processed in the order listed above.
-
Configure the Script:
- Modify the
config.jsfile to set your input and output paths, as well as other options.
- Modify the
-
Run the Script: Execute the script to start sorting the CSS files:
npm start
Cleared output dir: output
Sort dir: / [ 'css' ]
Sort dir: /css [ 'style1.css', 'style2.css' ]
Sort file: test/css/style1.css
Save file: output/css/style1.css
Sort file: test/css/style2.css
Merge duplicate selector: html,body
Merge duplicate selector: body
Merge duplicate selector: h2
Merge duplicate selector: h3
Added "font-size: 16px;" to html Tag
Save file: output/css/style2.css
-------------------------
Sort time: 32.247ms
Dirs: 2
Files: 2
Rules: 40
Merged: 4
-------------------------