1

so my question is I have a react application in this application I used material UI for only two components and I installed npm i @materialui/core so basically, I installed all material UI library but I only used 2 components of that big library will this cause my application to have bigger bundle size or when you run to build it will only bundle the codes that needed and ignore the rest of the unused material UI components? this is not particularly for material UI for most of the libraries.

2
  • 1
    It won't affect the bundle size Commented May 23, 2021 at 23:07
  • Look up tree-shaking, and in the Material-UI docs it explains how to set it up to only bundle the used modules. material-ui.com/guides/minimizing-bundle-size Commented May 23, 2021 at 23:25

1 Answer 1

1

According to the Material UI documentation, you can reduce bundle size by importing your components in the following way: let's say you want the button component, so you import it like this import Button from '@material-ui/core/Button', instead of this import { Button } from '@material-ui/core'. With the former import you'll be importing the Button module only and leaving the rest of the modules alone. For further detail, visit this link: https://v3.material-ui.com/guides/minimizing-bundle-size/. Hope you find it useful!

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for that explanation so that means even if we had installed so many packages but we did not import them in our code it won't affect the final build bundle size?
Well, in this specific case, MaterialUI is helping us reduce bundle size by having separated the whole package into modules. Thus, when you import them the way I showed you, you only import the ones you use. However, if you import only the necessary components you need by destructuring them from the library, like in the latter import in my answer, you will still import all those unused packages! There is a way to eliminate dead code using the tree shaking technique, this article might interest you: developers.google.com/web/fundamentals/performance/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.