-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (54 loc) · 1.51 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
const getFlatDependenciesTree = require('./dependenciesTree');
const calculateModulesSizes = require('./moduleSize');
const findInDirectory = require('./find');
const {
printDepInfo,
printHelp
} = require('./view');
const countImports = () => findInDirectory(/require\([\`\'\"].[^\.\/]..*\)/i, process.cwd())
.map(l => {
const m = l.match(/require\([\`\'\"]{1}([^\`\'\"\/]*)/i);
return m && m[1];
}).reduce((result, name) => {
const currentValue = (result[name] || 0) + 1;
return {
...result,
[name]: currentValue
};
}, {});
module.exports = () => {
let sizeType;
switch (process.argv[2]) {
case 'fast':
case undefined:
sizeType = 'ownSize';
break;
case 'slow':
sizeType = 'sharedSize';
break;
case 'full':
sizeType = 'fullSize';
break;
default:
return printHelp();
}
return calculateModulesSizes(getFlatDependenciesTree()).then((treeWithSizes) => {
const numberOfImports = countImports();
const maxSize = Math.max(...treeWithSizes.map(a => a[sizeType]));
const result = treeWithSizes.map(a => {
const imports = numberOfImports[a.name] || 0;
const size = a[sizeType];
const weight = imports ? ((size / imports) / maxSize) : 1;
return {
name: a.name,
sharedSize: a.sharedSize,
fullSize: a.fullSize,
ownSize: a.ownSize,
imports,
weight
};
});
result.sort((a, b) => b.weight - a.weight);
result.forEach(printDepInfo);
});
};