@codechecks/lighthouse-keeper
Version:
Keep an eye on Google Lighthouse score changes
40 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chromeLauncher = require("chrome-launcher");
const lighthouse = require("lighthouse");
const ReportGenerator = require("lighthouse/lighthouse-core/report/report-generator");
async function runLighthouseAndGetReport(url) {
const lighthouseResult = await launchChromeAndRunLighthouse(url);
const htmlReport = createHtmlReport(lighthouseResult.lhr);
const jsonReport = createJsonReport(lighthouseResult.lhr);
const categoryReport = createCategoryReport(lighthouseResult.lhr);
return { metrics: categoryReport, htmlReport, audits: Object.values(jsonReport.audits) };
}
exports.runLighthouseAndGetReport = runLighthouseAndGetReport;
const launchChromeAndRunLighthouse = async (url) => {
const chrome = await chromeLauncher.launch({
chromeFlags: ["--disable-gpu", "--headless", "--no-zygote", "--no-sandbox"],
});
const flags = {
port: chrome.port,
output: "json",
};
const result = await lighthouse(url, flags);
await chrome.kill();
return result;
};
function createHtmlReport(results) {
return ReportGenerator.generateReportHtml(results);
}
function createJsonReport(results) {
return JSON.parse(ReportGenerator.generateReport(results, "json"));
}
function createCategoryReport(results) {
const { categories } = results;
return Object.keys(categories).reduce((categoryReport, categoryName) => {
const category = results.categories[categoryName];
categoryReport[category.id] = Math.round(category.score * 100);
return categoryReport;
}, {});
}
//# sourceMappingURL=lighthouse.js.map