The web content discusses the use of Purgecss to remove unused CSS, potentially reducing CSS file sizes by more than 60% during development workflows, and is compatible with various build tools and JavaScript frameworks.
Abstract
The provided web content introduces the tool Purgecss as a solution for developers who utilize numerous CSS frameworks and libraries, which often result in bloated CSS files with a significant amount of unused styles. The article explains that Purgecss can be integrated into the development workflow to create leaner CSS files by analyzing the styles in use and eliminating the unnecessary ones. This integration is possible with popular build tools such as CLI, JavaScript API, Webpack, Gulp, and Rollup, as well as with modern JavaScript frameworks like React, Vue, Next, and Nuxt. The author also shares personal experience and provides examples of how to set up Purgecss with Gulp and Nuxt.js, emphasizing the ease of implementation and the substantial reduction in CSS file size that can be achieved, potentially exceeding 60%.
Opinions
The author expresses the importance of optimizing CSS, noting that developers often use a lot of CSS frameworks and libraries, which can lead to using less than 20% of the imported styles.
Purgecss is positively endorsed for its ability to streamline the CSS cleaning process and integrate seamlessly into various development workflows.
The author encourages readers to try Purgecss, suggesting that they will likely see a significant reduction in their CSS file sizes.
There is an expression of initial concern about Purgecss's compatibility with modern JavaScript frameworks like Nuxt.js, which was later resolved upon discovering its support for such frameworks in the documentation.
The author provides a subjective recommendation to read an article on setting up a CSS build process with Gulp, implying its usefulness for those interested in optimizing their CSS.
A hint of excitement is conveyed regarding the optimization possibilities that Purgecss offers for Nuxt.js projects.
The author invites feedback from readers who implement Purgecss, showing an interest in the community's results and experiences.
There is a suggestion to explore CSS methodologies for writing scalable and maintainable CSS code, indicating a belief in the value of combining methodologies for better code practices.
How to Remove Unused CSS
Reduce your CSS files more than 60% during your development workflow with Purgecss.
I have been searching for a while a tool that will allow me to clean my CSS from unused style.
Why was I looking for this kind of tool? Because nowadays almost every developer uses a lot of CSS frameworks and libraries to develop interfaces faster and easier so it is inevitably that the size of css files increase a lot.
But how much of the CSS style that you import from external libraries you really use? Sometimes even less than 20%, so why do you need to have all this unused style? No reason!
Purgecss
Here Purgecss come to help, it is a tool which helps to remove unused CSS and it can be used as part of your development workflow.
How Purgecss is working under the scene? It needs to know:
the file css that you want to clean.
the files where it need to inspect
the destination path of the new file generated
So from the initial css file[1] it will search in your html files(for example)[2] and from there it will create a new css file[3] only with the style that you really used.
I’m sure that if you try it you will be able to reduce more than 60% the size of your CSS.
The build tools
The cool thing of Purgecssis that you can implement it in your development workflow with a lot of popular build tools:
CLI
JavaScript API
Webpack
Gulp
Rollup
The JS frameworks
We can use it also with the most modern JavaScript frameworks such as:
React
Vue
Next
Nuxt
How to use Purgecss
I wrote a whole article about how to set up a CSS build process with Gulp, there I used also Purgecss so if you are interested in this build process I suggest you to read this article.
Here I will show you how to implement Purgecss with Gulp and Nuxt.js but you can read the official documentationhere to know how to implement Purgecss in different ways.
Purgecss with Gulp
At first you need to install the package with npm or yarn.
npm i -D gulp-purgecss
or
yarn add gulp-purgecss
after in your gulpfile.js you need to import the package that you just added
ps. if you are running your task in series or sequence remember to run this task after that you generate your html files, if not the task will generate an error.
Purgecss with Nuxt.js
In the beginning when I saw that Purgecss was working with HTML files I got upset because in the last months I’m developing different projects with Nuxt.js and for me it’s very important tooptimize my files. But reading the documentation I found out that you can implement Purgecss with the most modern JS frameworks such React, Vue, Next and Nuxt.
Let’s see how to use Purgecss in your Nuxt projects
At first you need to install the following packages to your project with npm or yarn
npm i--save-dev glob-all purgecss-webpack-plugin
or
yarn addglob-all purgecss-webpack-plugin
Now in your nuxt.config.js you need to import them (outside of the export default)