You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reportBuildStats.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* eslint-disable no-console*/
  21. const chalk = require('chalk');
  22. const sortBy = require('lodash/sortBy');
  23. function formatSize(bytes) {
  24. if (bytes === 0) {
  25. return '0';
  26. }
  27. const k = 1000; // or 1024 for binary
  28. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  29. const i = Math.floor(Math.log(bytes) / Math.log(k));
  30. return parseFloat((bytes / Math.pow(k, i)).toFixed(0)) + ' ' + sizes[i];
  31. }
  32. module.exports = (stats, bundleName = '', filesLimit = 10) => {
  33. if (stats.compilation.errors && stats.compilation.errors.length) {
  34. console.log(chalk.red.bold('Failed to create a production build!'));
  35. stats.compilation.errors.forEach(err => console.log(chalk.red(err.message || err)));
  36. process.exit(1);
  37. }
  38. const jsonStats = stats.toJson();
  39. const onlyJS = jsonStats.assets.filter(asset => asset.name.endsWith('.js'));
  40. console.log(`Biggest js chunks (${onlyJS.length} total) ${bundleName && `[${bundleName}]`}:`);
  41. sortBy(onlyJS, asset => -asset.size)
  42. .slice(0, filesLimit)
  43. .forEach(asset => {
  44. let sizeLabel = formatSize(asset.size);
  45. const leftPadding = ' '.repeat(Math.max(0, 8 - sizeLabel.length));
  46. sizeLabel = leftPadding + sizeLabel;
  47. console.log('', chalk.yellow(sizeLabel), asset.name);
  48. });
  49. console.log();
  50. const seconds = jsonStats.time / 1000;
  51. console.log('Duration: ' + seconds.toFixed(2) + 's');
  52. console.log();
  53. };