Skip to content

Instantly share code, notes, and snippets.

@HamidOsouli-zz
Created September 29, 2021 11:10
Show Gist options
  • Save HamidOsouli-zz/5a79294665fbacc9826066596863d214 to your computer and use it in GitHub Desktop.
Save HamidOsouli-zz/5a79294665fbacc9826066596863d214 to your computer and use it in GitHub Desktop.
Webpack bundle analyzer for CRA (create-react-app)
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');
const chalk = require('chalk');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const green = text => {
return chalk.green.bold(text);
};
webpackConfigProd.plugins.push(new BundleAnalyzerPlugin());
webpackConfigProd.plugins.push(
new ProgressBarPlugin({
format: `${green('analyzing...')} ${green('[:bar]')}${green('[:percent]')}${green('[:elapsed seconds]')} - :msg`,
}),
);
webpack(webpackConfigProd, (err, stats) => {
if (err || stats.hasErrors()) {
console.error(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment