By using this script, you can easily remove duplicated CSS properties and prettify the resulting CSS files, making your stylesheets more organized and optimized.
This Node.js script helps parse multiple CSS files, remove duplicated CSS properties, and save the processed files with the "new-" prefix. The script utilizes the css package for parsing and manipulating CSS, as well as the prettier package for prettifying the resulting CSS output.
Accepts an array of CSS file names as input. Parses each CSS file using the css package, building an Abstract Syntax Tree (AST) representation. Removes duplicate CSS properties by comparing rules across all previous files. Utilizes the prettier package to format the processed CSS output. Saves the processed CSS files with the "new-" prefix in the same location as the source files.
Ensure you have Node.js installed on your machine. Install the required packages by running the command npm install.
- Place the provided script file (duplicated-css.js) in your project directory.
- Open the script file and modify the cssFiles array with the names of the CSS files you want to process.
- Run the script using the command node duplicated-css.js.
- The script will process the CSS files, remove duplicate properties, prettify the resulting CSS, and save the processed files with the "new-" prefix in the same location as the source files.
Suppose you have three CSS files in your project directory: file1.css, file2.css, and file3.css. The contents of these files are as follows:
file1.css:
body {
line-height: 1;
font-family: Arial, Helvetica, sans-serif;
}
file2.css:
body {
font-family: Arial, Helvetica, sans-serif;
}
file3.css:
body {
line-height: 1;
font-family: Arial, Helvetica, sans-serif;
}
After running the script using node duplicated-css.js, the processed files will be saved as follows:
new-file1.css:
body {
line-height: 1;
font-family: Arial, Helvetica, sans-serif;
}
new-file2.css:
body {
}
new-file3.css:
body {
}
The duplicate CSS properties are removed from file2.css and file3.css, resulting in empty rule blocks.
Feel free to customize the script based on your specific requirements or extend its functionality to suit your needs.