forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputeBuildSizes.ts
64 lines (54 loc) · 1.62 KB
/
computeBuildSizes.ts
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
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import glob from 'fast-glob';
export const PACKAGE_SIZES_FILENAME = './packages/sizes.json';
async function getTotalSize(fileGlob: string, cwd: string): Promise<number> {
const files = await glob(fileGlob, { absolute: true, cwd, onlyFiles: true });
const sizes = await Promise.all<number>(
files.map(
(file) =>
new Promise((resolve, reject) => {
fs.stat(file, (error, stats) => {
if (error) {
reject(error);
} else {
resolve(stats.size);
}
});
}),
),
);
return sizes.reduce((sum, size) => sum + size, 0);
}
async function computeBuildSizes() {
const packages = await glob('./packages/*', { absolute: true, onlyDirectories: true });
const stats: { name: string; sizes: object }[] = [];
await Promise.all(
packages.map(async (packagePath) => {
const packageName = path.basename(packagePath);
stats.push({
name: packageName,
sizes: {
esm: await getTotalSize('./esm/**/*.js', packagePath),
lib: await getTotalSize('./lib/**/*.js', packagePath),
},
});
}),
);
// Sort so its deterministic
stats.sort((a, b) => a.name.localeCompare(b.name));
// Convert to an object
const sizes = stats.reduce(
(obj, stat) => ({
...obj,
[stat.name]: stat.sizes,
}),
{},
);
fs.writeFileSync(PACKAGE_SIZES_FILENAME, JSON.stringify(sizes), 'utf8');
}
computeBuildSizes().catch((error) => {
console.error(chalk.red(error.message));
process.exitCode = 1;
});