Skip to content
Ben Line

lighthouse audit

Packages1 min read

@benline/lighthouse-audit

Go to repo
  • websiteAudit
  • gatherAuditData
1import fs from 'fs';
2import lighthouse from 'lighthouse';
3import chromeLauncher from 'chrome-launcher';
4import objectToCSV from 'objects-to-csv';
5
6const camelString = (string) => string.replace(/-./g, (x) => x.toUpperCase()[1]);
7
8const websiteAudit = async (website, i) => {
9 const browser = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
10 const options = {
11 logLevel: 'info',
12 output: 'html',
13 onlyCategories: ['performance'],
14 port: browser.port,
15 }
16
17 const auditRunner = await lighthouse(website, options);
18
19 console.log('Report is done for', auditRunner.lhr.finalUrl);
20 console.log('Performance score was', auditRunner.lhr.categories.performance.score * 100);
21
22 const requiredAuditsNames = [
23 'first-contentful-paint',
24 'speed-index',
25 'largest-contentful-paint',
26 'interactive',
27 'total-blocking-time',
28 'cumulative-layout-shift',
29 ];
30
31 const requiredAudits = {};
32 for (let i = 0; i < requiredAuditsNames.length; i++) {
33 const auditName = requiredAuditsNames[i];
34 requiredAudits[camelString(auditName)] = + auditRunner.lhr.audits[auditName].numericValue.toFixed(
35 3,
36 );
37 }
38 requiredAudits.performanceScore = auditRunner.lhr.categories.performance.score * 100;
39
40 await browser.kill();
41 return requiredAudits;
42}
43
44const gatherAuditData = async (website, count) => {
45 const values = [];
46
47 for (let i = 0; i < count; i++) {
48 const performance = await websiteAudit(`${website}`, i);
49 values.push(performance);
50 }
51
52 const csv = new objectToCSV(values);
53 await csv.toDisk(`./${website}/data.csv`);
54};
© 2021 by Ben Line. All rights reserved.