|
| 1 | +const { default: chalk } = require('chalk'); |
| 2 | +const Table = require('cli-table3'); |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const glob = require('glob'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const buildFixture = require('./utils/buildFixture'); |
| 8 | +const { hrToSeconds } = require('./utils/helpers'); |
| 9 | +const prepareFixture = require('./utils/prepareFixture'); |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {{ verbose: boolean }} options |
| 13 | + */ |
| 14 | +module.exports = async function build(options) { |
| 15 | + const { verbose } = options; |
| 16 | + |
| 17 | + const startTime = process.hrtime(); |
| 18 | + const artifactsDir = path.resolve(process.cwd(), 'dist', 'bundle-size'); |
| 19 | + |
| 20 | + await fs.remove(artifactsDir); |
| 21 | + |
| 22 | + if (verbose) { |
| 23 | + console.log(`${chalk.blue('[i]')} artifacts dir is cleared`); |
| 24 | + } |
| 25 | + |
| 26 | + const fixtures = glob.sync('bundle-size/*.fixture.js', { |
| 27 | + cwd: process.cwd(), |
| 28 | + }); |
| 29 | + |
| 30 | + if (verbose) { |
| 31 | + console.log(`${chalk.blue('[i]')} Measuring bundle size for ${fixtures.length} fixture(s)...`); |
| 32 | + console.log(fixtures.map(fixture => ` - ${fixture}`).join('\n')); |
| 33 | + } |
| 34 | + |
| 35 | + const preparedFixtures = await Promise.all(fixtures.map(prepareFixture)); |
| 36 | + const measurements = []; |
| 37 | + |
| 38 | + for (const preparedFixture of preparedFixtures) { |
| 39 | + measurements.push(await buildFixture(preparedFixture, verbose)); |
| 40 | + } |
| 41 | + |
| 42 | + measurements.sort((a, b) => a.path.localeCompare(b.path)); |
| 43 | + |
| 44 | + await fs.writeJSON(path.resolve(process.cwd(), 'dist', 'bundle-size', 'bundle-size.json'), measurements); |
| 45 | + |
| 46 | + if (verbose) { |
| 47 | + const table = new Table({ |
| 48 | + head: ['Fixture', 'Minified size', 'GZIP size'], |
| 49 | + }); |
| 50 | + |
| 51 | + measurements.forEach(r => { |
| 52 | + table.push([r.name, r.minifiedSize, r.gzippedSize]); |
| 53 | + }); |
| 54 | + |
| 55 | + console.log(table.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + console.log(`Completed in ${hrToSeconds(process.hrtime(startTime))}`); |
| 59 | +}; |
0 commit comments