/
loader-stats.js
executable file
·84 lines (63 loc) · 2.16 KB
/
loader-stats.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
#!/usr/bin/env node
const path = require( 'path' );
const stats = require( path.join( __dirname, '..', 'server', 'bundler', 'assets.json' ) );
const _ = require( 'lodash' );
const gzipSize = require( 'gzip-size' );
function getChunkByName( name ) {
return stats.chunks.find( chunk => chunk.names.indexOf( name ) !== -1 );
}
function getChunkById( id ) {
return stats.chunks.find( chunk => chunk.id === id );
}
function getChunkAndSiblings( which ) {
const mainChunk = getChunkByName( which );
return [
mainChunk,
...mainChunk.siblings.map( getChunkById )
];
}
const sectionsToCheck = process.argv.slice(2);
const sectionChunks = _.compact( sectionsToCheck.map( getChunkByName ) );
if ( sectionChunks.length !== sectionsToCheck.length ) {
console.log( `bad section chunk name` );
}
const sectionsToLoad = [];
sectionsToLoad.push({
name: 'boot',
chunks: getChunkAndSiblings('build')
});
sectionChunks.forEach(section => {
sectionsToLoad.push({
name: section.names.join(','),
chunks: _.difference(getChunkAndSiblings(section.names[0]), _.flatMap(sectionsToLoad, section => section.chunks))
});
});
filesToLoadPerSection = sectionsToLoad.map(section => {
return {
name: section.name,
filesToLoad: _.flatMap(section.chunks, chunk => {
return chunk.files.map(file => path.join(__dirname, '..', 'public', file.replace('/calypso/', '')));
})
}
});
async function calculateSizes( section ) {
const fileSizePromises = section.filesToLoad.map( f => gzipSize.file( f ) );
const fileSizes = await Promise.all( fileSizePromises );
const filesWithSizes = _.zipObject( section.filesToLoad, fileSizes );
console.log( `${section.name}:` );
section.filesToLoad.forEach( f => {
console.log( ` ${f}: (${ filesWithSizes[ f ] / 1000 }kb)`);
})
const totalSize = section.filesToLoad.reduce((totalSize, f) => totalSize + filesWithSizes[f], 0);
console.log("\t" + ( totalSize / 1000) + "kb");
console.log('');
return totalSize;
}
async function go() {
let totalSize = 0;
for (section in filesToLoadPerSection) {
totalSize += await calculateSizes(filesToLoadPerSection[ section ]);
}
console.log("Total Load: " + (totalSize / 1000) + "kb");
}
go().catch( console.error );