'use strict' require('./check-versions')() const fs = require("fs") const chalk = require('chalk') const { merge } = require('webpack-merge'); const packageConfig = require('../package.json') process.env.NODE_ENV = 'production' // 用来记录当前打包的模块路径列表 let moduleList = (process.argv[2] || "").replace(/\s*$/g, "") if (moduleList == "") { moduleList = require('./module-conf').moduleList || [] } else { moduleList = moduleList.split(',') } // console.log('----moduleList-----:\n',moduleList,'\n----------------------') // 如果有传参时,对传入的参数进行检测,如果参数非法,那么停止打包操作 const checkModule = require('./module-conf').checkModule for (let i = moduleList.length - 1; i >= 0; i--) { if (!checkModule(moduleList[i])) { return } } const path = require('path') // const ora = require('ora') const rm = require('rimraf') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const webpackConfig = require('./webpack.base.conf') let webpackList = [], rmList = []; //删除文件 function getRm(pathStr) { return new Promise((resolve, reject) => { rm(pathStr, err => { if (err) { reject(pathStr + 'del failed!!'); throw err; } resolve(); }); }).then(res => { //console.log(res) }); } //构建webpack 配置 for (let i = moduleList.length - 1; i >= 0; i--) { let MODULE = moduleList[i]; //构建时要删除的模块目录 rmList.push(getRm(path.resolve(__dirname, '../dist', MODULE))); //添加每个模块的webpack 配置 let MODULE_NAME = MODULE.split('/').pop(); let entry = {}; entry[MODULE_NAME] = `${moduleList[i]}/index.js`; let wpk = merge({}, webpackConfig, { entry: entry, output: { path: path.resolve(__dirname, '../dist', MODULE), publicPath:`/e5/render/${packageConfig.author}/${MODULE.replace('./','')}` } }); let htmlTemplate = `${MODULE}/index.html` var checkPath = fs.existsSync(htmlTemplate); //如果目录存在 返回 true ,如果目录不存在 返回false if (checkPath == false) { htmlTemplate = path.resolve(__dirname, '../index.html') } wpk.plugins.push( new HtmlWebpackPlugin({ filename: path.resolve(__dirname, '../dist', MODULE, 'index.html'), template: htmlTemplate,//如果html 不存在考虑用全局根目录下 inject: true, // 这里要指定把哪些chunks追加到html中,默认会把所有入口的chunks追加到html中,这样是不行的 // chunks: ['vendor', 'manifest', 'elementUI', 'hiui', MODULE_NAME], minify: { removeComments: true,//去除注释 collapseWhitespace: true,//是否去除空格 } })) webpackList.push(wpk); } //编译所有 // webpack(webpackList, (err, stats) => { // if (err || stats.hasErrors()) {//编译错误不在 err 对象内,而是需要使用 stats.hasErrors() 单独处理 // const info = stats.toJson(); // // console.error(info.errors); // for(let i=0,l=info.errors.length;i<l;i++){ // console.log(chalk.red(info.errors[i].message)); // } // // process.exit(1); // } // // console.log(chalk.cyan(' Build complete.\n')); // }); console.log(chalk.green(`正在编译,总共(${moduleList.length})页面...`)) console.log(`===============================================================`); console.log(chalk.yellow(`序号\t 模块\t 路径`)); console.log(`=================================================================`); let webpackIndex=0; function doWebpack(webpackItem){ let mods = []; for(let name in webpackItem.entry){ let ent = webpackItem.entry[name]; ent = ent.replace(/\/index\.js/gi,''); ent = ent.replace(/\/dtv\/1\.0\.0\/desktop/gi,''); ent = ent.split('/').pop(); console.log(`${webpackIndex}\t${ent}\t 路径`,webpackItem.entry[name]); } webpack(webpackItem, (err, stats) => { if (err || stats.hasErrors()) {//编译错误不在 err 对象内,而是需要使用 stats.hasErrors() 单独处理 const info = stats.toJson(); // console.error(info.errors); for(let i=0,l=info.errors.length;i<l;i++){ console.log(chalk.red(info.errors[i].message)); } // process.exit(1); } //else{ // 处理完成 webpackIndex++; webpackList[webpackIndex]&&doWebpack(webpackList[webpackIndex]); // } }); } doWebpack(webpackList[webpackIndex]);