我有一个问题-如何分析包装尺寸?
如果要在gitlab中推送提交,我想获取捆绑文件如何更改的信息。
我一直在寻找类似于anger.js的东西,但它可能不支持gitlab。
答案 0 :(得分:5)
您可以使用此脚本进行分析,而无需弹出 create-react-app
将analyze.js放入项目的根目录(package.json所在的位置)
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);
}
});
现在将node ./analyze.js
简单地放在package.json脚本中
"scripts": {
.....
"analyze": "node ./analyze.js"
},
在运行npm run analyze
答案 1 :(得分:2)
您也可以使用 create-react-app
和 webpack-bundle-analyzer
支持的 webpack stats json 文件来完成此操作。
使用 create-react-app
运行构建时,添加 --stats
标志:
yarn build --stats
这将创建一个 build/bundle-stats.json
文件,其中包含 webpack 统计信息,允许您使用 webpack-bundle-analyzer
CLI 来分析此统计信息。
yarn run webpack-bundle-analyzer build/bundle-stats.json
参考: https://www.npmjs.com/package/webpack-bundle-analyzer#user-content-usage-as-a-cli-utility
当时用 CRA 最新版本测试:v4