|
| 1 | +const path = require('path'); |
| 2 | +const prettifyTime = require('./prettifyTime'); |
| 3 | +const logger = require('../Logger'); |
| 4 | +const emoji = require('./emoji'); |
| 5 | +const filesize = require('filesize'); |
| 6 | + |
| 7 | +const LARGE_BUNDLE_SIZE = 1024 * 1024; |
| 8 | +const NUM_LARGE_ASSETS = 10; |
| 9 | +const COLUMNS = [ |
| 10 | + {align: 'left'}, // name |
| 11 | + {align: 'right'}, // size |
| 12 | + {align: 'right'} // time |
| 13 | +]; |
| 14 | + |
| 15 | +function bundleReport(mainBundle, detailed = false) { |
| 16 | + // Get a list of bundles sorted by size |
| 17 | + let bundles = Array.from(iterateBundles(mainBundle)).sort( |
| 18 | + (a, b) => b.totalSize - a.totalSize |
| 19 | + ); |
| 20 | + let rows = []; |
| 21 | + |
| 22 | + for (let bundle of bundles) { |
| 23 | + // Add a row for the bundle |
| 24 | + rows.push([ |
| 25 | + formatFilename(bundle.name, logger.chalk.cyan.bold), |
| 26 | + logger.chalk.bold( |
| 27 | + prettifySize(bundle.totalSize, bundle.totalSize > LARGE_BUNDLE_SIZE) |
| 28 | + ), |
| 29 | + logger.chalk.green.bold(prettifyTime(bundle.bundleTime)) |
| 30 | + ]); |
| 31 | + |
| 32 | + // If detailed, generate a list of the top 10 largest assets in the bundle |
| 33 | + if (detailed && bundle.assets.size > 1) { |
| 34 | + let assets = Array.from(bundle.assets) |
| 35 | + .filter(a => a.type === bundle.type) |
| 36 | + .sort((a, b) => b.bundledSize - a.bundledSize); |
| 37 | + |
| 38 | + let largestAssets = assets.slice(0, NUM_LARGE_ASSETS); |
| 39 | + for (let asset of largestAssets) { |
| 40 | + // Add a row for the asset. |
| 41 | + rows.push([ |
| 42 | + (asset == assets[assets.length - 1] ? '└── ' : '├── ') + |
| 43 | + formatFilename(asset.name, logger.chalk.reset), |
| 44 | + logger.chalk.dim(prettifySize(asset.bundledSize)), |
| 45 | + logger.chalk.dim(logger.chalk.green(prettifyTime(asset.buildTime))) |
| 46 | + ]); |
| 47 | + } |
| 48 | + |
| 49 | + // Show how many more assets there are |
| 50 | + if (assets.length > largestAssets.length) { |
| 51 | + rows.push([ |
| 52 | + '└── ' + |
| 53 | + logger.chalk.dim( |
| 54 | + `+ ${assets.length - largestAssets.length} more assets` |
| 55 | + ) |
| 56 | + ]); |
| 57 | + } |
| 58 | + |
| 59 | + // If this isn't the last bundle, add an empty row before the next one |
| 60 | + if (bundle !== bundles[bundles.length - 1]) { |
| 61 | + rows.push([]); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Render table |
| 67 | + logger.log(''); |
| 68 | + logger.table(COLUMNS, rows); |
| 69 | +} |
| 70 | + |
| 71 | +module.exports = bundleReport; |
| 72 | + |
| 73 | +function* iterateBundles(bundle) { |
| 74 | + yield bundle; |
| 75 | + for (let child of bundle.childBundles) { |
| 76 | + yield* iterateBundles(child); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function prettifySize(size, isLarge) { |
| 81 | + let res = filesize(size); |
| 82 | + if (isLarge) { |
| 83 | + res = logger.chalk.yellow(emoji.warning + ' ' + res); |
| 84 | + } else { |
| 85 | + res = logger.chalk.magenta(res); |
| 86 | + } |
| 87 | + |
| 88 | + return res; |
| 89 | +} |
| 90 | + |
| 91 | +function formatFilename(filename, color = logger.chalk.reset) { |
| 92 | + let dir = path.relative(process.cwd(), path.dirname(filename)); |
| 93 | + return ( |
| 94 | + logger.chalk.dim(dir + (dir ? path.sep : '')) + |
| 95 | + color(path.basename(filename)) |
| 96 | + ); |
| 97 | +} |
0 commit comments