build.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) overstep = sizeNum > MAX_CHUNK_SIZE;
  30. list.push({
  31. path,
  32. size: sizeNum >= 1024 ? `${kbSize} KB` : `${sizeNum} B`,
  33. sizePen,
  34. pathPen,
  35. });
  36. }
  37. }
  38. const maxLength = list.reduce(function (pre, next) {
  39. return next.size.length > pre ? next.size.length : pre;
  40. }, 0);
  41. list.forEach(function ({path, size, sizePen, pathPen}) {
  42. const afterSize = size.padEnd(maxLength + 2, ' ');
  43. console.log(sizePen(`Size: ${afterSize}`), pathPen(path));
  44. });
  45. if (overstep) {
  46. const varColor = '#177cb0';
  47. const pathColor = '#f00056';
  48. console.log();
  49. console.log();
  50. console.log(
  51. chalk.grey(
  52. `Some chunks are larger ${
  53. MAX_CHUNK_SIZE / 1024
  54. }KB after compilation. Consider:`,
  55. ),
  56. );
  57. console.log();
  58. console.log(
  59. chalk.hex(pathColor)(
  60. '1.Using dynamic import() to code-split the application',
  61. ),
  62. );
  63. console.log(
  64. chalk.hex(pathColor)(
  65. '2.Adjust the prompt size by adjusting' +
  66. ` ${chalk.hex(varColor)('SWT_MAX_CHUNK_SIZE')}` +
  67. ` in ${chalk.white('packages/app/.env')}`,
  68. ),
  69. );
  70. console.log(
  71. chalk.hex(pathColor)(
  72. `3.Modify ${chalk.hex(varColor)('splitChunks')} in ` +
  73. `${chalk.white('packages/webpack/config/optimization.js')}`,
  74. ),
  75. );
  76. }
  77. }
  78. try {
  79. const compiler = webpack(config);
  80. fs.emptyDirSync(outputPath);
  81. compiler.run(function (err, stats) {
  82. if (err) {
  83. errorLogger('Failed to compile');
  84. console.log();
  85. console.log(err.message);
  86. console.log();
  87. process.exit(1);
  88. }
  89. const {assets, startTime, endTime} = stats.compilation;
  90. const time = endTime - startTime;
  91. clearConsole();
  92. console.log();
  93. console.log(chalk.green('Successfully 🤩'));
  94. console.log(
  95. chalk.gray(`Compiled successfully in ${(time / 1000).toFixed(2)}s`),
  96. );
  97. console.log();
  98. printAssets(assets);
  99. console.log();
  100. });
  101. } catch (error) {
  102. errorLogger('Failed to compile');
  103. console.log();
  104. console.log(error.message);
  105. console.log();
  106. process.exit(1);
  107. }