1 | 'use strict';
|
2 | var gutil = require('gulp-util');
|
3 | var through = require('through2');
|
4 | var chalk = require('chalk');
|
5 | var prettyBytes = require('pretty-bytes');
|
6 | var gzipSize = require('gzip-size');
|
7 |
|
8 | function log(title, what, size, gzip) {
|
9 | gutil.log('gulp-size: ' + title + what + ' ' + prettyBytes(size) +
|
10 | (gzip ? chalk.gray(' (gzipped)') : ''));
|
11 | };
|
12 |
|
13 | module.exports = function (options) {
|
14 | options = options || {};
|
15 |
|
16 | var totalSize = 0;
|
17 | var fileCount = 0;
|
18 | var title = options.title ? options.title + ' ' : '';
|
19 |
|
20 | return through.obj(function (file, enc, cb) {
|
21 | if (file.isNull()) {
|
22 | this.push(file);
|
23 | return cb();
|
24 | }
|
25 |
|
26 | if (file.isStream()) {
|
27 | this.emit('error', new gutil.PluginError('gulp-size', 'Streaming not supported'));
|
28 | return cb();
|
29 | }
|
30 |
|
31 | var finish = function (err, size) {
|
32 | totalSize += size;
|
33 |
|
34 | if (options.showFiles === true && size > 0) {
|
35 | log(title, chalk.blue(file.relative), size, options.gzip);
|
36 | }
|
37 |
|
38 | fileCount++;
|
39 | this.push(file);
|
40 | cb();
|
41 | }.bind(this);
|
42 |
|
43 | if (options.gzip) {
|
44 | gzipSize(file.contents, finish);
|
45 | } else {
|
46 | finish(null, file.contents.length);
|
47 | }
|
48 | }, function (cb) {
|
49 | if (fileCount === 1 && options.showFiles === true && totalSize > 0) {
|
50 | return cb();
|
51 | }
|
52 |
|
53 | log(title, chalk.green('total'), totalSize, options.gzip);
|
54 | cb();
|
55 | });
|
56 | };
|