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');
// 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 = [];
if (argvs) {
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 pathList = glob.sync(list[i] + '/package.json'); // 这个执行比较快 114.811ms
if (pathList.length == 0) {
// 查找list[i]目录
let ls = glob.sync(`${list[i]}/!(dist|node_modules|build)/**/package.json`)
for (let k = 0, l = ls.length; k < l; k++) {
moduleList.push(ls[k].replace('/package.json',''));
}
continue
} else {
moduleList.push(list[i]);
}
}
} else {
moduleList = require('./webpack.get-module').moduleList || [];
}
moduleList = repeat(moduleList);//去重
let evnConfig = require('./getEnvVar')
let webpackList = [];
//构建webpack 配置
for (let i = 0, l = moduleList.length; i < l; i++) {
let MODULE = moduleList[i];
// console.log(i, MODULE);
//添加每个模块的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