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?
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.
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.
As per the official Create React App documentation (https://create-react-app.dev/docs/analyzing-the-bundle-size/):
Below are the steps:
npm install --save source-map-explorer
Alternatively, you may use yarn:
yarn add source-map-explorer
"scripts": {
+ "analyze": "source-map-explorer 'build/static/js/*.js'",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
npm run build
npm run analyze
Now you should see a screen in your browser opened like this
You don't need to eject. Try this:
➜ simple-react-router git:(master) ✗ npm install webpack-bundle-analyzer --save-dev
➜ 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")
➜ 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.
Steps:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With