Skip to content

Commit

Permalink
Add support for displaying the brotli compressed size.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanlinden committed Apr 20, 2021
1 parent 84c7724 commit 670b04c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 34 deletions.
110 changes: 78 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,50 @@ const chalk = require('chalk');
const prettyBytes = require('pretty-bytes');
const StreamCounter = require('stream-counter');
const gzipSize = require('gzip-size');
const brotliSize = require('brotli-size');

module.exports = options => {
options = {
pretty: true,
showTotal: true,
uncompressed: !options || options.uncompressed || !(options.gzip || options.brotli),
...options
};

let totalSize = 0;
let fileCount = 0;
const totalSize = {};

function log(what, size) {
function log(what, sizes) {
let {title} = options;
title = title ? chalk.cyan(title) + ' ' : '';
size = options.pretty ? prettyBytes(size) : (size + ' B');
fancyLog(title + what + ' ' + chalk.magenta(size) + (options.gzip ? chalk.gray(' (gzipped)') : ''));
const desc = {uncompressed: '', gzip: ' (gzipped)', brotli: (' (brotli)')};
const strings = Object.entries(sizes).map(([k, v]) => {
const size = options.pretty ? prettyBytes(v) : (v + ' B');
return chalk.magenta(size) + chalk.gray(desc[k]);
});

fancyLog(title + what + ' ' + strings.join(chalk.magenta(', ')));
}

function addPropWise(a, b) {
for (const k in b) {
if (k in b) {
a[k] = (a[k] + b[k]) || b[k];
}
}

return a;
}

function hasSize(sizes) {
return Object.values(sizes).some(a => a > 0);
}

function promisify(stream, property, event = 'run') {
return new Promise((resolve, reject) => {
stream.on(event, () => resolve(stream[property]));
stream.on('error', error => reject(error));
});
}

return through.obj((file, encoding, callback) => {
Expand All @@ -36,50 +64,68 @@ module.exports = options => {
return;
}

totalSize += size;
addPropWise(totalSize, size);

if (options.showFiles === true && size > 0) {
if (options.showFiles === true && hasSize(size)) {
log(chalk.blue(file.relative), size);
}

fileCount++;
callback(null, file);
};

const calc = [];
const names = [];

if (file.isStream()) {
if (options.gzip) {
file.contents.pipe(gzipSize.stream())
.on('error', finish)
.on('end', function () {
finish(null, this.gzipSize);
});
} else {
file.contents.pipe(new StreamCounter())
.on('error', finish)
.on('finish', function () {
finish(null, this.bytes);
});
calc.push(promisify(file.contents.pipe(gzipSize.stream()), 'gzipSize'));
names.push('gzip');
}

return;
if (options.brotli) {
calc.push(promisify(file.contents.pipe(brotliSize.stream()), 'brotliSize'));
names.push('brotli');
}

if (options.uncompressed) {
calc.push(promisify(file.contents.pipe(new StreamCounter()), 'bytes', 'finish'));
names.push('uncompressed');
}
}

if (options.gzip) {
(async () => {
try {
finish(null, await gzipSize(file.contents));
} catch (error) {
finish(error);
}
})();
} else {
finish(null, file.contents.length);
if (file.isBuffer()) {
if (options.uncompressed) {
// Shoehorning, because one size fits all
calc.push(new Promise(resolve => resolve(file.contents.length)));
names.push('uncompressed');
}

if (options.gzip) {
calc.push(gzipSize(file.contents));
names.push('gzip');
}

if (options.brotli) {
calc.push(brotliSize.default(file.contents));
names.push('brotli');
}
}
}, function (callback) {
this.size = totalSize;
this.prettySize = prettyBytes(totalSize);

if (!(fileCount === 1 && options.showFiles) && totalSize > 0 && fileCount > 0 && options.showTotal) {
(async () => {
try {
finish(null, await Promise.all(calc).then(res => {
// Name each result
return res.reduce((acc, cur, idx) => ({...acc, [names[idx]]: cur}), {});
}));
} catch (error) {
finish(error);
}
})();
}, callback => {
this.size = totalSize[Object.keys(totalSize)[0]];
this.prettySize = prettyBytes(this.size);
if (!(fileCount === 1 && options.showFiles) && hasSize(totalSize) && fileCount > 0 && options.showTotal) {
log(chalk.green('all files'), totalSize);
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"measure",
"inspect",
"debug",
"gzip"
"gzip",
"brotli"
],
"dependencies": {
"brotli-size": "^4.0.0",
"chalk": "^2.3.0",
"fancy-log": "^1.3.2",
"gzip-size": "^5.1.1",
Expand Down
16 changes: 15 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ Give it a title so it's possible to distinguish the output of multiple instances
Type: `boolean`<br>
Default: `false`

Displays the gzipped size instead.
Displays the gzipped size.

##### brotli

Type: `boolean`<br>
Default: `false`

Displays the brotli compressed size.

##### uncompressed

Type: `boolean`<br>
Default: `false` if either of gzip or brotli is `true`, otherwise `true`

Displays the uncompressed size.

##### pretty

Expand Down

0 comments on commit 670b04c

Please sign in to comment.