build.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const webpack = require('webpack');
  2. const chalk = require('chalk');
  3. const config = require('../config/webpack.config');
  4. const {clearConsole, errorLogger} = require('./utils');
  5. const {outputPath} = require('../config/paths');
  6. const fs = require('fs-extra');
  7. const MAX_CHUNK_SIZE = Number(process.env.MAX_CHUNK_SIZE ?? 0);
  8. function printAssets(assets) {
  9. console.log('Packaging resource list:');
  10. console.log();
  11. const list = [];
  12. let overstep = false;
  13. for (const [path, size] of Object.entries(assets)) {
  14. const isJs = path.lastIndexOf('.js') === path.length - 3;
  15. const isCss = path.lastIndexOf('.css') === path.length - 4;
  16. const isMedia = path.includes('static/assets');
  17. const sizeNum = size.size();
  18. const kbSize = Math.ceil(sizeNum / 1024);
  19. const sizePen
  20. = sizeNum > MAX_CHUNK_SIZE
  21. ? chalk.hex('#ff2121').bold
  22. : chalk.hex('#0aa344');
  23. const pathPen = isJs
  24. ? chalk.hex('#48c0a3')
  25. : isCss
  26. ? chalk.hex('#b0a4e3')
  27. : chalk.hex('#eacd76');
  28. if (isJs || isMedia || isCss) {
  29. if (!overstep)
  30. overstep = sizeNum > MAX_CHUNK_SIZE;
  31. list.push({
  32. path,
  33. size: sizeNum >= 1024 ? `${kbSize} KB` : `${sizeNum} B`,
  34. sizePen,
  35. pathPen,
  36. });
  37. }
  38. }
  39. const maxLength = list.reduce(function(pre, next) {
  40. return next.size.length > pre ? next.size.length : pre;
  41. }, 0);
  42. list.forEach(function({path, size, sizePen, pathPen}) {
  43. const afterSize = size.padEnd(maxLength + 2, ' ');
  44. console.log(sizePen(`Size: ${afterSize}`), pathPen(path));
  45. });
  46. if (overstep) {
  47. const varColor = '#177cb0';
  48. const pathColor = '#f00056';
  49. console.log();
  50. console.log();
  51. console.log(
  52. chalk.grey(
  53. `Some chunks are larger ${
  54. MAX_CHUNK_SIZE / 1024
  55. }KB after compilation. Consider:`,
  56. ),
  57. );
  58. console.log();
  59. console.log(
  60. chalk.hex(pathColor)(
  61. '1.Using dynamic import() to code-split the application',
  62. ),
  63. );
  64. console.log(
  65. chalk.hex(pathColor)(
  66. '2.Adjust the prompt size by adjusting'
  67. + ` ${chalk.hex(varColor)('SWT_MAX_CHUNK_SIZE')}`
  68. + ` in ${chalk.white('packages/app/.env')}`,
  69. ),
  70. );
  71. console.log(
  72. chalk.hex(pathColor)(
  73. `3.Modify ${chalk.hex(varColor)('splitChunks')} in `
  74. + `${chalk.white('packages/webpack/config/optimization.js')}`,
  75. ),
  76. );
  77. }
  78. }
  79. try {
  80. const compiler = webpack(config);
  81. fs.emptyDirSync(outputPath);
  82. compiler.run(function(err, stats) {
  83. if (err) {
  84. errorLogger('Failed to compile');
  85. console.log();
  86. console.log(err.message);
  87. console.log();
  88. process.exit(1);
  89. }
  90. const {assets, startTime, endTime} = stats.compilation;
  91. const time = endTime - startTime;
  92. clearConsole();
  93. console.log();
  94. console.log(chalk.green('Successfully 🤩'));
  95. console.log(
  96. chalk.gray(`Compiled successfully in ${(time / 1000).toFixed(2)}s`),
  97. );
  98. console.log();
  99. printAssets(assets);
  100. console.log();
  101. });
  102. } catch (error) {
  103. errorLogger('Failed to compile');
  104. console.log();
  105. console.log(error.message);
  106. console.log();
  107. process.exit(1);
  108. }