gpt4 book ai didi

reactjs - react 分析包大小

转载 作者:行者123 更新时间:2023-12-05 08:16:08 56 4
gpt4 key购买 nike

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

我想了解 bundle 文件如何更改的信息,以防我将提交推送到 gitlab 中。

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

最佳答案

您可以使用此脚本进行分析,而无需弹出 create-react-app

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

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

分析.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);
}
});

现在只需将 node ./analyze.js 放在 package.json 脚本中

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

之后运行 npm run analyze

enter image description here

关于reactjs - react 分析包大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009974/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com