UNPKG

1.06 kBJavaScriptView Raw
1'use strict';
2const { Transform } = require('stream');
3const filesize = require('filesize');
4const gzipSize = require('gzip-size');
5const log = require('fancy-log');
6
7module.exports = (options = {}, callback) => {
8 function formatSize(size) {
9 if (options.bytes) {
10 return size += ' B';
11 } else {
12 return filesize(size, options);
13 }
14 }
15
16 function getSize(file) {
17 const info = {
18 filename: file.relative,
19 toString: () => info.sizeString
20 };
21
22 info.size = info.sizeString = formatSize(file.contents.length);
23 info.gzip = formatSize(gzipSize.sync(file.contents));
24
25 if (options.gzip) {
26 info.sizeString += ` (gzipped: ${info.gzip})`;
27 }
28
29 if (callback instanceof Function) {
30 callback(info);
31 } else {
32 log(`${file.relative}: ${info}`);
33 }
34 }
35
36 return new Transform({
37 objectMode: true,
38 transform: function(file, encoding, transformCallback) {
39 if (file.isStream() || file.isBuffer()) {
40 getSize(file);
41 }
42
43 return transformCallback(null, file);
44 }
45 });
46};