GitBucket
4.6.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull Requests
Labels
Milestones
Wiki
08335
/
hivui-platform-template
hivui平台项目模板
Browse code
编译命令优化:判断环境编译指定功能 目录时,自动爬取相关package.json目录
master
1 parent
5556bcf
commit
6ed4bcb752b888c9256544121765451cdef231b5
caibinghong
authored
on 13 May 2022
Showing
1 changed file
build/webpack.get-list.js
Ignore Space
Show notes
View
build/webpack.get-list.js
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
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) // 用来记录当前打包的模块路径列表,判断进程参数 let argvs = (process.argv[2] || "").replace(/\s*$/g, "") let moduleList = []; if(argvs){ // console.log('指定模块编译:') 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) continue moduleList.push(list[i]); } }else{ moduleList = require('./webpack.get-module').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
Show line notes below