-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfReportTemplate.js
125 lines (117 loc) · 3.01 KB
/
perfReportTemplate.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
const HumanReadableSize = size =>
(size >= 1000 || size <= -1000 ? `${Math.round(size / 1e3)}kB` : `${size}B`);
const cSS = `
body, table {
font-family: Helvetica Neue, arial;
font-size: 20px;
}
table {
width: 100%;
text-align: right;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 5px;
}
th {
font-weight: 500;
}
.name {
text-align: left;
}
.smaller {
color: green;
}
.bigger {
color: #e60606;
}
`;
const thead = `
<thead>
<tr>
<th class="name">Bundle file name</th>
<th>Change</th>
<th>Now</th>
<th>Before</th>
</tr>
</thead>
`;
const sizeRow = ({
change, name, newSize, oldSize, type,
}) =>
`
<tr>
<td class="name">${name}</td>
<td class="${type}">${change > 0 ? '+' : ''}${HumanReadableSize(change)}</td>
<td>${HumanReadableSize(newSize)}</td>
<td><del>${HumanReadableSize(oldSize)}</del></td>
</tr>
`;
const perfReportTemplate = ({
branch, fileSizes, getS3Url, repo,
}) => {
const totalChange = fileSizes.reduce((total, fileSize) => total + fileSize.change, 0);
let description;
if (totalChange > 0) {
description = `The total size increased with:
<span class="bigger">+${HumanReadableSize(totalChange)}</span>`;
} else if (totalChange === 0) {
description = 'The total size stayed the same, great job!!! (?)';
} else {
description = `The size decreased with:
<span class="smaller">${HumanReadableSize(totalChange)}</span>, great job!!!`;
}
const typeOrder = ['new', 'bigger', 'smaller', 'deleted'];
const changed = fileSizes
.filter(fileSize => fileSize.type !== 'unchanged')
.sort((a, b) => typeOrder.indexOf(a.type) >= typeOrder.indexOf(b.type));
return `
<style>${cSS}</style>
<h1>Bundle size report for the <em>${branch}</em> branch</h1>
<p>
${description}
</p>
<h3>Changed bundles</h3>
<table>
${thead}
<tbody>
${changed.map(fileSize => sizeRow(fileSize)).join('')}
</tbody>
</table>
<h3>All bundles</h3>
<table>
${thead}
<tbody>
${fileSizes.map(fileSize => sizeRow(fileSize)).join('')}
</tbody>
</table>
<h3>Details</h3>
<ul>
<li>
<a href="${getS3Url({ branch, fileName: 'report.html', repo })}">
Bundle sizes treemap (webpack-bundle-analyzer report.html)
</a>
</li>
<li>
<a href="${getS3Url({ branch, fileName: 'stats.json', repo })}">
Webpack stats.json object
</a>
</li>
</ul>
<h3>Master branch details (for comparisons)</h3>
<ul>
<li>
<a href="${getS3Url({ branch: 'master', fileName: 'report.html', repo })}">
<del>Bundle sizes treemap (webpack-bundle-analyzer report.html)</del>
</a>
</li>
<li>
<a href="${getS3Url({ branch: 'master', fileName: 'stats.json', repo })}">
<del>Webpack stats.json object</del>
</a>
</li>
</ul>
`;
};
module.exports = perfReportTemplate;