forked from sindresorhus/gulp-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (86 loc) · 2.65 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
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
'use strict';
var gutil = require('gulp-util'),
through = require('through2'),
chalk = require('chalk'),
prettyBytes = require('pretty-bytes');
function log(title, what, sizediff) {
if (typeof sizediff === 'string') {
gutil.log(title + ' ' + (what ? what + ' ' : '') + chalk.magenta(sizediff));
} else {
title = title ? ('\'' + chalk.cyan(title) + '\' ') : '';
var stats = [prettyBytes(sizediff.startSize), ' / ',
prettyBytes(sizediff.endSize), ' / ',
prettyBytes(sizediff.diff), ' / ',
Math.round(sizediff.diffPercent * 100) + '% / ',
sizediff.compressionRatio.toFixed(2)].join('');
gutil.log(title + ' ' + (what ? what + ' ' : '') + chalk.magenta(stats));
}
}
function sizediff() {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-sizediff', 'Streaming not supported'));
return;
}
file.sizediff = {
startSize: file.contents && file.contents.length || file.stats && file.stats.size || 0
};
cb(null, file)
}, function(cb) {
cb()
});
}
sizediff.start = sizediff;
sizediff.stop = function (options) {
if (typeof options === 'undefined') {
options = {};
}
var totalSize = {
start: 0,
end: 0
},
fileCount = 0;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-sizediff', 'Streaming not supported'));
return;
}
file.sizediff.endSize = file.contents && file.contents.length || file.stats && file.stats.size || 0;
var finish = function (err, sizediff) {
totalSize.start += sizediff.startSize;
totalSize.end += sizediff.endSize;
if (options.showFiles === true && sizediff.endSize > 0) {
sizediff.diff = sizediff.startSize - sizediff.endSize;
sizediff.diffPercent = sizediff.endSize / sizediff.startSize;
sizediff.compressionRatio = sizediff.diff / sizediff.startSize;
log(options.title, chalk.blue(file.relative), typeof options.formatFn === 'function' ? options.formatFn(sizediff) : sizediff);
}
fileCount++;
cb(null, file);
};
finish(null, file.sizediff);
}, function (cb) {
var sizediff = {
startSize: totalSize.start,
endSize: totalSize.end,
diff: totalSize.start - totalSize.end,
diffPercent: totalSize.end / totalSize.start,
compressionRatio: (totalSize.start - totalSize.end) / totalSize.start
};
if (fileCount === 1 && options.showFiles === true && totalSize > 0) {
cb();
return;
}
log(options.title, '', typeof options.formatFn === 'function' ? options.formatFn(sizediff) : sizediff);
cb();
});
};
module.exports = sizediff;