1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- const program = require("commander");
- const tools = require("./tools");
- const HtmlWebpackPlugin = require("html-webpack-plugin");
- const paths = require("../config/paths");
- const myConfig = require("../src/global");
- program
- .version("0.1.0")
- .usage("[options] <file ...>")
- .option("-b, --build", "是否是打包输出到dist")
- .option("-a, --all", "是否操作全部")
- .option("-d, --dev", "开发")
- .option("-p, --pro", "生成")
- .option("-n, --nginx", "打包到nginx")
- .option("-m, --map", "线上环境是否生成map文件")
- .parse(process.argv);
- async function buildOrDev() {
- let isEnvProduction = process.env.NODE_ENV === "production";
- let params = {
- configEntry: {},
- htmls: null,
- isNginx: program.nginx ? program.nginx : false,
- CONFIG: program.pro //dev和pro区别配置
- ? myConfig.pro //生成
- : myConfig.dev, //开发
- };
- let targetProject;
- if (program.all) {
- // 读取pages下面所有构建文件
- let allFiles = await tools.getAllBuildFile();
- targetProject = Array.isArray(allFiles) ? allFiles : ["login"];
- } else {
- if (program["_name"] === "build") {
- targetProject = program.args;
- } else {
- targetProject = [...new Set([...program.args, "login", "404"])];
- }
- }
- if (program["_name"] === "build") {
- if(!program.map){
- process.env.GENERATE_SOURCEMAP ='false'
- }
- console.log(
- "启动打包===>",
- program.pro ? "线上" : "测试",
- program.nginx ? "打包到nginx" : "打包到项目目录下",
- program.map ? '生成MAP文件':'不生成MAP文件'
- );
- } else {
- console.log(
- "启动本地环境===>",
- program.pro ? "线上" : "测试",
- program.nginx ? "打包到nginx" : "打包到项目目录下"
- );
- }
- console.log(targetProject);
- /* 设定入口文件 */
- targetProject.forEach((item) => {
- params.configEntry[item] =
- paths.appPath + "/src/pages/" + item + "/entry/index.tsx";
- });
- /* 设定html输出 */
- params.htmls = targetProject.map((item) => {
- /* https://www.webpackjs.com/plugins/html-webpack-plugin/ */
- return new HtmlWebpackPlugin(
- Object.assign(
- {},
- {
- inject: true,
- title: item,
- filename:item !== 'login'? `${item}/index.html`:`index.html`, //HashRouter路由将login的HTML单独拿出来放在主目录下
- // filename:`${item}/index.html`, //BrowserRouter路由多入口
- template: paths.appPath + "/build/index.html",
- chunks: [item],
- },
- isEnvProduction
- ? {
- minify: {
- removeComments: true,
- collapseWhitespace: true,
- removeRedundantAttributes: true,
- useShortDoctype: true,
- removeEmptyAttributes: true,
- removeStyleLinkTypeAttributes: true,
- keepClosingSlash: true,
- minifyJS: true,
- minifyCSS: true,
- minifyURLs: true,
- },
- }
- : undefined
- )
- );
- });
- return params;
- }
- module.exports = buildOrDev;
|