Skip to content

Commit

Permalink
feat: Custom reporter support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp authored and tivac committed Jun 17, 2017
1 parent 793565f commit 7505a7e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Add to your rollup build as the last plugin via JS API or Config file.

```js
var rollup = require("rollup"),

buble = require("rollup-plugin-buble"),
sizes = require("rollup-plugin-sizes");

Expand Down Expand Up @@ -88,6 +88,8 @@ export default {

`details` - Set to true to enable file-by-file breakdowns of space usage.

`report` - Customize reporting. See [source code](index.js) for the default reporter.

## A Note on Versioning

This project's version number currently has a "0.x" prefix, indicating that it's a young
Expand Down
67 changes: 36 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
"use strict";

var path = require("path"),

each = require("lodash.foreach"),
sum = require("lodash.sumby"),
parse = require("module-details-from-path"),
filesize = require("filesize");


function defaultReport({ entry, data, totals, total, options }) {
// Sort
totals.sort((a, b) => b.size - a.size);
console.log("%s:", entry);

totals.forEach((item) => {
console.log(
"%s - %s (%s%%)",
item.name,
filesize(item.size),
((item.size / total) * 100).toFixed(2)
);

if(options.details) {
data[item.name]
.sort((a, b) => b.size - a.size)
.forEach((file) => console.log(
"\t%s - %s (%s%%)",
file.path,
filesize(file.size),
((file.size / item.size) * 100).toFixed(2)
));
}
});
}

module.exports = (options) => {
var entry, base;
var entry, base, report;

if(!options) {
options = false;
}

report = (options && options.report) || defaultReport;

return {
name : "rollup-plugin-sizes",

Expand All @@ -27,11 +55,11 @@ module.exports = (options) => {
ongenerate : (details) => {
var data = {},
totals = [],
total = 0;
total = 0;

details.bundle.modules.forEach((module) => {
var parsed;

// Handle rollup-injected helpers
if(module.id.indexOf("\u0000") === 0) {
parsed = {
Expand All @@ -41,7 +69,7 @@ module.exports = (options) => {
};
} else {
parsed = parse(module.id);

if(!parsed) {
parsed = {
name : "app",
Expand All @@ -63,37 +91,14 @@ module.exports = (options) => {
var size = sum(files, "size");

total += size;

totals.push({
name,
size
});
});

// Sort
totals.sort((a, b) => b.size - a.size);

console.log("%s:", entry);

totals.forEach((item) => {
console.log(
"%s - %s (%s%%)",
item.name,
filesize(item.size),
((item.size / total) * 100).toFixed(2)
);

if(options.details) {
data[item.name]
.sort((a, b) => b.size - a.size)
.forEach((file) => console.log(
"\t%s - %s (%s%%)",
file.path,
filesize(file.size),
((file.size / item.size) * 100).toFixed(2)
));
}
});
report({ entry, data, totals, total, options });
}
};
};

0 comments on commit 7505a7e

Please sign in to comment.