-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
237 lines (207 loc) · 7.95 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env node
const chalk = require('chalk');
const program = require('commander');
const fs = require('fs');
const path = require('path');
var oldPath;
var newPath;
var cutoffDelta;
function run() {
program
.arguments('<baseline> <current>')
.option('-c, --cutoff <cutoff>', 'absolute cutoff delta to print report')
.action(function(baseline, current, cutoff) {
oldPath = path.resolve(process.cwd(), baseline);
newPath = path.resolve(process.cwd(), current);
cutoffDelta = program.cutoff || 0;
_compare();
process.exit(0);
})
.parse(process.argv);
_validateInputs();
}
function _compare() {
let oldJson = require(oldPath);
let newJson = require(newPath);
let noChange = [];
let increased = [];
let decreased = [];
let onlyOld = [];
let onlyNew = [];
let assets = _compareAssets(oldJson, newJson);
for (let assetName in assets) {
let asset = assets[assetName];
if (asset.oldSize === asset.newSize) {
noChange.push(asset);
} else if (asset.oldSize < asset.newSize) {
increased.push(asset);
} else if (asset.oldSize > asset.newSize) {
decreased.push(asset);
} else if (asset.oldSize && !asset.newSize) {
onlyOld.push(asset);
} else if (!asset.oldSize && asset.newSize) {
onlyNew.push(asset);
}
};
_printReport(noChange, increased, decreased, onlyOld, onlyNew);
}
function _compareAssets(oldJson, newJson) {
let assets = {};
let modules = {};
let oldChunks = _createChunksDictionary(oldJson.chunks);
let newChunks = _createChunksDictionary(newJson.chunks);
let assetNames = _createAssetNamesDictionary(oldJson, newJson);
oldJson.assets.forEach((asset) => {
let assetName = assetNames[asset.name] || asset.name;
assets[assetName] = {
name: assetName,
oldSize: asset.size,
modules: _getModulesForAsset(modules, oldChunks, asset, false /* isNew */)
};
});
newJson.assets.forEach((asset) => {
let assetName = assetNames[asset.name] || asset.name;
if (assets[assetName]) {
assets[assetName].newSize = asset.size;
assets[assetName].modules = _getModulesForAsset(modules, newChunks, asset, true /* isNew */);
} else {
assets[assetName] = {
name: assetName,
newSize: asset.size,
modules: _getModulesForAsset(modules, newChunks, asset, true /* isNew */)
};
}
});
return assets;
}
function _getModulesForAsset(modulesDict, chunks, asset, isNew) {
let modules = [];
asset.chunks.forEach((chunkId) => {
let chunk = chunks[chunkId];
chunk.modules.forEach((module) => {
let moduleName = module.name.substr(module.name.lastIndexOf('/') + 1) + '_' + module.depth;
if (!modulesDict[moduleName]) {
modulesDict[moduleName] = {
name: moduleName,
identifier: module.identifier,
oldSize: 0,
newSize: 0
}
}
if (isNew) {
modulesDict[moduleName].newSize = module.size;
modulesDict[moduleName].newId = module.id;
} else {
modulesDict[moduleName].oldSize = module.size;
modulesDict[moduleName].oldId = module.id;
}
modules.push(modulesDict[moduleName]);
});
});
return modules;
}
function _createChunksDictionary(chunks) {
let dictChunks = {};
chunks.forEach((chunk) => {
dictChunks[chunk.id] = chunk;
});
return dictChunks;
}
function _createAssetNamesDictionary(oldJson, newJson) {
let names = {};
if (oldJson.assetsByChunkName) {
for (let assetName in oldJson.assetsByChunkName) {
names[oldJson.assetsByChunkName[assetName]] = assetName;
}
}
if (newJson.assetsByChunkName) {
for (let assetName in newJson.assetsByChunkName) {
names[newJson.assetsByChunkName[assetName]] = assetName;
}
}
return names;
}
function _printReport(noChange, increased, decreased, onlyOld, onlyNew) {
const error = chalk.red;
const warning = chalk.yellow;
const success = chalk.green;
console.log(chalk.red(`* ONLY IN NEW: ${onlyNew.length}`));
onlyNew.sort((a, b) => { return b.newSize - a.newSize });
onlyNew.forEach((asset) => {
console.log(` ${asset.name} : ${asset.newSize}`);
});
console.log(chalk.red(`* INCREASED: ${increased.length}`));
increased.sort((a, b) => { return ((b.newSize - b.oldSize) - (a.newSize - a.oldSize)) });
increased.forEach((asset) => {
console.log(` ${asset.name}: ${asset.newSize - asset.oldSize}`);
console.log(` ${_pad("Module")}\tCurrent\tBase\tDiff`);
console.log(` ======================================================`);
let modules = asset.modules.sort((a, b) => { return ((b.newSize - b.oldSize) - (a.newSize - a.oldSize)) });
let sum = 0;
modules.forEach((module) => {
let moduleDelta = module.newSize - module.oldSize;
if (Math.abs(moduleDelta) >= cutoffDelta) {
let moduleName = module.name.substr(0, module.name.lastIndexOf('_'));
let paddedName = _pad(moduleName);
if (moduleDelta > 0) {
console.log(error(` ${paddedName}\t${module.newSize}\t${module.oldSize}\t${moduleDelta}`));
}
if (moduleDelta < 0) {
console.log(success(` ${paddedName}\t${module.newSize}\t${module.oldSize}\t${moduleDelta}`));
}
if (moduleDelta !== 0) {
// parse identifier and print individual file names for composed modules
if (module.identifier.indexOf(' ') !== -1 && moduleName.endsWith(' modules')) {
let files = module.identifier.split(' ');
files.forEach((file) => {
let filePath = file.substr(file.lastIndexOf('!') + 1);
let fileName = filePath.substr(filePath.lastIndexOf('\\') + 1);
console.log(` - ${fileName}`);
})
}
}
}
sum += moduleDelta;
});
console.log(` ======================================================`);
// console.log(` Total: ${sum}`); // TODO: why doesn't the sum add up????
});
console.log(chalk.yellow(`* UNCHANGED: ${noChange.length}`));
if (cutoffDelta === 0) {
noChange.forEach((asset) => {
console.log(` ${asset.name}`);
});
}
console.log(chalk.green(`* DECREASED: ${decreased.length}`));
decreased.sort((a, b) => { return ((b.newSize - b.oldSize) - (a.newSize - a.oldSize)) });
decreased.forEach((asset) => {
let delta = asset.newSize - asset.oldSize;
if (Math.abs(delta) >= cutoffDelta) {
console.log(` ${asset.name} : ${delta}`);
}
});
console.log(chalk.green(`* ONLY IN OLD: ${onlyOld.length}`));
onlyOld.sort((a, b) => { return b.oldSize - a.oldSize });
onlyOld.forEach((asset) => {
console.log(` ${asset.name} : ${asset.oldSize}`);
});
}
function _pad(str) {
let pad = Array(30).join(' ');
return (str + pad).substring(0, pad.length);
}
function _validateInputs() {
_checkPathExists(oldPath);
_checkPathExists(newPath);
}
function _checkPathExists(path) {
if (!path) {
console.log(chalk.red('Error: Missing required parameter: webpack-diff <baseline> <new>'));
process.exit(1);
}
if (!fs.existsSync(path)) {
console.error(chalk.red(`Error: ${path} does not exist.`));
process.exit(1);
}
}
run();