08335 / hivui-platform-template
hivui平台项目模板
Newer
Older
hivui-platform-template / build / webpack.prod.conf.js
caibinghong on 15 Mar 2022 共${len}个
// 多进程 编译所有
//parallel-webpack允许您并行运行多个Webpack构建,从而将工作分散到各个处理器上,从而有助于显着加快构建速度
// const os = require('os');
// const cpus = os.cpus().length;
// console.log(cpus, ' Math.floor(cpus/2)||1', Math.floor(cpus / 2) || 1)
const chalk = require('chalk')
let webpack = require('webpack')
let argvs = (process.argv[2] || "").replace(/\s*$/g, "");
let moduleList = argvs != "" ? argvs.split(',') : [];
let len = moduleList.length;
let startTime = new Date().valueOf();
//----------- 【一般编译,按参数,一个接一个的】 ---------------
function DoWebpackBuild(list, i, l) {
  let wpkCfg, step = 1, isAll = false;
  if (i == null && l == null) {
    //------------ 【webpack 一口气编译指定的所有功能】 -------------- 
    isAll = true;
    wpkCfg = list;
  } else {
    wpkCfg = list[i];
    if (wpkCfg == null) {
      console.log(chalk.green(`消耗 ${(new Date().valueOf() - startTime) / 1000} 秒`));
      return;
    }
    console.log(chalk.yellow(`进度 ${i + 1}/${l} ,正在编译${wpkCfg.output.path}\n`));
  }
  webpack(wpkCfg, (err, stats) => {
    //这个回调一定要写,不然打包不出来
    if (err || stats.hasErrors()) {//编译错误不在 err 对象内,而是需要使用 stats.hasErrors() 单独处理
      let info = {};
      try {
        info = stats.toJson();
      } catch (e) {
        info = {
          errors: []
        };
        console.log('stats.toJson()', err, stats)
      }
      // console.error(info.errors);
      for (let i = 0, l = info.errors.length; i < l; i++) {
        // console.log(chalk.red(info.errors[i].message));
        // console.error(info.errors[i].message)
        console.error(info.errors[i])
      }
    }
    // 处理完成
    if (isAll) {
      console.log(chalk.green(`消耗 ${(new Date().valueOf() - startTime) / 1000} 秒`));
    }
    isAll == false && DoWebpackBuild(list, i + step, l);
  });
}

if (len == 0) {
  //-------- 【用parallel-webpack,全部编译情况下】 ------------
  /*
  moduleList = require('./webpack.get-module').moduleList || [];
  console.log(`=========进入全模块编译状态(${moduleList.length})模块...=============`)
  var run = require('parallel-webpack').run;
  run(
    require.resolve('./webpack.get-list.js'), 
    {
      watch: false,
      maxRetries: 1,
      stats: false, // defaults to false
      maxConcurrentWorkers: 1,// use 2 workers  cup个数
      argv: [argvs]
    }, ()=>{
      //多打包时,如果其中一个模块出错,不知道是哪个模块
      console.log('\n--------编译完成------- \n');
    });
  */
  let webpackList = require('./webpack.get-list.js');
  len = webpackList.length;
  console.log(`【编译所有】共${len}个=====================\n`)
  DoWebpackBuild(webpackList);//, 0, len
  //以上两种时间上差不多   20s
} else {
  let webpackList = require('./webpack.get-list.js');
  len = webpackList.length;
  console.log(`【按需编译】共${len}个=====================\n`)
  DoWebpackBuild(webpackList)//, 0, len
}