1. 程式人生 > 其它 >Vue webpack 打包時長統計

Vue webpack 打包時長統計

技術標籤:前端Vue效能vue

Vue webpack 打包時長統計

在package.json中發現build實際呼叫的是vue-cli-service

"scripts": {
        "dev": "vue-cli-service serve",
        "build": "vue-cli-service build"
    },

執行的函式是 node_modules/@vue/cli-service/lib/commands/build/index.js 中的build

函式。做如下修改:

//開始時間
let startTime = '';

//在構建函式中記錄開始時間
async function build (args, api, options) {
  startTime = Date.parse(new Date())
  console.log('開始時間戳:'+startTime)
  /*
  * ...
  */
  return new Promise((resolve, reject) => {
    webpack(webpackConfig, (err, stats) => {
		//....
          if (!args.
watch) { console.log('結束時間戳:'+Date.parse(new Date())) console.log('花費時間:'+(Date.parse(new Date())-startTime)/1000+'秒') done(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.`) info(`Check out deployment instructions at ${
chalk.cyan(`https://cli.vuejs.org/guide/deployment.html`)}
\n`
) } else { done(`Build complete. Watching for changes...`) } } } //... resolve() }) }) }

重新執行npm run build結果如下:
在這裡插入圖片描述