UNPKG

@pierrad/web-carbon-analyzer

Version:

A tool to measure the carbon footprint of websites using CO2.js

76 lines 3.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const playwright_lighthouse_1 = require("playwright-lighthouse"); const logger_1 = __importDefault(require("../utils/logger")); class LighthouseController { /** * Creates a new LighthouseController instance * @param config Lighthouse configuration */ constructor(config) { this.config = config; } /** * Run lighthouse audit on the current page * @param page The Playwright page to audit * @param url The URL being audited (for reporting purposes) * @returns Lighthouse audit results */ async runAudit(page, url) { if (!this.config.enabled) { logger_1.default.info('Lighthouse audit is disabled, skipping'); return null; } logger_1.default.info(`Running Lighthouse audit for ${url}`); try { // Make sure we have a port configured const port = this.config.port || 9222; // Run the audit const auditResults = await (0, playwright_lighthouse_1.playAudit)({ page, port, thresholds: this.config.thresholds, opts: this.config.opts, config: this.config.config, reports: this.config.reports }); // Extract the metrics we're interested in const lhr = auditResults?.lhr; if (!lhr) { logger_1.default.warn('Lighthouse audit did not return expected results'); return null; } // Extract the specific metrics we want const metrics = { scores: { performance: Math.round((lhr.categories.performance.score ?? 0) * 100), accessibility: Math.round((lhr.categories.accessibility.score ?? 0) * 100), bestPractices: Math.round((lhr.categories['best-practices'].score ?? 0) * 100), seo: Math.round((lhr.categories.seo.score ?? 0) * 100), pwa: lhr.categories.pwa ? Math.round((lhr.categories.pwa.score ?? 0) * 100) : 0 }, metrics: { firstContentfulPaint: lhr.audits['first-contentful-paint'].numericValue, largestContentfulPaint: lhr.audits['largest-contentful-paint'].numericValue, totalBlockingTime: lhr.audits['total-blocking-time'].numericValue, speedIndex: lhr.audits['speed-index'].numericValue, timeToInteractive: lhr.audits['interactive'].numericValue, cumulativeLayoutShift: lhr.audits['cumulative-layout-shift'].numericValue }, stackPacks: lhr.stackPacks, }; logger_1.default.info(`Lighthouse audit for ${url} completed successfully`); return metrics; } catch (error) { logger_1.default.error(`Lighthouse audit failed: ${error.message}`); // We don't want to fail the whole analysis if lighthouse fails return null; } } } exports.default = LighthouseController; //# sourceMappingURL=lighthouse-controller.js.map