I'm currently working on a monorepo that includes a UI package and a web application built using React and Vite. The problem I'm facing is with the tree-shaking feature during the build process. When I import my UI library into my web app, it seems that unused components from my library are not being tree-shaken, while other libraries like lucide-react work fine and only include the icons I use. I build my UI package using unbuild, and I've also tried using Vite, but the problem persists. Does anyone have insights on how to fix this? Here's a link to the repository where I'm documenting the issue: https://github.com/Maqed/treeshake-not-working-bug.
2 Answers
I think your issue might stem from how the exports are defined in your package. Try checking that the components are being exported properly and that you're using named exports if needed. This can affect tree-shaking significantly. Also, ensure that the Vite and Rollup configurations are set up to recognize and process tree-shaking correctly. Sometimes, optimizations can be ignored if the modules aren't set up correctly in the build settings.
I've had a similar issue before. In my case, it was due to how I was importing the components. If you're using default imports from your UI library, it might not support tree-shaking as well as named imports. Try changing your imports to named imports and see if that helps. Also, consider updating your build tools and configurations, as newer versions may have improvements regarding tree-shaking.

Related Questions
How to Build a Custom GPT Journalist That Posts Directly to WordPress
Cloudflare Origin SSL Certificate Setup Guide
How To Effectively Monetize A Site With Ads