Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for also displaying Brotli compressed size #47

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 74 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,40 @@ 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 => {
module.exports = (options = {}) => {
options = {
pretty: true,
showTotal: true,
uncompressed: options.uncompressed || !(options.gzip || options.brotli),
...options
};

let totalSize = 0;
let fileCount = 0;
const totalSize = new Map();
const desc = {uncompressed: '', gzip: ' (gzipped)', brotli: (' (brotli)')};
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved

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 sizeStrings = [...sizes].map(([key, size]) => {
size = options.pretty ? prettyBytes(size) : (size + ' B');
return chalk.magenta(size) + chalk.gray(desc[key]);
});

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

function hasSize(sizes) {
return [...sizes.values()].some(size => size > 0);
}

function promisify(stream, property, event = 'end') {
return new Promise((resolve, reject) => {
stream.on(event, () => resolve(stream[property]));
stream.on('error', error => reject(error));
});
}
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved

return through.obj((file, encoding, callback) => {
Expand All @@ -30,56 +48,77 @@ module.exports = options => {
return;
}

const finish = (error, size) => {
const finish = (error, sizes) => {
if (error) {
callback(new PluginError('gulp-size', error));
return;
}

totalSize += size;
sizes.forEach((size, key) => totalSize.set(key, size + (totalSize.get(key) || 0)));
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved

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

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

const calc = [];
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved
const names = [];
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved

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

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.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) {
calc.push(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 {
const res = await Promise.all(calc);
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved
// Name each result
const namedResult = new Map();
for (const [idx, size] of res.entries()) {
jonatanlinden marked this conversation as resolved.
Show resolved Hide resolved
namedResult.set(names[idx], size);
}

finish(null, namedResult);
} catch (error) {
finish(error);
}
})();
}, function (callback) {
this.size = totalSize.values().next().value;
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