Repository navigation
Expand file tree
/
Copy pathsize.ts
More file actions
52 lines (45 loc) · 1.73 KB
/
Copy pathsize.ts
File metadata and controls
52 lines (45 loc) · 1.73 KB
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
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { gzipSync } from 'node:zlib'
// Size budgets for the web component bundles, in gzipped bytes (1 kB = 1000 bytes, as Vite reports).
// The ecosystem bundle is meant for marketing and product pages:
// keep it small and free of the Seam API client.
const bundles = [
{ file: 'dist/elements.js', maxGzipBytes: null },
{ file: 'dist/ecosystem.js', maxGzipBytes: 80_000 },
] as const
// Strings that only appear when the Seam API client or telemetry is bundled.
const forbiddenInEcosystem = ['connect.getseam.com']
const main = async (): Promise<void> => {
const errors: string[] = []
for (const { file, maxGzipBytes } of bundles) {
const buff = await readFile(fileURLToPath(new URL(file, import.meta.url)))
const gzipBytes = gzipSync(buff).length
const budget =
maxGzipBytes == null ? '' : ` (budget ${formatBytes(maxGzipBytes)})`
// eslint-disable-next-line no-console
console.log(
`${file}: ${formatBytes(buff.length)} raw, ${formatBytes(gzipBytes)} gzip${budget}`
)
if (maxGzipBytes != null && gzipBytes > maxGzipBytes) {
errors.push(
`${file} is ${formatBytes(gzipBytes)} gzip, over the ${formatBytes(maxGzipBytes)} budget`
)
}
if (file === 'dist/ecosystem.js') {
const code = buff.toString()
for (const str of forbiddenInEcosystem) {
if (code.includes(str)) {
errors.push(
`${file} includes '${str}': the Seam API client or telemetry was bundled`
)
}
}
}
}
if (errors.length > 0) {
throw new Error(errors.join('\n'))
}
}
const formatBytes = (bytes: number): string => `${(bytes / 1000).toFixed(2)} kB`
await main()