-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
133 lines (107 loc) · 3.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable no-console */
"use strict";
const path = require("path");
const parse = require("module-details-from-path");
const filesize = require("filesize");
function defaultReport(details) {
const args = Object.assign({}, details);
// Sort
args.totals.sort((a, b) => b.size - a.size);
console.log("%s:", args.input);
args.totals.forEach((item) => {
const itemSize = item.size || 0;
console.log(
"%s - %s (%s%%)",
item.name,
filesize(itemSize),
((itemSize / args.total) * 100).toFixed(2)
);
if(args.options.details) {
args.data[item.name]
.sort((a, b) => b.size - a.size)
.forEach((file) => console.log(
"\t%s - %s (%s%%)",
file.path,
filesize(file.size || 0),
((file.size / itemSize) * 100).toFixed(2)
));
}
});
}
module.exports = (options) => {
let input;
let bases;
if(!options) {
options = false;
}
const report = (options && options.report) || defaultReport;
return {
name : "rollup-plugin-sizes",
// Grab some needed bits out of the options
options : (config) => {
const { input : original } = config;
if(Array.isArray(original)) {
input = original;
} else if(typeof original === "object") {
input = Object.values(original);
} else {
input = [ original ];
}
bases = input.map((file) => path.dirname(file));
},
// Spit out stats during bundle generation
generateBundle : (_, bundles) => {
Object.values(bundles).forEach((bundle, idx) => {
if(!bundle.modules) {
return;
}
const base = bases[idx] || "";
let total = 0;
const data = {};
const totals = [];
const ids = Object.keys(bundle.modules);
ids.forEach((id) => {
const module = bundle.modules[id];
let parsed;
// Handle rollup-injected helpers
if(id.indexOf("\u0000") === 0) {
parsed = {
name : "rollup helpers",
basedir : "",
path : id.replace("\u0000", ""),
};
} else {
parsed = parse(id);
if(!parsed) {
parsed = {
name : "app",
basedir : base,
path : path.relative(base, id),
};
}
}
if(!(parsed.name in data)) {
data[parsed.name] = [];
}
data[parsed.name].push(Object.assign(parsed, { size : module.originalLength }));
});
// Sum all files in each chunk
Object.entries(data).forEach(([ name, files ]) => {
const sum = files.reduce((out, { size }) => out + size, 0);
total += sum;
totals.push({
name,
size : sum,
});
});
report({
input : input[idx] || bundle.fileName,
data,
totals,
total,
options,
});
});
},
};
};