UNPKG

aiot-qiankun-ucf-scripts

Version:
110 lines (97 loc) 3.58 kB
/* Start Webpack4 config * @Author: Kvkens(yueming@yonyou.com) * @Date: 2019-01-21 13:02:27 * @Last Modified by: Kvkens * @Last Modified time: 2019-05-10 10:45:56 */ const glob = require('glob'); const HtmlWebPackPlugin = require('html-webpack-plugin'); const webpack = require('webpack'); const merge = require('webpack-merge'); const argv = require("minimist")(process.argv.slice(2)); const commands = argv; const util = require('./util'); const base = require('./base.config'); const AlterHtmlContentPlugin = require('./plugins/alter-html-content-plugin');// htmlwebpackplugin处理之后对html内容更改 const cfg = util.getUcfConfig(commands._); const consul = cfg.consul || {needConsul:true, consulService:'consul'}; // 是否开启consul配置中心 delete cfg.consul; //当前应用模式 //入口集合 const entries = {}; //HTML插件 const HtmlPlugin = []; //启动器控制 const _bootList = new Set(); // 启动器 const bootList = cfg.bootList ? cfg.bootList : true; // 扫描微应用入口规则 const scan_root = cfg.scan_root ? cfg.scan_root : 'ucf-apps'; //加载本地开发环境的Portal glob.sync('./ucf-common/src/portal/src/app.js').forEach(_path => { entries['index'] = './ucf-common/src/portal/src/app.js'; const htmlConf = { filename: `index.html`, template: './ucf-common/src/portal/src/index.html', inject: 'body', chunks: ['index'], hash: true }; HtmlPlugin.push(new HtmlWebPackPlugin(htmlConf)); }); //构造模块加载入口以及html出口 glob.sync(`./${scan_root}/**/src/app.js`).forEach(_path => { let _context = ""; //TODO 当publicPath=true 因为start.js webpack-dev-middleware 中配置了publicPath, 所以此处产出路径不在包含 context if (!cfg.publicPath && cfg.context) { _context = `${cfg.context}/`; } //模块名 const module = `${_path.split(`./${scan_root}/`)[1].split('/src/app.js')[0]}`; const chunk = `${_context}${module}/index`; const htmlConf = { filename: `${chunk}.html`, template: `${_path.split('/app.js')[0]}/index.html`, inject: 'body', chunks: [chunk], hash: true }; //处理启动器逻辑 if (bootList && typeof bootList == 'boolean') { entries[chunk] = [require.resolve('./webpack-hot-middleware/client'),_path]; HtmlPlugin.push(new HtmlWebPackPlugin(htmlConf)); HtmlPlugin.push(new AlterHtmlContentPlugin({context: cfg.context, consul})); } else if (Array.isArray(bootList) && bootList.length > 0) { bootList.forEach(item => { _bootList.add(item); }); if (_bootList.has(module)) { entries[chunk] = [require.resolve('./webpack-hot-middleware/client'),_path]; HtmlPlugin.push(new HtmlWebPackPlugin(htmlConf)); HtmlPlugin.push(new AlterHtmlContentPlugin({context: cfg.context, consul})); } } }); //默认的配置用于merge操作 const config = { devtool: "cheap-module-eval-source-map", mode: 'development', externals: cfg.externals, resolve: { alias: cfg.alias }, module: { rules: cfg.loader }, plugins: [ new webpack.HotModuleReplacementPlugin(), ...HtmlPlugin ] } //入口 config.entry = entries; //环境变量注入 cfg.global_env && (config.plugins = config.plugins.concat(new webpack.DefinePlugin(cfg.global_env))); //传入插件设置 cfg.devPlugins && (config.plugins = config.plugins.concat(cfg.devPlugins)); module.exports = merge(base, config);