-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better PurgeCSS support #1822
Comments
Yes please |
Surely we might improve navbar and other components but in the meantime a doc page is a good idea |
@jtommy I will try to have something to push sometime during this upcoming week :) |
Thats going to be cool, hoping to see this change soon! 👍 |
I've been experimenting with the whitelisting feature of PurgeCSS and it seems to be working as long you test very carefully. Here's a good summary post on whitelisting in PurgeCSS. Below is an example of some of the Buefy components that required whitelisting for one of my projects. I'm only testing this in development and wouldn't be very comfortable using this on a large project that is already in production.
|
Also looking forward to this. Noticed that my tables & other components weren't loading when I ran 'npm run prod', eventually found out that the purgeCss module kicks in and removes all of the CSS |
@pera Correct me if I'm wrong (still too much to learn in modern web dev) but does the setup posted by you in issue description remove any unused Buefy/Bulma CSS ? Lets say i'm using only a few components and want to purge anything else. It doesn't right ? (all Bulma css is included no matter how many components i'm using) |
@Liwoj If you decide to make a custom build of Bulma/Buefy, you can probably eliminate a good portion of the unused CSS. PurgeCSS does this automatically by analyzing the CSS used in your files to reduce the final size of your production CSS. However, to my knowledge, PurgeCSS is not able to pick up CSS applied dynamically inside Vue components, so you have to manually whitelist some of the CSS rules used inside Buefy components. If you decide to go this route, it's probably easier to do this at the start of your project and whitelist as you develop. I tried running PurgeCSS later on in a project and it was hard to find which classes to whitelist as I had to manually check every page. |
But if you pass all Buefy components into Purgecss plugin as in the description, it will be as you are using all components and remove nothing. Correct ? |
I think PurgeCSS searches within your HTML to look for CSS rules that are actually being used in your front-end. It then removes what it believes to be the unused CSS rules before your final CSS bundle is created. It works extremely well, but struggles with dynamically generated CSS rules within Vue components since the CSS rules don't actually exist in the HTML. You have to whitelist the CSS rules. |
@neovive did you get to a point where you are using PurgeCss in production? |
@wrabit I was close, but opted not to use it since I continued finding classes that needed whitelisting with no way to automate the process. I might try PurgeCSS again when starting a new project, but it was very hard to integrate later on in a large project. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This is worth looking into again. I do not currently have time to dig into this but might in the future. When I try purgeCSS and manually add back some rules I still get a HUGE performance boost. It should be possible to provide better support using something like this, right? https://medium.com/@collinslagat/hack-dealing-with-purgecss-deleting-css-classes-from-vue-component-libraries-element-ui-vuetify-955bbe3f8add |
It's a good approach, I didn't know it. |
|
I actually wrote something to address this a while back and forgot about it, haha. The article that @frellan linked is quite similar to my approach, but the extra spice is that it adds to paths to PurgeCSS based on whatever Webpack has loaded up (i.e., imports/requires). I end up having to manually whitelist some helper, but most of the time, I don't get any issues from the following Webpack plugin.
It's been quite a while since I wrote this code, and I'm trying to jog my memory, but these two files shed a bit of light on how PurgeCSS does its thing:
I'll have to attach a debugger on it and wade through it again, but PurgeCSS doesn't see the paths we want it to (as the article says). There is probably an easier way to do it, and I'll be investigating it this week. One of the big caveats is that when we install/configure buefy with In testing (before gzip, with all components loaded):
My next step is to programmatically add components based on a scan of directories and some more hacky code to massage buefy tags out. As an aside, I was going to call this module buefylo (like buffalo—buefy loader), until I found out that the way I'm pronouncing it was wrong, haha. |
I was able to recover my notes and going through the debugger helped a ton. The basic idea is that we want to use the dependency graph from Webpack in order to inform us which components were loaded. tl;dr: https://github.com/phatj/nuxt-buefy-loader-example is a working example (generated from It seems like this solution works in both server and static mode. It's a trivial example, so most styles are thrown out, but it's 1/10th of the full size. These two resources are immensely helpful:
The snippet I dropped above does not use the correct compilation point ( Some interesting observations:
In terms of the next steps, I think that the AutoloaderPlugin and the loader should be two different packages:
This project could probably benefit from investigating the ESM generator weirdness (it's probably heavily related to I'm excited to slide this in some of my production apps and see what happens. Cheers! |
Just a quick update on this journey: I've fixed some resolution logic in the AutoloaderPlugin which show now work in both nuxt server and static modes. I've tossed it into a production environment, and she's humming along (~100KB lighter than usual). My matching logic was trying to be too fancy; I went old school with dual loops and an early bailout condition. It's much easier to read now. I'll probably sit down and shift these into separate packages/pull requests eventually, but it requires more thought on supporting non-nuxt environments. In the meantime, for anyone needing an immediate fix, you can copy the buefy-loader from the above sample repo. |
@phatj you're amazing. Really thank you for your fantastic work!! Really curious to see the evolution and working on Gridsome. Please keep us informed of your work and as soon as you have a little tutorial, repository, or something else we can share and find more people to test it 😄 . |
@phatj - thank you for putting this together! |
@phatj curious to know the progress in the last months? Have it worked perfectly? Thank you for all the work. |
Description
Following the discussion in #1086 (comment), the Tailwind CSS documentation has a nice page about PurgeCSS, particularly on how class names should be presented in the processed code: https://tailwindcss.com/docs/controlling-file-size/#writing-purgeable-html
Their advice is simply to not dynamically construct class names, since PurgeCSS doesn't perform any sort of code evaluation (it just matches strings that may look like classes in one's code). Buefy has just a few cases like this (e.g. navbar), so to make it work out of the box Buefy should replace these cases to use complete string literals for class names. Another option could be to document the required whitelist; for instance, this is what I am using right now on a vue-cli project:
Why Buefy need this feature
PurgeCSS can greatly reduce the size of production builds, reducing loading times and therefore improving the overall user experience. PurgeCSS is also the only tool of its kind that easily integrates with Vue projects thanks to purgecss-webpack-plugin.
The text was updated successfully, but these errors were encountered: