This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (108 loc) · 3.88 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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const buf = ' ';
const tab = ' ';
const borderX = `${Array(30).join('-')}\n`;
const formatBytes = (bytes) => {
if (bytes === 0) return '0 Byte'
let k = 1000;
let dm = 3;
let sizes = ['Bytes', 'KB', 'MB', 'GB'];
let i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
};
const analyze = (bundle, opts = {}, format = false) => {
let { root, limit, filter } = opts;
let deps = {};
let entrySize;
let bundleSize = 0;
let bundleModules = bundle.modules;
return new Promise((resolve, reject) => {
let modules = bundleModules.map((m, i) => {
let { id, originalLength: origSize, renderedLength, isEntry, code } = m;
id = id.replace(root, '');
let size = renderedLength;
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
if (isEntry) entrySize = size;
bundleSize += size;
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
if (typeof filter === 'string' && !id.match(filter)) return null
m.dependencies.forEach((d) => {
d = d.replace(root, '');
deps[d] = deps[d] || [];
deps[d].push(id);
});
return {id, size, origSize, isEntry}
}).filter((m) => m);
if (entrySize) bundleSize = entrySize;
modules.sort((a, b) => b.size - a.size);
if (limit || limit === 0) modules = modules.slice(0, limit);
modules.forEach((m) => {
m.dependents = deps[m.id] || [];
m.percent = ((m.size / bundleSize) * 100).toFixed(2);
m.reduction = 100 - ((m.size / m.origSize) * 100).toFixed(2);
});
if (!format) return resolve(modules)
let heading = `Rollup File Analysis\n`;
let displaySize = `${borderX}bundle size: ${formatBytes(bundleSize)}\n`;
let formatted = `${borderX}${heading}${displaySize}${borderX}`;
modules.forEach((m) => {
if (m.isEntry) return
formatted += `file:${buf}${m.id}\n`;
formatted += `size:${buf}${formatBytes(m.size)}\n`;
formatted += `percent:${buf}${m.percent}%\n`;
formatted += `orig. size:${buf}${formatBytes(m.origSize || 'unknown')}\n`;
formatted += `code reduction:${buf}${m.reduction}%\n`;
formatted += `dependents:${buf}${m.dependents.length}\n`;
m.dependents.forEach((d) => {
formatted += `${tab}-${buf}${d.replace(root, '')}\n`;
});
formatted += `${borderX}`;
});
return resolve(formatted)
})
};
const formatted = (bndl, opts) => analyze(bndl, opts, true);
const plugin = (opts = {}) => {
let cb = opts.writeTo || (opts.stdout ? console.log : console.error);
if (opts.onAnalysis) cb = opts.onAnalysis;
let written;
let modules;
let runAnalysis = (out, bundle, isWrite) => new Promise((resolve, reject) => {
resolve();
if (written) return
written = true;
if (out.bundle) bundle = out.bundle;
if (!Array.isArray(bundle.modules)) {
modules.forEach((m) => {
let bm = bundle.modules[m.id];
bm.id = bm.id || m.id;
bm.isEntry = bm.isEntry || m.isEntry;
bm.dependencies = m.dependencies || [];
});
modules = Object.keys(bundle.modules).map((k) => bundle.modules[k]);
} else {
modules = bundle.modules;
}
return analyze({modules}, opts, !opts.onAnalysis).then(cb)
});
return {
name: 'rollup-analyzer-plugin',
transformChunk: (_a, _b, chunk) => new Promise((resolve, reject) => {
resolve(null);
if (!chunk || !chunk.orderedModules) return
modules = chunk.orderedModules.map((m) => {
return {
id: m.id,
isEntry: m.isEntryPoint,
dependencies: m.dependencies.map((d) => d.id)
}
});
}),
generateBundle: runAnalysis,
ongenerate: runAnalysis
}
};
exports.analyze = analyze;
exports.formatted = formatted;
exports.plugin = plugin;