extend.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const program = require("commander");
  2. const tools = require("./tools");
  3. const HtmlWebpackPlugin = require("html-webpack-plugin");
  4. const paths = require("../config/paths");
  5. const myConfig = require("../src/global");
  6. program
  7. .version("0.1.0")
  8. .usage("[options] <file ...>")
  9. .option("-b, --build", "是否是打包输出到dist")
  10. .option("-a, --all", "是否操作全部")
  11. .option("-d, --dev", "开发")
  12. .option("-p, --pro", "生成")
  13. .option("-n, --nginx", "打包到nginx")
  14. .option("-m, --map", "线上环境是否生成map文件")
  15. .parse(process.argv);
  16. async function buildOrDev() {
  17. let isEnvProduction = process.env.NODE_ENV === "production";
  18. let params = {
  19. configEntry: {},
  20. htmls: null,
  21. isNginx: program.nginx ? program.nginx : false,
  22. CONFIG: program.pro //dev和pro区别配置
  23. ? myConfig.pro //生成
  24. : myConfig.dev, //开发
  25. };
  26. let targetProject;
  27. if (program.all) {
  28. // 读取pages下面所有构建文件
  29. let allFiles = await tools.getAllBuildFile();
  30. targetProject = Array.isArray(allFiles) ? allFiles : ["login"];
  31. } else {
  32. if (program["_name"] === "build") {
  33. targetProject = program.args;
  34. } else {
  35. targetProject = [...new Set([...program.args, "login", "404"])];
  36. }
  37. }
  38. if (program["_name"] === "build") {
  39. if(!program.map){
  40. process.env.GENERATE_SOURCEMAP ='false'
  41. }
  42. console.log(
  43. "启动打包===>",
  44. program.pro ? "线上" : "测试",
  45. program.nginx ? "打包到nginx" : "打包到项目目录下",
  46. program.map ? '生成MAP文件':'不生成MAP文件'
  47. );
  48. } else {
  49. console.log(
  50. "启动本地环境===>",
  51. program.pro ? "线上" : "测试",
  52. program.nginx ? "打包到nginx" : "打包到项目目录下"
  53. );
  54. }
  55. console.log(targetProject);
  56. /* 设定入口文件 */
  57. targetProject.forEach((item) => {
  58. params.configEntry[item] =
  59. paths.appPath + "/src/pages/" + item + "/entry/index.tsx";
  60. });
  61. /* 设定html输出 */
  62. params.htmls = targetProject.map((item) => {
  63. /* https://www.webpackjs.com/plugins/html-webpack-plugin/ */
  64. return new HtmlWebpackPlugin(
  65. Object.assign(
  66. {},
  67. {
  68. inject: true,
  69. title: item,
  70. filename:item !== 'login'? `${item}/index.html`:`index.html`, //HashRouter路由将login的HTML单独拿出来放在主目录下
  71. // filename:`${item}/index.html`, //BrowserRouter路由多入口
  72. template: paths.appPath + "/build/index.html",
  73. chunks: [item],
  74. },
  75. isEnvProduction
  76. ? {
  77. minify: {
  78. removeComments: true,
  79. collapseWhitespace: true,
  80. removeRedundantAttributes: true,
  81. useShortDoctype: true,
  82. removeEmptyAttributes: true,
  83. removeStyleLinkTypeAttributes: true,
  84. keepClosingSlash: true,
  85. minifyJS: true,
  86. minifyCSS: true,
  87. minifyURLs: true,
  88. },
  89. }
  90. : undefined
  91. )
  92. );
  93. });
  94. return params;
  95. }
  96. module.exports = buildOrDev;