Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React analyze bundle size

Tags:

reactjs

bundle

I have a question - how to analyze bundle size?

I want to get informations how bundle files change in case i will push the commit in gitlab.

I was looking for something like danger.js but it probably doesn't support gitlab.

like image 351
Dawid Kwiatoń Avatar asked Sep 20 '25 05:09

Dawid Kwiatoń


1 Answers

You can use this script to analyze, without ejecting create-react-app

Put analyze.js in root of your project ( where the package.json is located )

npm install progress-bar-webpack-plugin
npm install webpack-bundle-analyzer

analyze.js

process.env.NODE_ENV = 'production';

const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const webpackConfigProd = require('react-scripts/config/webpack.config')('production');

// this one is optional, just for better feedback on build
const chalk = require('chalk');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const green = text => {
  return chalk.green.bold(text);
};

// pushing BundleAnalyzerPlugin to plugins array
webpackConfigProd.plugins.push(new BundleAnalyzerPlugin());

// optional - pushing progress-bar plugin for better feedback;
// it can and will work without progress-bar,
// but during build time you will not see any messages for 10-60 seconds (depends on the size of the project)
// and decide that compilation is kind of hang up on you; progress bar shows nice progression of webpack compilation
webpackConfigProd.plugins.push(
  new ProgressBarPlugin({
    format: `${green('analyzing...')} ${green('[:bar]')}${green('[:percent]')}${green('[:elapsed seconds]')} - :msg`,
  }),
);

// actually running compilation and waiting for plugin to start explorer
webpack(webpackConfigProd, (err, stats) => {
  if (err || stats.hasErrors()) {
    console.error(err);
  }
});

Now simply put node ./analyze.js in the package.json scripts

  "scripts": {
      .....
    "analyze": "node ./analyze.js"
  },

after that run npm run analyze

enter image description here

like image 194
Rutul Patel Avatar answered Sep 23 '25 12:09

Rutul Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!