build.js 3.1 KB

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