-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
77 lines (61 loc) · 2.04 KB
/
index.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
import fancyLog from 'fancy-log';
import chalk from 'chalk';
import prettyBytes from 'pretty-bytes';
import {gzipSize} from 'gzip-size';
import brotliSize from 'brotli-size';
import {gulpPlugin} from 'gulp-plugin-extras';
const hasSize = sizes => [...sizes.values()].some(size => size > 0);
export default function gulpSize(options = {}) {
options = {
pretty: true,
showTotal: true,
uncompressed: options.uncompressed || !(options.gzip || options.brotli),
...options,
};
let fileCount = 0;
const totalSize = new Map();
const description = new Map([
['uncompressed', ''],
['gzip', ' (gzipped)'],
['brotli', ' (brotli)'],
]);
const log = (what, sizes) => {
let {title} = options;
title = title ? chalk.cyan(title) + ' ' : '';
const sizeStrings = [...sizes].map(([key, size]) => {
size = options.pretty ? prettyBytes(size) : (size + ' B');
return chalk.magenta(size) + chalk.gray(description.get(key));
});
fancyLog(title + what + ' ' + sizeStrings.join(chalk.magenta(', ')));
};
return gulpPlugin('gulp-size', async file => {
const selectedSizes = new Map();
if (options.uncompressed) {
selectedSizes.set('uncompressed', file.contents.length);
}
if (options.gzip) {
selectedSizes.set('gzip', gzipSize(file.contents));
}
if (options.brotli) {
selectedSizes.set('brotli', brotliSize.default(file.contents));
}
let sizes = await Promise.all([...selectedSizes].map(async ([key, size]) => [key, await size]));
sizes = new Map(sizes);
for (const [key, size] of sizes) {
totalSize.set(key, size + (totalSize.get(key) ?? 0));
}
if (options.showFiles === true && hasSize(sizes)) {
log(chalk.blue(file.relative), sizes);
}
fileCount++;
return file;
}, {
async * onFinish(stream) { // eslint-disable-line require-yield
stream.size = totalSize.values().next().value ?? 0;
stream.prettySize = prettyBytes(stream.size);
if (!(fileCount === 1 && options.showFiles) && hasSize(totalSize) && fileCount > 0 && options.showTotal) {
log(chalk.green('all files'), totalSize);
}
},
});
}