-
Notifications
You must be signed in to change notification settings - Fork 4
/
calorimetre.js
39 lines (38 loc) · 1.28 KB
/
calorimetre.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
// a quick and dirty tool to know what is taking up space in a bundle
// this should be made generic
var exec = require('child_process').exec
, chalk = require('chalk')
, entryFile = 'src/index.js'
;
exec(`browserify --deps ${entryFile}`, function (err, stdout, stderr) {
var files = {}
, incBy = {}
, deps = JSON.parse(stdout)
;
deps.forEach(function (dep) {
files[dep.file] = {
size: dep.source.replace(/^\/\/# sourceMap.*/m, '').length
, entry: !!dep.entry
};
for (var k in dep.deps) {
var includes = dep.deps[k];
if (!incBy[includes]) incBy[includes] = {};
incBy[includes][dep.file] = true;
}
});
var keys = Object.keys(files)
.sort(function (a, b) {
if (files[a].entry) return -1;
if (files[b].entry) return 1;
if (files[a].size > files[b].size) return -1;
if (files[a].size < files[b].size) return 1;
return 0;
})
;
keys.forEach(function (k) {
console.log(chalk.bold.red(k + (files[k].entry ? ' [entry]' : '')));
console.log(chalk.green(' chars: ') + files[k].size);
console.log(chalk.green(' included by:'));
for (var inc in incBy[k]) console.log(' ' + inc);
});
});