beam-cli
Version:
A beautifully simple CLI for running Lighthouse audits on a statically generated (SSG) website
52 lines (51 loc) • 1.7 kB
JavaScript
import { lighthouseCategoriesArray } from '../lighthouse/categories.js';
/** Gets the total transfered size of a page (in KiB) */
const getPageSizeFromRunResult = (result) => {
if (!result)
return 0;
const { details } = result;
if (details && details.type === 'table') {
const total = details.items.find(i => i.resourceType === 'total');
if (total) {
return total.transferSize ?? 0;
}
}
return 0;
};
/** Extracts the data that we are interested in
* from the lighthouse audit results */
export const analyseLighthouseResults = (runnerResults, generatedHTMLFiles) => {
const results = {};
const names = Object.keys(runnerResults);
for (const name of names) {
const runResult = runnerResults[name];
if (!runResult)
continue;
const { result, type, url } = runResult;
const resourceSummary = result.lhr.audits['resource-summary'];
const sizeDisplay = resourceSummary?.displayValue ?? '';
const size = getPageSizeFromRunResult(resourceSummary);
const scores = {};
for (const cat of lighthouseCategoriesArray) {
const category = result.lhr.categories[cat];
if (category) {
scores[cat] = category.score ?? 0;
}
}
const htmlPath = generatedHTMLFiles[name];
const summaryResult = {
name,
size,
sizeDisplay,
mobile: type === 'mobile',
scores,
htmlPath,
url,
};
results[name] = summaryResult;
}
return {
data: results,
date: Date.now().valueOf(),
};
};