UNPKG

@vocento/wpo-check

Version:

An internal CLI tool to measure and validate Web Performance KPIs during development and CI/CD workflows.

54 lines (46 loc) 1.81 kB
// @ts-nocheck import lighthouse from 'lighthouse'; import * as chromeLauncher from 'chrome-launcher'; import { reportPartialResults } from './reporter.js'; export async function executeCheck(thresholds, config) { const results = []; const { routes, settings } = config; const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] }); try { const flags = { logLevel: 'silent', output: 'json', onlyCategories: ['performance'], port: chrome.port, }; for (let i = 0; i < routes.length; i++) { const runnerResult = await lighthouse(routes[i], flags, settings); const report = runnerResult.lhr; const audits = report.audits; const metrics = { lcp: audits['largest-contentful-paint'].numericValue / 1000, cls: audits['cumulative-layout-shift'].numericValue, inp: audits['interaction-to-next-paint-insight']?.score, fcp: audits['first-contentful-paint'].numericValue / 1000, tbt: audits['total-blocking-time'].numericValue, tti: audits['interactive'].numericValue, fid: audits['max-potential-fid']?.numericValue ?? 'n/a', networkRequests: audits['network-requests'].details.items.length, size: audits['total-byte-weight'].numericValue / 1000000, domSize: audits['dom-size'].numericValue, domDepth: audits['dom-size-insight'] .details .items .find((item) => item.statistic === 'DOM depth') ?.value ?.value, }; results.push(reportPartialResults(routes[i], metrics, thresholds, config.reportPath)); } } catch (err) { console.error(err); } finally { await chrome.kill(); } return results.length === routes.length && results.every((result) => result); }