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
adda
master
1 parent
d345f59
commit
623b8133cbde7169bf6f9c2f44e590e88bb65b67
caibinghong
authored
on 3 Dec 2021
Showing
3 changed files
build/build.js
build/webpack.base.conf.js
build/webpack.config.js
Ignore Space
Show notes
View
build/build.js
'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]);
'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) throw err; // if (stats.hasErrors()) { // stats.stats.map(item => { // item.compilation.errors.map(msg => { // console.log(chalk.red(msg)); // }) // }) // // console.log(chalk.red(' Build failed with errors.\n')); // 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]);
Ignore Space
Show notes
View
build/webpack.base.conf.js
//考虑用,parallel-webpack 并发打包加快打包速度 const path = require('path') const webpack = require('webpack') const VueLoaderPlugin = require('vue-loader/lib/plugin') const TerserPlugin = require("terser-webpack-plugin"); const MiniCssExtractPlugin = require('mini-css-extract-plugin');//本插件基于 webpack v4 的新特性(模块类型)构建,并且需要 webpack 4 才能正常工作。 const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin') const ProgressBarPlugin = require('progress-bar-webpack-plugin'); const chalk = require('chalk') //运行打包命令的时候就可以看到每个loader 和插件执行耗时 const SpeedMeasurePlugin = require("speed-measure-webpack-plugin"); const utils = require('./utils') const packageConfig = require('../package.json') const vueLoaderConfig = require('./vue-loader.conf') // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin //读取.evn 对应的文件,写入环境变量 var dotenv = require('dotenv').config({ path: resolve('.env.' + process.env.NODE_ENV) }); var cfg = { ...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))) } let evnConfig = {} for (let key in cfg) { evnConfig[key] = '"' + cfg[key] + '"' } //路径转换 function resolve(dir) { // console.log(dir,'---------------------------',path.join(__dirname, '..', dir)) return path.join(__dirname, '..', dir) } //语法验证 const createLintingRule = () => ({ test: /\.(js|vue|dtvevt)$/, loader: 'eslint-loader', enforce: 'pre', include: [resolve('src'), resolve('test')], options: { formatter: require('eslint-friendly-formatter'), emitWarning: true } }) const useEslint = true; //project 这个目录是暂时命名,当用hivui create 创建时候 会根据用户输入确定 const projectName = packageConfig.name == 'hi-vui-template' ? 'project' : packageConfig.name; const smp = new SpeedMeasurePlugin();//smp.wrap smp.wrap( module.exports = { //指定目录,默认的值就是项目的根目录 // context: resolve(projectName), //编译入口 String||Array // entry: '', //没有指定入口文件时,按目录搜索 对应的package.json所在的入口 entry() { // 初始化入口配置 const entry = {} // 所有模块的列表 const moduleToBuild = require('./module-conf').getModuleToBuild() || [] // 根据传入的待打包目录名称,构建多入口配置 for (let module of moduleToBuild) { // entry[module] = `${module}/index.js` let name = module.replace(/\//gi, '_'); entry[name] = `${module}/index.js` } return entry }, //输出路径 output: { path: resolve('dist'), filename: 'static/js/[name].js', //指定打包后的资源目录 相对于dist 下 模块index.html publicPath: './' }, //如何寻找模块所对应的文件 resolve: { // 配置省略文件路径后缀名,引入文件的时候可以不用写文件的后缀名 extensions: ['.css', '.less', '.js', '.jsx', '.vue', '.json'], //指定引用别名 alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve(projectName), '@main': resolve(projectName + '/hivuiMain'), } }, //排除不打包 externals: { // 'axios':'axios', 'vue': 'Vue', "element-ui": "ELEMENT", // 'vue-router': 'VueRouter', // 'vuex':'Vuex', }, //解析模块的规则 module: { rules: [ // ...(useEslint ? [createLintingRule()] : []), ...(utils.styleLoaders({ sourceMap: false, extract: true, usePostCSS: true })), { test: /\.(vue)$/, loader: 'vue-loader', options: vueLoaderConfig }, { test: /\.(js|jsx)$/, loader: 'babel-loader', exclude: /node_modules/, options: { cacheDirectory: true } // exclude: [ // /node_modules\/(webpack|html-webpack-plugin)\//, // ], }, // element amdin src/icons使用 // { // test: /\.svg$/, // loader: 'svg-sprite-loader', // include: [resolve('src/icons')], // options: { // symbolId: 'icon-[name]' // } // }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: 'static/img/[name].[ext]'//.[hash:7] // publicPath:"../img/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 // outputPath:"static/img/" //生成之后存放的路径 } }, { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: 'static/media/[name].[ext]'//.[hash:7] // publicPath:"../media/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 // outputPath:"static/media/" //生成之后存放的路径 } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: "[name].[ext]",//[hash:8]. publicPath: "../fonts/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 outputPath: "static/fonts/" //生成之后存放的路径 // name: 'static/fonts/[name].[hash:7].[ext]' } } ] }, devtool: "source-map", optimization: { minimizer: [ new CssMinimizerPlugin(), new MiniCssExtractPlugin({ filename: 'static/css/[name].[contenthash:10].css'//.[contenthash] }), new TerserPlugin() ], splitChunks: { chunks: 'all', cacheGroups: { // vue: { // name: 'vue', // test: /[\\/]node_modules[\\/]vue[\\/]/, // priority: -10 // }, // vuex: { // name: 'vuex', // test: /[\\/]node_modules[\\/]vuex[\\/]/, // priority: -10 // }, // 'vue-router': { // name: 'vue-router', // test: /[\\/]node_modules[\\/]vue-router[\\/]/, // priority: -10 // }, // 'element-ui': { // name: 'element-ui', // test: /[\\/]node_modules[\\/]element-ui[\\/]/, // priority: -10 // }, 'module-vendors': { name: 'module-vendors', test: /[\\/]node_modules[\\/]/, priority: -20 } }, } }, plugins: [ new ProgressBarPlugin({ // complete: "█", // format: `${chalk.green('Building')} [ ${chalk.blue(':bar')} ] ':msg:' ${chalk.bold('(:percent)')}`, // clear: true }), //注入全局变量的插件,通常使用该插件来判别代码运行的环境变量 new webpack.DefinePlugin({ 'process.env': evnConfig }), new VueLoaderPlugin(), //预设一些基础库 new webpack.DllReferencePlugin({ context: resolve("assets_platform/vendor_dll"), //告诉webpack哪些库不参与打包,同时使用时名称也得变 manifest:resolve('assets_platform/vendor_dll/vuelibs-manifest.json') }), new webpack.DllReferencePlugin({ context: resolve("assets_platform/vendor_dll"), //告诉webpack哪些库不参与打包,同时使用时名称也得变 manifest:resolve('assets_platform/vendor_dll/vendor-manifest.json') }), // new AddAssetHtmlPlugin({ // filepath: resolve('webpackDll/Vue.js'), // outputPath:'./static/js/', // publicPath:'./static/js/' // }), ], };
//考虑用,parallel-webpack 并发打包加快打包速度 const path = require('path') const webpack = require('webpack') const VueLoaderPlugin = require('vue-loader/lib/plugin') const TerserPlugin = require("terser-webpack-plugin"); const MiniCssExtractPlugin = require('mini-css-extract-plugin');//本插件基于 webpack v4 的新特性(模块类型)构建,并且需要 webpack 4 才能正常工作。 const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin') const ProgressBarPlugin = require('progress-bar-webpack-plugin'); const chalk = require('chalk') //运行打包命令的时候就可以看到每个loader 和插件执行耗时 const SpeedMeasurePlugin = require("speed-measure-webpack-plugin"); const utils = require('./utils') const packageConfig = require('../package.json') const vueLoaderConfig = require('./vue-loader.conf') // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin //读取.evn 对应的文件,写入环境变量 var dotenv = require('dotenv').config({ path: resolve('.env.' + process.env.NODE_ENV) }); var cfg = { ...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))) } let evnConfig = {} for (let key in cfg) { evnConfig[key] = '"' + cfg[key] + '"' } //路径转换 function resolve(dir) { // console.log(dir,'---------------------------',path.join(__dirname, '..', dir)) return path.join(__dirname, '..', dir) } //语法验证 const createLintingRule = () => ({ test: /\.(js|vue|dtvevt)$/, loader: 'eslint-loader', enforce: 'pre', include: [resolve('src'), resolve('test')], options: { formatter: require('eslint-friendly-formatter'), emitWarning: true } }) const useEslint = true; //project 这个目录是暂时命名,当用hivui create 创建时候 会根据用户输入确定 const projectName = packageConfig.name == 'hi-vui-template' ? 'project' : packageConfig.name; const smp = new SpeedMeasurePlugin();//smp.wrap smp.wrap( module.exports = { //指定目录,默认的值就是项目的根目录 // context: resolve(projectName), //编译入口 String||Array // entry: '', //没有指定入口文件时,按目录搜索 对应的package.json所在的入口 entry() { // 初始化入口配置 const entry = {} // 所有模块的列表 const moduleToBuild = require('./module-conf').getModuleToBuild() || [] // 根据传入的待打包目录名称,构建多入口配置 for (let module of moduleToBuild) { // entry[module] = `${module}/index.js` let name = module.replace(/\//gi, '_'); entry[name] = `${module}/index.js` } return entry }, //输出路径 output: { path: resolve('dist'), filename: 'static/js/[name].js', //指定打包后的资源目录 相对于dist 下 模块index.html publicPath: './' }, //如何寻找模块所对应的文件 resolve: { // 配置省略文件路径后缀名,引入文件的时候可以不用写文件的后缀名 extensions: ['.css', '.less', '.js', '.jsx', '.vue', '.json'], //指定引用别名 alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve(projectName), '@main': resolve(projectName + '/hivuiMain'), } }, //排除不打包 externals: { // 'axios':'axios', 'vue': 'Vue', "element-ui": "ELEMENT", // 'vue-router': 'VueRouter', // 'vuex':'Vuex', }, //解析模块的规则 module: { rules: [ // ...(useEslint ? [createLintingRule()] : []), ...(utils.styleLoaders({ sourceMap: false, extract: true, usePostCSS: true })), { test: /\.(vue)$/, loader: 'vue-loader', options: vueLoaderConfig }, { test: /\.(js|jsx)$/, loader: 'babel-loader', exclude: /node_modules/, options: { cacheDirectory: true } // exclude: [ // /node_modules\/(webpack|html-webpack-plugin)\//, // ], }, // element amdin src/icons使用 // { // test: /\.svg$/, // loader: 'svg-sprite-loader', // include: [resolve('src/icons')], // options: { // symbolId: 'icon-[name]' // } // }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: 'static/img/[name].[ext]'//.[hash:7] // publicPath:"../img/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 // outputPath:"static/img/" //生成之后存放的路径 } }, { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: 'static/media/[name].[ext]'//.[hash:7] // publicPath:"../media/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 // outputPath:"static/media/" //生成之后存放的路径 } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, name: "[name].[ext]",//[hash:8]. publicPath: "../fonts/", //替换CSS引用的图片路径 可以替换成爱拍云上的路径 outputPath: "static/fonts/" //生成之后存放的路径 // name: 'static/fonts/[name].[hash:7].[ext]' } } ] }, devtool: "source-map", optimization: { minimizer: [ new CssMinimizerPlugin(), new MiniCssExtractPlugin({ filename: 'static/css/[name].[contenthash:10].css'//.[contenthash] }), new TerserPlugin() ], splitChunks: { chunks: 'all', cacheGroups: { // vue: { // name: 'vue', // test: /[\\/]node_modules[\\/]vue[\\/]/, // priority: -10 // }, // vuex: { // name: 'vuex', // test: /[\\/]node_modules[\\/]vuex[\\/]/, // priority: -10 // }, // 'vue-router': { // name: 'vue-router', // test: /[\\/]node_modules[\\/]vue-router[\\/]/, // priority: -10 // }, // 'element-ui': { // name: 'element-ui', // test: /[\\/]node_modules[\\/]element-ui[\\/]/, // priority: -10 // }, 'module-vendors': { name: 'module-vendors', test: /[\\/]node_modules[\\/]/, priority: -20 } }, } }, plugins: [ new ProgressBarPlugin({ complete: "█", format: `${chalk.green('Building')} [ ${chalk.blue(':bar')} ] ':msg:' ${chalk.bold('(:percent)')}`, clear: true }), //注入全局变量的插件,通常使用该插件来判别代码运行的环境变量 new webpack.DefinePlugin({ 'process.env': evnConfig }), new VueLoaderPlugin(), //预设一些基础库 new webpack.DllReferencePlugin({ context: resolve("assets_platform/vendor_dll"), //告诉webpack哪些库不参与打包,同时使用时名称也得变 manifest:resolve('assets_platform/vendor_dll/vuelibs-manifest.json') }), new webpack.DllReferencePlugin({ context: resolve("assets_platform/vendor_dll"), //告诉webpack哪些库不参与打包,同时使用时名称也得变 manifest:resolve('assets_platform/vendor_dll/vendor-manifest.json') }), // new AddAssetHtmlPlugin({ // filepath: resolve('webpackDll/Vue.js'), // outputPath:'./static/js/', // publicPath:'./static/js/' // }), ], };
Ignore Space
Show notes
View
build/webpack.config.js
'use strict' process.env.NODE_ENV = 'production' require('./check-versions')() const fs = require("fs") const chalk = require('chalk') const { merge } = require('webpack-merge'); // 用来记录当前打包的模块路径列表,判断进程参数 let moduleList = (process.argv[2] || "").replace(/\s*$/g, "") if (moduleList == "") { moduleList = require('./module-conf').moduleList || [] } else { moduleList = moduleList.split(',') } // 如果有传参时,对传入的参数进行检测,如果参数非法,那么停止打包操作 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 = []; const packageConfig = require('../package.json') //删除文件 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] = `${MODULE}/index.js`; // console.log(`/e5/${MODULE.replace('./','')}`); let wpk = merge({}, webpackConfig, { entry: entry, output: { path: path.resolve(__dirname, '../dist', MODULE), // filename: 'static/js/[name].[contenthash:10].js' // publicPath:`/e5/render/${MODULE.replace('./','')}` 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','app','hiui', MODULE_NAME], minify: { removeComments: true,//去除注释 collapseWhitespace: true,//是否去除空格 removeAttributeQuotes: true//去除空属性 }, compile: true })) webpackList.push(wpk); } module.exports = webpackList;
'use strict' process.env.NODE_ENV = 'production' require('./check-versions')() const fs = require("fs") const chalk = require('chalk') const { merge } = require('webpack-merge'); // 用来记录当前打包的模块路径列表,判断进程参数 let moduleList = (process.argv[2] || "").replace(/\s*$/g, "") if (moduleList == "") { moduleList = require('./module-conf').moduleList || [] } else { moduleList = moduleList.split(',') } // 如果有传参时,对传入的参数进行检测,如果参数非法,那么停止打包操作 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 = []; const packageConfig = require('../package.json') //删除文件 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] = `${MODULE}/index.js`; // console.log(`/e5/${MODULE.replace('./','')}`); let wpk = merge({}, webpackConfig, { entry: entry, output: { path: path.resolve(__dirname, '../dist', MODULE), // filename: 'static/js/[name].[contenthash:10].js' // publicPath:`/e5/render/${MODULE.replace('./','')}` publicPath:`/e5/render/${packageConfig.author}/${MODULE.replace('./','')}` // /e5/render/0000206/jwjx/hivuiMain/static/css/hivuiMain.5f904307f4.css } }); 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','app','hiui', MODULE_NAME], minify: { removeComments: true,//去除注释 collapseWhitespace: true,//是否去除空格 removeAttributeQuotes: true//去除空属性 }, compile: true })) webpackList.push(wpk); } module.exports = webpackList;
Show line notes below