-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Material UI Icons are not being tree shook #10857
Comments
@dantman I believe that we have already done our best to have bundler tree shaking Material-UI. We have been documentation the alternatives: https://material-ui.com/guides/minimizing-bundle-size.
Follow #10212. |
Sorry, you're right. I did not see the es/ folder available in I tried telling WebPack to alias everything to the |
@dantman Thanks for the feedback. This confirms what I have experienced with webpack so far. Tree shaking has never worked. I have never seen anyone taking advantage of it. We have put |
For posterity I just ran the following tests on my project, by changing the Tests on
Tests on
At first I thought tree-shaking was not working because bundle size are slightly different when using the different import styles, but that difference is negligible (789 -> 790 for |
I want to confirm that tree shaking works............in production only! TL;DR
Background
Cause
Here is the result of aliasing multi repo dependencies to just the local app node_modules, you can see the sheer size of @material-ui/icons accounting for ~30% of the stat size: SolutionThis applies to dev only, and I repeat - it has no bearing on a production build. Production builds with tree shaking work just fine when importing from an esm index. If you want to optimize dev builds:
e.g. replace I hope my few hours of hair pulling helps someone out! Once again, this has no bearing on production, and only applies to optimizing a dev build. |
@rosskevin Better not use aliases here. They don't support the full range of tree-shaking. If you need tree-shaking for development you should try some combination of config flags from https://webpack.js.org/configuration/mode/#mode-productionhttps://webpack.js.org/configuration/mode/#mode-production. You probably need |
I need source aliases because, well it's all our source code. In this case our app source imports icons, and our platform monorepo (source aliased) import icons which generated the duplicate inclusion. Adding a node_module source alias for icons solved the duplicate inclusion but the package still accounts for almost 30% of the memory footprint. I would generally agree not to have to go this direction but I also don't want to add time to the build/rebuild in development mode by using the production optimization. The memory footprint may also not be a big deal to others, but our app is big enough that it made sense for us to optimize while we were at it. I did use There is no doubt I'm in an edge case, but I hope to save someone some time in the future. |
Expected Behavior
I use WebPack and have Tree Shaking setup, I only use a few icons from
material-ui-icons
, with imports like likeimport {Edit, AddBox} from 'material-ui-icons';
and expect the tree shaking algorithm to strip out all the unused icons from the bundle.Current Behavior
Analyzing the contents of my production bundle I see that the entirety of material-ui-icons is used in my bundle, including piles of icons I definitely do not use.
And I manually verified that icons I do not use are present in the bundle.
Looking at the contents of node_modules/material-ui-icons/, I notice that while an index.es.js exists and is being used, the modules being imported themselves appear to be CommonJS modules.
Given the "Use ES2015 module syntax (i.e. import and export)." limitation of tree shaking, I have a feeling that the reason .
It's a side topic but I just realized that the analysis of the Material-UI import looks strange as well. Material UI has an entire
material-ui/es/
folder and anindex.es.js
that is being imported, but it looks as if all the modules are being imported from the CommonJS modules instead of the modules insidematerial-ui/es/
.Your Environment
The text was updated successfully, but these errors were encountered: