原因
正常情况下构建的工程,一般都会自带这些信息参数,但是遇到一个老项目,并没时间参数,在做打包优化的过程中,都看不到打包时间,所以需要自己补上
代码
打包代码的核心代码中有个stats参数,里边就有startTime,endTime 属性,我们简单加工一下
新增的打包时间代码
// 打包时间
const times = stats.endTime - stats.startTime;
const minute = Math.floor(times / 1000 / 60);
const second = Math.floor(times / 1000 - minute * 60);
console.log('the build time:\n' + minute + ' minute , ' + second + ' second \n');
build.js代码
// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
measureFileSizesBeforeBuild(paths.appBuild)
.then(previousFileSizes => {
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
fs.emptyDirSync(paths.appBuild);
// Merge with the public folder
copyPublicFolder();
// Start the webpack build
return build(previousFileSizes);
})
.then(
({ stats, previousFileSizes, warnings }) => {
if (warnings.length) {
console.log(chalk.yellow('Compiled with warnings.\n'));
console.log(warnings.join('\n\n'));
console.log(
'\nSearch for the ' +
chalk.underline(chalk.yellow('keywords')) +
' to learn more about each warning.'
);
console.log(
'To ignore, add ' + chalk.cyan('// eslint-disable-next-line') + ' to the line before.\n'
);
} else {
console.log(chalk.green('Compiled successfully.\n'));
}
// 打包时间
const times = stats.endTime - stats.startTime;
const minute = Math.floor(times / 1000 / 60);
const second = Math.floor(times / 1000 - minute * 60);
console.log('the build time:\n' + minute + ' minute , ' + second + ' second \n');
// 打包体积
console.log('File sizes after gzip:\n');
printFileSizesAfterBuild(
stats,
previousFileSizes,
paths.appBuild,
WARN_AFTER_BUNDLE_GZIP_SIZE,
WARN_AFTER_CHUNK_GZIP_SIZE
);
const appPackage = require(paths.appPackageJson);
const publicUrl = paths.publicUrl;
const publicPath = config.output.publicPath;
const buildFolder = path.relative(process.cwd(), paths.appBuild);
printHostingInstructions(appPackage, publicUrl, publicPath, buildFolder, useYarn);
},
err => {
console.log(chalk.red('Failed to compile.\n'));
printBuildError(err);
process.exit(1);
}
);