index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * @Author: liyxt
  3. * @Date: 2018-09-17 11:10:21
  4. * @LastEditors: liyxt
  5. * @LastEditTime: 2020-04-08 09:34:15
  6. * @Description: file content
  7. */
  8. const configJSON = require('../config.json');
  9. const buildEntry = require('./buildEntry');
  10. const { spawn } = require('child_process');
  11. // buildPath是npm参数
  12. let buildParam,
  13. paramMap = {};
  14. if (process.env.npm_config_argv) {
  15. [, , ...buildParam] = JSON.parse(process.env.npm_config_argv).original;
  16. } else {
  17. [, , , ...buildParam] = process.argv;
  18. }
  19. buildParam.forEach(param => {
  20. let key = param.split('=')[0],
  21. value = param.split('=')[1];
  22. paramMap[key] ? paramMap[key].push(value) : (paramMap[key] = [value]);
  23. });
  24. let buildEntryPath = (paramMap['--env.buildPath'] || []).filter(e => e),
  25. buildOutputPath = paramMap['--env.buildOutputPath'] || [];
  26. if (!buildEntryPath.length) {
  27. buildEntryPath = configJSON.buildEntryPath || './src/*/*/*/*/index.js';
  28. }
  29. let buildWithoutHTML = configJSON.buildWithoutHTML;
  30. buildWithoutHTML && typeof buildWithoutHTML === 'string' && (buildWithoutHTML = [buildWithoutHTML]);
  31. // mode是nodeJs参数
  32. let [, , mode, client] = process.argv;
  33. let { entries: hashEntries } = buildEntry({ buildPath: buildEntryPath, buildWithoutHTML, hash: true, client });
  34. let { entries } = buildEntry({ buildPath: buildEntryPath, buildWithoutHTML, hash: false, client });
  35. // 加载打包二开相关文件
  36. let extendBuildEntryPath = configJSON.extendBuildEntryPath || [];
  37. let { entries: extendEntries } = buildEntry({
  38. buildPath: extendBuildEntryPath,
  39. buildWithoutHTML,
  40. hash: false,
  41. client,
  42. fse: true
  43. });
  44. if (Object.keys(hashEntries).length) {
  45. runSpawn(buildEntryPath, mode, true, false);
  46. }
  47. if (Object.keys(entries).length) {
  48. runSpawn(buildEntryPath, mode, false, false);
  49. }
  50. if (Object.keys(extendEntries).length) {
  51. runSpawn(extendBuildEntryPath, mode, false, true);
  52. }
  53. function runSpawn(buildEntryPath, mode, hash, fse) {
  54. const ls = spawn('node', [
  55. '--max_old_space_size=8192',
  56. 'node_modules/webpack/bin/webpack.js',
  57. '--progress',
  58. '--colors',
  59. '--config',
  60. './config/webpack.prod.config.js',
  61. `--env.mode=${mode}`,
  62. `--env.hash=${hash}`,
  63. `--env.client=${client}`,
  64. `--env.fse=${fse}`,
  65. `--env.outputPath=${buildOutputPath.join('/')}`,
  66. ...buildEntryPath.map(e => '--env.buildPath=' + e)
  67. ]);
  68. ls.stdout.on('data', data => {
  69. if (data.includes('ERROR')) {
  70. throw new Error(data);
  71. } else {
  72. data && console.log(`${data}`);
  73. }
  74. });
  75. ls.stderr.on('data', data => {
  76. if (data.includes('ERROR')) {
  77. throw new Error(data);
  78. } else {
  79. data && console.log(`${data}`);
  80. }
  81. });
  82. }