var fsextra = require('fs-extra');
const path = require("path");
const glob = require('glob')
// const fs = require("fs");
// const {Chalk} = require('chalk')
const merge = require('webpack-merge');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const baseWebpackConfig = require('./webpack.base.conf');
const packageConfig = require('../package.json');
const projectName = packageConfig.name=='hi-vui-template'?'project':packageConfig.name
// console.log('---------------------webpack.get-list.js------------', process.env.NODE_ENV)
function repeat(array) {
let map = new Map();
let array1 = []; // 数组用于返回结果
for (let i = 0, leng = array.length; i < leng; i++) {
if (map.has(array[i])) {
// 判断是否存在key
map.set(array[i], true);
} else {
map.set(array[i], false);
array1.push(array[i]);
}
}
return array1;
}
// 用来记录当前打包的模块路径列表,判断进程参数
let argvs = (process.argv[2] || "").replace(/\s*$/g, "")
let moduleList = [];
let isBuildAll=true;
if (argvs) {
isBuildAll=false;
moduleList = [];
let list = argvs.split(',');
for (let i = 0, l = list.length; i < l; i++) {
// let checkPath = fs.existsSync(list[i]+'/package.json');
//如果目录存在 返回 true ,如果目录不存在 返回false
// if (checkPath == false) continue;
let modulePath = list[i];
let pathList = glob.sync(modulePath + '/package.json'); // 这个执行比较快 114.811ms
if (pathList.length == 0) {
// 查找modulePath目录
// let ls = glob.sync(`${modulePath}/!(dist|node_modules|build)/**/package.json`)
// for (let k = 0, l = ls.length; k < l; k++) {
// moduleList.push(ls[k].replace('/package.json', ''));
// }
if(modulePath=='./assets_platform'){
let res = modulePath.replace('./', '');
var isexist = fsextra.existsSync(path.resolve(__dirname, '../', res));
isexist&&fsextra.copy(path.resolve(__dirname, '../', res), path.resolve(__dirname, '../dist/', res), function (err) {
// ../project/AnNiu/res
if (err){
return console.error('build_error:'+err)
} else{
return console.log(res,"拷贝文件成功!")
}
}) //拷贝模块资源目录
}else if(/\/deploy\/desktop\/[\w,\/,\.]*\/dpdst\/[\w,\/,\.]*(\.json)$/.test(modulePath)){//服务部署环境配置的.json
let buildScheme=fsextra.readJsonSync(path.resolve(__dirname, '../', modulePath));
let settingPath=path.resolve(__dirname, '../dist/',projectName+'/setting/desktop/appsetting.js')
fsextra.copy(path.resolve(__dirname, '../', buildScheme.appSetting), settingPath, function (err) {
if (err){
return console.error('build_error:'+err)
} else{
return console.log(settingPath,"拷贝文件成功!")
}
}) //拷贝模块资源目录
}else{// if(/(\/res)$/.test(modulePath))
let res = modulePath.replace('./', '');
var isexist = fsextra.existsSync(path.resolve(__dirname, '../', res));
isexist&&fsextra.copy(path.resolve(__dirname, '../', res), path.resolve(__dirname, '../dist/', res), function (err) {
// ../project/AnNiu/res
if (err){
return console.error('build_error:'+err)
} else{
return console.log(res,"拷贝文件成功!")
}
}) //拷贝模块资源目录
}continue
} else {
moduleList.push(modulePath);
}
}
} else {
moduleList = require('./webpack.get-module').moduleList || [];
}
moduleList = repeat(moduleList); //去重
let evnConfig = require('./getEnvVar')
let webpackList = [],
resList = [];
//构建webpack 配置
for (let i = 0, l = moduleList.length; i < l; i++) {
let MODULE = moduleList[i];
if(isBuildAll){
//全部编译时拷资源文件res
let res = MODULE.indexOf('/view') > -1 ? MODULE.split('view')[0] + '/res' : '';
if (res) {
res = res.replace('./', '');
var isexist = fsextra.existsSync(path.resolve(__dirname, '../', res));
isexist&&fsextra.copy(path.resolve(__dirname, '../', res), path.resolve(__dirname, '../dist/', res), function (err) {
if (err) return console.error(err)
}) //拷贝模块资源目录
}
}
//添加每个模块的webpack 配置
let MODULE_NAME = MODULE.split('/').pop();
let entry = {};
let outputPath = path.resolve(__dirname, '../dist', MODULE)
entry[MODULE_NAME] = ['babel-polyfill', `${MODULE}/index.js`]; // @babel/polyfill
let wpk = merge({
mode: 'production',
// mode: 'development',
// devtool: 'source-map',
}, baseWebpackConfig, {
entry: entry,
output: {
path: outputPath,
filename: `static/js/[name].js`,
// publicPath:'./'
// publicPath: `/${evnConfig.VITE_APP_SERVER.replace(/"/gi,"")}/render/${packageConfig.author}/${MODULE.replace('./', '')}/`
publicPath: `${evnConfig.VITE_APP_USERPATH.replace(/"/gi,"")}/${MODULE.replace('./', '')}/`
}
});
let htmlTemplate = `${MODULE}/index.html`
// var checkPath = fs.existsSync(htmlTemplate);
// //如果目录存在 返回 true ,如果目录不存在 返回false
// if (checkPath == false) {
// htmlTemplate = path.resolve(__dirname, './template.html')
// }
let pathList = glob.sync(htmlTemplate); // 这个执行比较快 114.811ms
if (pathList.length == 0) htmlTemplate = path.resolve(__dirname, './template.html');
wpk.plugins.push(new CleanWebpackPlugin([outputPath], {
root: path.resolve(__dirname, '../'), //根目录
//其他配置按需求添加
verbose: false, //不提示删除
}));
wpk.plugins.push(
new MiniCssExtractPlugin({
filename: 'static/css/[name].[contenthash].css',
chunkFilename: `static/css/[name].[contenthash].css`
})
);
wpk.plugins.push(
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, '../dist', MODULE, 'index.html'),
template: htmlTemplate, //如果html 不存在考虑用全局根目录下
inject: true,
minify: {
removeComments: true, //去除注释
collapseWhitespace: true, //是否去除空格
removeAttributeQuotes: true //去除空属性
},
compile: true
}))
webpackList.push(wpk);
}
module.exports = webpackList; // [webpackList[4]] ////[webpackList[3]];//27