Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to analyze create react app build size and reduce it?

I have been following this guide: https://create-react-app.dev/docs/analyzing-the-bundle-size/ and just about to run the analyze command to see how big my app is. is this the best way to check bundle/build size in React/JS?

also is there a way to not include certain files from the prod build? like tests? Does CRA handle this automatically?

like image 613
Red Baron Avatar asked Mar 10 '20 06:03

Red Baron


People also ask

How do I reduce the size of my react app build?

Create react app generates source maps in default but if you modify package. json modify your build command like this : “build”: “GENERATE_SOURCEMAP=false react-scripts build”, then the source code would not be visible. It reduced the build size of my application from 24 MB to 7 MB just using this.

How do you analyze bundle size react?

Using webpack-bundle-analyzer From top to bottom - we can see the name of the bundle (which will match what you see in the network tab), and then the node_modules folder. Inside of that you can see that react-dom takes up the majority of the bundle size and then react takes up a much smaller amount on the right.


3 Answers

As per the official Create React App documentation (https://create-react-app.dev/docs/analyzing-the-bundle-size/):

Below are the steps:

Step 1: add Source map explorer

npm install --save source-map-explorer

Alternatively, you may use yarn:

yarn add source-map-explorer

Step 2: Include npm script

"scripts": {
+    "analyze": "source-map-explorer 'build/static/js/*.js'",
     "start": "react-scripts start",
     "build": "react-scripts build",
     "test": "react-scripts test",

Step 3: Run the scripts to build the app

npm run build

Step 4: Run the scripts to analyze the build created in step3

npm run analyze

Now you should see a screen in your browser opened like this enter image description here

like image 112
Varun Sukheja Avatar answered Oct 08 '22 00:10

Varun Sukheja


You don't need to eject. Try this:

  1. install the analyser:enter image description here
➜  simple-react-router git:(master) ✗ npm install webpack-bundle-analyzer --save-dev

  1. create a new file, I called mine: sample.js
➜  simple-react-router git:(master) ✗ cat sample.js 
process.env.NODE_ENV = "production"
var BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin

const webpackConfigProd = require("react-scripts/config/webpack.config.prod")

webpackConfigProd.plugins.push(
  new BundleAnalyzerPlugin({
    analyzerMode: "static",
    reportFilename: "report.html",
  })
)

require("react-scripts/scripts/build")
  1. Run with node
➜  simple-react-router git:(master) ✗ node sample.js                                
Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist`
Creating an optimized production build...
Webpack Bundle Analyzer saved report to /Users/dixitk13/code/simple-react-router/build/report.html
Compiled successfully.

File sizes after gzip:

  54.49 KB  build/static/js/1.0ee1e308.chunk.js
  1.9 KB    build/static/js/main.73bea786.chunk.js
  763 B     build/static/js/runtime~main.229c360f.js
.
.
.

a new browser tab should open for you.

like image 3
dixitk13 Avatar answered Oct 08 '22 01:10

dixitk13


Steps:

  • Install webpack-bundle-analyzer and config that in your webpack config.
  • Try to use the webpack code splitting feature to reduce the file sizes at the first load time.
like image 1
Ali Torki Avatar answered Oct 08 '22 01:10

Ali Torki