-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlighthouse.js
112 lines (103 loc) · 4.34 KB
/
lighthouse.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
const fs = require('fs');
const lighthouse = require('lighthouse');
const chromium = require('chromium');
const chromeLauncher = require('chrome-launcher');
const Sitemapper = require('sitemapper');
const commandLineArgs = require('command-line-args');
const options = commandLineArgs([
{ name: 'url', alias: 'u', type: String },
{ name: 'exclude', alias: 'x', type: String, multiple: true, optional: true }
]);
const rundate = new Date().toISOString().replace(/\//g, '-').replace(/:/g, '-');
let chromeOptions = {
chromeFlags: ['--headless', '--disable-dev-shm-usage'],
chromePath: chromium.path
}
let lighthouseOptions = {
logLevel: 'error',
output: 'html',
onlyCategories: ['performance'],
throttle: {
rttMs: 150,
throughputKbps: 1.6 * 1024,
cpuSlowdownMultiplier: 4
}
}
function logResultsToScreen(url, fcp, lcp, tbt, cls, speed, finalScore)
{
console.log(`\n\tLighthouse Results For: ${url}`);
console.log(`\t🎨 FCP: ${fcp}`);
console.log(`\t🖼️ LCP: ${lcp}`);
console.log(`\t⌛️ TBT: ${tbt}`);
console.log(`\t☁️ CLS: ${cls}`);
console.log(`\t🚀 Speed Index: ${speed}`);
console.log(`\t🔥 Performance Score: ${finalScore * 100}`);
}
function writeResultToFile(page, report, hasAlerts)
{
if(!fs.existsSync('reports')) {
fs.mkdirSync('reports');
}
if(!fs.existsSync(`reports/${rundate}`)) {
fs.mkdirSync(`reports/${rundate}`);
fs.mkdirSync(`reports/${rundate}/pass`);
fs.mkdirSync(`reports/${rundate}/fail`);
}
// clean up the URL and sort it into the proper folder
let pageSlug = page.replace(/^.*\/\/[^\/]+/, '').replace(/\//g, '-').slice(1);
if(pageSlug[pageSlug.length-1] === '-') {
pageSlug = pageSlug.slice(0, pageSlug.length-1);
}
// If there is nothing, it must be the index
if(pageSlug === '') {
pageSlug = 'index';
}
let targetFile = `reports/${rundate}/${hasAlerts ? 'fail' : 'pass'}/${pageSlug}.html`
fs.writeFileSync(`${targetFile}`, report);
}
async function getPagesToCheck() {
const mapper = new Sitemapper({timeout: 5000});
const { sites } = await mapper.fetch(options.url);
if(!options.exclude) {
options.exclude = [];
}
let pages = sites.filter(item => {
for(let exclusion of options.exclude) {
if(item.includes(exclusion)) {
return false;
}
}
return true;
});
return pages;
}
(async () => {
const pages = await getPagesToCheck();
const chrome = await chromeLauncher.launch(chromeOptions);
lighthouseOptions.port = chrome.port;
for(let page of pages) {
let hasAlerts = false;
console.log(`testing: ${page}`);
const runnerResult = await lighthouse(page, lighthouseOptions);
const first_contentful_paint = runnerResult.lhr.audits['first-contentful-paint'].displayValue;
const first_meaningful_paint = runnerResult.lhr.audits['first-meaningful-paint'].displayValue;
const largest_contentful_paint = runnerResult.lhr.audits['largest-contentful-paint'].displayValue;
const total_blocking_time = runnerResult.lhr.audits['total-blocking-time'].displayValue;
const speed_index = runnerResult.lhr.audits['speed-index'].displayValue;
const cls = runnerResult.lhr.audits['cumulative-layout-shift'].displayValue;
logResultsToScreen(page, first_contentful_paint, largest_contentful_paint, total_blocking_time, cls, speed_index, runnerResult.lhr.categories.performance.score);
if(runnerResult.lhr.audits['largest-contentful-paint'].numericValue > 2500) {
console.log(`\t❌ LCP Over Limit: ${runnerResult.lhr.audits['largest-contentful-paint'].numericValue - 2500}ms`)
hasAlerts = true;
}
if(runnerResult.lhr.audits['cumulative-layout-shift'].numericValue > .10) {
console.log(`\t❌ CLS Too High: ${runnerResult.lhr.finalUrl}`);
hasAlerts = true;
}
if(runnerResult.lhr.audits['first-meaningful-paint'].numericValue < 2500 && runnerResult.lhr.audits['cumulative-layout-shift'].numericValue < .10 && runnerResult.lhr.audits['largest-contentful-paint'].numericValue <= 2500) {
console.log(`\t🍀 Results OK!`);
}
writeResultToFile(page, runnerResult.report, hasAlerts);
}
await chrome.kill();
})();