/
sizer.js
40 lines (32 loc) · 1.01 KB
/
sizer.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
/*eslint-disable no-console */
const { existsSync, readdirSync } = require('fs');
const { resolve } = require('path');
const chalk = require('chalk');
const { fileSync } = require('gzip-size');
const ps = require('prettysize');
const limit = 1024 * 45;
function getSizes(path, regex = /\.js$/) {
if (!existsSync(path)) {
return 0;
}
return readdirSync(path)
.filter(file => regex.test(file))
.reduce((acc, file) => acc + fileSync(resolve(path, file)), 0);
}
const totalSize = getSizes('./', /^([A-Z]|index).*\.js/) + getSizes('./utils');
console.log('');
console.log(chalk.magenta('>>> LIBRARY SIZE'));
console.log('');
if (!totalSize) {
console.log([chalk.bgRed(' FAIL '), chalk.red('Nothing to measure')].join(' '));
process.exit(1);
}
if (totalSize > limit) {
console.log(
[chalk.bgRed(' FAIL '), chalk.red(`${ps(totalSize)} > ${ps(limit)} (gzip)`)].join(' '),
);
process.exit(1);
}
console.log(
[chalk.bgGreen(' PASS '), chalk.green(`${ps(totalSize)} < ${ps(limit)} (gzip)`)].join(' '),
);