Open History to see the last commit
1#!/usr/bin/env node
2β
3import fs from 'node:fs'
4import path from 'node:path'
5import { parseArgs } from 'node:util'
6β
7const { values } = parseArgs({
8 allowPositionals: false,
9 options: {
10 current: {
11 type: 'string',
12 default: 'benchmarks/bundle-size/results/current.json',
13 },
14 id: { type: 'string' },
15 'top-sources': { type: 'string', default: '30' },
16 json: { type: 'boolean' },
17 },
18})
19β
20if (!values.id) {
21 throw new Error('Missing required argument: --id')
22}
23β
24const current = JSON.parse(
25 fs.readFileSync(path.resolve(values.current), 'utf8'),
26)
27const metric = (current.metrics || []).find((item) => item.id === values.id)
28β
29if (!metric) {
30 throw new Error(`Unknown bundle-size metric: ${values.id}`)
31}
32β
33if (!metric.sources) {
34 throw new Error(
35 `No source attribution found for ${values.id}. Re-run measure with --analysis.`,
36 )
37}
38β
39const sourceBytes = new Map()
40for (const chunk of metric.sources) {
41 for (const source of chunk.sources || []) {
42 sourceBytes.set(
43 source.source,
44 (sourceBytes.get(source.source) || 0) + source.estimatedBytes,
45 )
46 }
47}
48β
49const rows = [...sourceBytes]
50 .map(([source, estimatedBytes]) => ({ source, estimatedBytes }))
51 .sort((a, b) => b.estimatedBytes - a.estimatedBytes)
52 .slice(0, Number.parseInt(values['top-sources'], 10))
53β
54if (values.json) {
55 process.stdout.write(JSON.stringify(rows, null, 2) + '\n')
56} else {
57 for (const row of rows) {
58 process.stdout.write(`${row.estimatedBytes} ${row.source}\n`)
59 }
60}