Input AND output size
JayBizzle opened this issue · comments
Would showing the input AND output size of files be in the scope of the plugin?
I've hacked together the functionality for my dev machine (never written a plugin before), but it is a bit flakey depending on what pipes the files are run through previously, mainly gulp-ruby-sass
.
This is where i got to. The main problem when passing through gulp-ruby-sass
is that the original file size isn't available
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var prettyBytes = require('pretty-bytes');
module.exports = function (options) {
options = options || {};
var totalSize = 0;
var totalOriginalSize = 0;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-size', 'Streaming not supported'));
return cb();
}
var size = file.contents.length;
totalSize += size;
if(file.stat) {
var originalSize = file.stat.size;
totalOriginalSize += originalSize;
}
if (options.showFiles === true) {
if(file.stat) {
var perc = Math.round(100-(size/originalSize*100)).toFixed(2) + '%';
gutil.log('gulp-size: ' + gutil.colors.blue(file.relative) + ' ' + prettyBytes(originalSize) + ' -> ' + prettyBytes(size) + ' ' + gutil.colors.green(perc));
} else {
gutil.log('gulp-size: ' + gutil.colors.blue(file.relative) + ' ' + prettyBytes(size));
}
}
this.push(file);
cb();
}, function (cb) {
gutil.log('gulp-size: ' + gutil.colors.green('total ') + prettyBytes(totalOriginalSize) + ' -> ' + prettyBytes(totalSize));
cb();
});
Not something i want built-in, but you could put the plugin first in the chain and last to show both before and after, though not the difference.
Actually, see https://github.com/ben-eb/gulp-bytediff
@sindresorhus i tested gulp-bytediff
earlier, but it seems gul-ruby-sass
stops is from working. Not at my dev machine at moment, so can't post the message...will do tomorrow
I've posted the error message on the gulp-bytediff
issue tracker as im not sure which plugin is causing the issue...