-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.js
199 lines (167 loc) · 5.77 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
'use strict';
const fs = require('fs');
const path = require('path');
const prettyBytes = require('pretty-bytes');
const chalk = require('chalk');
const logSymbols = require('log-symbols');
const THRESHOLD = 70;
class Output {
constructor() {
/**
* What indices from BigQuery do we want to summarize scores
* for in the CLI output?
* @type {number[]}
*/
this.indicesOfInterest = [3, 6, 9]; // 25th, 50th, 75th percentile
/**
* Is the weight higher than one of the BigQuery
* percentiles?
* @type {boolean}
*/
this.fasterThanAPercentile = false;
this.bigQueryData = {
desktop: [],
mobile: [],
titles: []
};
/**
* A subset of BigQuery data that we actually use for output
* This should represent the indices of interest from the
* bigQueryData.
* @type {{desktop: Array, mobile: Array, titles: Array}}
*/
this.outputData = {
desktop: [],
mobile: [],
titles: []
};
/**
* Average image weights per site, based on BigQuery data
* @type {{desktop: number, mobile: number}}
*/
this.medians = {};
const data = fs.readFileSync(path.join(__dirname, 'data/bigquery.csv'), 'utf8').split('\n');
// Complete full set of available BigQuery Data
this.bigQueryData.titles = data[0].split(',');
this.bigQueryData.desktop = data[1].split(',').slice(1).map(x => parseInt(x, 10));
this.bigQueryData.mobile = data[2].split(',').slice(1).map(x => parseInt(x, 10));
// Sub-slice portions of data we're interested
for (const item of this.indicesOfInterest) {
this.outputData.desktop.push(this.bigQueryData.desktop[item]);
this.outputData.mobile.push(this.bigQueryData.mobile[item]);
this.outputData.titles.push(this.bigQueryData.titles[item]);
}
this.medians.mobile = parseInt(this.bigQueryData.mobile[1], 10);
this.medians.desktop = parseInt(this.bigQueryData.desktop[1], 10);
}
/**
* Compare a supplied image weight with the weight in a supplied percentile.
* @param siteImageWeight
* @param percentileImageWeight
* @param percentile
* @returns {string} Summary message of comparisons to the percentile
*/
compareWeightPercentile(siteImageWeight, percentileImageWeight, percentile) {
if (siteImageWeight === undefined) {
return;
}
let diff = (siteImageWeight - parseInt(percentileImageWeight, 10)) * 1000;
if (diff > 0) {
diff = chalk.red('+' + prettyBytes(diff));
} else {
diff *= -1;
diff = chalk.green('-' + prettyBytes(diff));
this.fasterThanAPercentile = true;
}
return diff + (' compared to sites in the ') + chalk.yellow(percentile.replace('p', '') + 'th') + ' percentile';
}
compareWeights(siteImageWeight, sizes, percentiles) {
if (siteImageWeight === undefined) {
return;
}
let comparisons = '';
siteImageWeight = parseInt(siteImageWeight, 10);
for (let i = 0; i < percentiles.length; i++) {
comparisons += this.compareWeightPercentile(siteImageWeight, sizes[i], percentiles[i]) + '\n';
}
return comparisons;
}
/**
* Check if image weight is higher than one of the available percentile sizes
* @param sizeImageWeight
* @param sizes
* @param percentiles
* @returns {*}
*/
getHighestPercentile(sizeImageWeight, sizes, percentiles) {
if (sizeImageWeight === undefined) {
return;
}
let highestPercentileMatch = -1;
let result;
// Begin with index 2 to avoid catching unnecessary labels
// like `desktop` and `mobile` included in this row of data
for (let i = 2; i < percentiles.length; i++) {
sizes[i] = parseInt(sizes[i], 10);
if (sizeImageWeight > sizes[i]) {
highestPercentileMatch = i;
}
}
if (highestPercentileMatch === -1) {
result = '0';
} else {
result = percentiles[highestPercentileMatch];
}
return result;
}
process(opts, res) {
const threshold = opts.threshold || THRESHOLD;
const yourImageWeight = parseInt(res.pageStats.imageResponseBytes || 0, 10);
const unoptimizedImages = res.formattedResults.ruleResults.OptimizeImages.urlBlocks;
const desktopWeights = this.compareWeights(yourImageWeight / 1000, this.outputData.desktop, this.outputData.titles);
const mobileWeights = this.compareWeights(yourImageWeight / 1000, this.outputData.mobile, this.outputData.titles);
const unoptimizedUrls = unoptimizedImages[0] && unoptimizedImages[0].urls;
let imagesToOptimize = '';
if (opts.verbose && unoptimizedUrls && unoptimizedUrls.length > 0) {
for (const url of unoptimizedUrls) {
for (const x of url.result.args) {
let result = '';
switch (x.type) {
case 'URL':
result += chalk.green(x.value);
break;
case 'BYTES':
result += `Size: ${chalk.red(x.value)}`;
break;
case 'PERCENTAGE':
result += `Can be improved by ${chalk.yellow(x.value)}\n`;
break;
default:
// No default
}
imagesToOptimize += result + '\n';
}
}
}
console.log([
chalk.cyan('\nYour image weight'), prettyBytes(yourImageWeight),
chalk.gray('Median mobile site image weight'), prettyBytes(this.medians.mobile * 1000),
chalk.gray('Median desktop site image weight'), prettyBytes(this.medians.desktop * 1000),
chalk.cyan('\nOn Mobile'),
mobileWeights,
chalk.cyan('On Desktop'),
desktopWeights
].join('\n'));
if (this.fasterThanAPercentile) {
console.log(chalk.cyan('Thanks for keeping the web fast <3'));
}
if (imagesToOptimize.length > 0) {
console.log(chalk.underline('\nImages to optimize\n') + imagesToOptimize + chalk.cyan('This list does not include images which cannot be optimized further.\nYou may consider removing those images if possible.\n'));
}
if (res.score < threshold) {
console.error(chalk.bold(`${logSymbols.error} Threshold of ${threshold} not met with score of ${res.score}`));
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}
}
}
module.exports = () => new Output();