Excessive CSS Bundle Size

Is there a better way to reduce the css bundle size when using component libraries?

I love the idea behind RadixUI Themes.

However, it does add a lot of unused css to the bundle. I am trying to figure out the best way to axe as much unused css from the bundle as possible but I have not been able to find an elegant way to do it so far.

The only way I could reduce some of it was by cloning @radix-ui/themes package into my monorepo and manually removing the unused css imports from it's css export.

Doing this cut down the overall css bundle by half. And Pagespeed also stopped complaining about unused css.

I know, I know. I am not trying to justify my approach with the results. It is bad in so many ways. Adds a bunch of maintenance debt which will eventually be the reason I stop using the dependency in my next annual website design refresh. But, I don't like to obsess too much on just reaching the end goal.

The process of how you reach there is just as important.

So, I am still exploring ways to get this done more elegantly without having to clone a bunch of code within my monorepo. If you have any ideas, do reach out to discuss. Any help is appreciated.


Update — three days later.

  • Removed the cloned package.
  • Applied my changes to the dependency using pnpm patch.

Since the changes I needed in the css were inside src directory, I couldn't directly patch target css files. The exported css in dependency is compiled on build step. So, I had to build the package with my changes applied, and copy the output css from dist directory and apply the patch using pnpm patch.

Still not ideal, but something that works and is better than not having it.

© 2024 Shubham Gulati. All rights reserved.