反应分析包大小

Daw*_*toń 2 bundle reactjs

我有一个问题 - 如何分析包大小?

我想获取有关捆绑文件如何更改的信息,以防我将在 gitlab 中推送提交。

我正在寻找类似dangerous.js 的东西,但它可能不支持gitlab。

Rut*_*tel 9

可以使用这个脚本进行分析,无需弹出create-react-app

放入analyze.js项目的根目录(所在package.json位置)

npm install progress-bar-webpack-plugin
npm install webpack-bundle-analyzer
Run Code Online (Sandbox Code Playgroud)

分析.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);
  }
});
Run Code Online (Sandbox Code Playgroud)

现在简单地把node ./analyze.jspackage.json脚本

  "scripts": {
      .....
    "analyze": "node ./analyze.js"
  },
Run Code Online (Sandbox Code Playgroud)

在那次跑步之后 npm run analyze

在此处输入图片说明