-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbundle-size-report.js
executable file
·137 lines (116 loc) · 3.51 KB
/
bundle-size-report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env node
import { join } from 'path'
import { table } from 'table'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import {filesize} from 'filesize'
import fs from 'fs'
const __dirname = dirname(fileURLToPath(import.meta.url))
// ensure that K and B values line up vertically
const filesizeConfig = {symbols: {KB: 'K'}}
const prettySize = bytes => filesize(bytes, filesizeConfig)
function getBundles(path) {
const meta = JSON.parse(fs.readFileSync(join(path, './dist/meta.json')))
let metaBundles = Object.values(meta.bundles)
// filter out support bundles, since they don't generate CSS
metaBundles = metaBundles.filter(bundle => !isSupportBundleName(bundle.name))
const bundles = {}
for (const bundle of metaBundles) {
const stats = JSON.parse(fs.readFileSync(join(path, `./${bundle.stats}`)))
const entry = {
name: bundle.name,
path: bundle.css,
stats: stats
}
bundles[bundle.name] = entry
}
return bundles
}
const tableOptions = {
singleLine: true,
border: {
topBody: '',
topJoin: '',
topLeft: '',
topRight: '',
bottomBody: '',
bottomJoin: '',
bottomLeft: '',
bottomRight: '',
bodyLeft: '|',
bodyRight: '|',
bodyJoin: '|',
joinBody: '',
joinLeft: '',
joinRight: '',
joinJoin: ''
}
}
// const sortByName = (a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)
const sortByGZipSize = (a, b) => b[3] - a[3]
// const sortByRawSize = (a, b) => b[5] - a[5]
const posNeg = v => (v > 0 ? '+ ' : v < 0 ? '- ' : '')
;(async () => {
const currentBundles = getBundles(join(__dirname, '../'))
const latestBundles = getBundles(join(__dirname, '../tmp/node_modules/@primer/css/'))
let data = []
// Build the rows
for (const name of Object.keys(currentBundles)) {
const {
stats: {
selectors: {
total: current_selectors_total
},
gzipSize: current_gzip_size,
size: current_size
}
} = currentBundles[name] || { stats: { selectors: { total: 0 }, gzipSize: 0, size: 0 } }
const {
stats: {
selectors: {
total: latest_selectors_total
},
gzipSize: latest_gzip_size,
size: latest_size
}
} = latestBundles[name] || { stats: { selectors: { total: 0 }, gzipSize: 0, size: 0 } }
const delta = [
current_selectors_total - latest_selectors_total,
current_gzip_size - latest_gzip_size,
current_size - latest_size
].reduce((a, b) => a + b, 0)
if (delta !== 0) {
data.push([
name,
current_selectors_total,
current_selectors_total - latest_selectors_total,
current_gzip_size,
current_gzip_size - latest_gzip_size,
current_size,
current_size - latest_size
])
}
}
// Sort the data
data = data.sort(sortByGZipSize)
// Beautify the data
data = data.map(row => {
console.log(row)
row[2] = posNeg(row[2]) + `${row[2]}`.replace('-', '')
row[3] = prettySize(row[3])
row[4] = posNeg(row[4]) + `${prettySize(row[4])}`.replace('-', '')
row[5] = prettySize(row[5])
row[6] = posNeg(row[6]) + `${prettySize(row[6])}`.replace('-', '')
return row
})
// Adding header
data = [
['name', 'selectors', '±', 'gzip size', '±', 'raw size', '±'],
[':-', '-:', '-:', '-:', '-:', '-:', '-:']
].concat(data)
console.log(table(data, tableOptions))
})()
function isSupportBundleName(bundleName) {
// "support", "marketing-support", and any future ones?
return bundleName.endsWith('support')
}