Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree shaking create-react-app?

Tags:

I created my React app with create-react-app and now use TypeScript with create-react-app-typescript.

Everything works ok, but the problem is - especially because I use the good but heavy material-ui - the size of the built bundle is quite big - almost reaching 1MB.

How can I use tree shaking tools (like in Webpack 2 or rollup? I don't want to eject already so seems like I don't really have access to the Webpack configuration.

I wish it was possible to tree shake a minified code :)

Thanks!

like image 374
mllm Avatar asked Aug 01 '17 22:08

mllm


People also ask

Does create react app have tree shaking?

If you're using modern tooling, such as webpack, create-react-app and similars, you already have tree shaking set up. Tree shaking or dead code elimination means that unused modules will not be included in the bundle during the build process.

How do you turn on tree shaking in roll up?

In Rollup, there is literally nothing you need to setup to gain the Tree Shaking capability within your codes. The only thing to do is just to make sure that you code in ESM, that's it! During the build, Rollup will eliminate your unused codes automatically.

Does Nextjs tree shake?

Tree shaking is enabled in Next. js through webpack automatically.


1 Answers

If your project was built with create-react-app, the latest version has the fix, just update react-scripts to version 2.0.4 or higher.

npm install react-scripts@latest --save

This new version has excellent tree shaking thanks to the updated dependencies. I've tested with both material-ui and mdi-material-ui.

like image 145
Gary Vernon Grubb Avatar answered Oct 18 '22 13:10

Gary Vernon Grubb