@foo-software/lighthouse-persist
Version:
A tool for persisting Lighthouse audit results used for website performance monitoring and analysis.
52 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadReportUtils = void 0;
// a bit of a hack until we only support ES Modules
// note the key here:
// https://github.com/microsoft/TypeScript/pull/44501#issue-914346744
const loadReportUtils = async () => {
const module = await import('lighthouse/report/renderer/report-utils.js');
return module.ReportUtils;
};
exports.loadReportUtils = loadReportUtils;
// inspired by:
// https://github.com/GoogleChrome/lighthouse/blob/8100b8034507e679c95b2fab5ab48965875443b6/report/renderer/performance-category-renderer.js#L98
const getWastedMs = (audit) => {
if (audit.result.details && audit.result.details.type === 'opportunity') {
const details = audit.result.details;
if (typeof details.overallSavingsMs !== 'number') {
throw new Error('non-opportunity details passed to getWastedMs');
}
return details.overallSavingsMs;
}
else {
return Number.MIN_VALUE;
}
};
// inspired by:
// https://github.com/GoogleChrome/lighthouse/blob/2e9c3c9b5f7d75b39be9d1e2ba116d49cf811f81/lighthouse-core/report/html/renderer/performance-category-renderer.js#L224-L226
exports.default = async (result) => {
const ReportUtils = await (0, exports.loadReportUtils)();
return result.categories.performance.auditRefs
.reduce((accumulator, audit) => {
const auditResult = result.audits[audit.id];
const detailsType = auditResult?.details?.type;
if (detailsType !== 'opportunity' ||
ReportUtils.showAsPassed(auditResult)) {
return accumulator;
}
return [
...accumulator,
{
...audit,
result: {
...auditResult,
// "average" | "fail" | "pass" | ...
rating: ReportUtils.calculateRating(auditResult.score, auditResult.scoreDisplayMode),
},
},
];
}, [])
.sort((auditA, auditB) => getWastedMs(auditB) - getWastedMs(auditA));
};
//# sourceMappingURL=getOpportunities.js.map