File size: 1,974 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Lighthouse budgets for the public sign-in page (signed-in screens are covered by e2e/a11y.e2e.ts).
// Usage: npm run lighthouse [-- <url>]   (default: the e2e server, http://127.0.0.1:8811/login)
// Lighthouse runs through `npx lighthouse@<pinned>` instead of a devDependency: its own dependency tree carries npm audit
// advisories we do not want in the lockfile (docs/DEPENDENCIES.md). Needs Chrome/Chromium (Playwright's works).
import { spawnSync } from 'node:child_process';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

const LIGHTHOUSE = 'lighthouse@13.6.0';
const url = process.argv[2] || 'http://127.0.0.1:8811/login';
const BUDGET = { performance: 80, accessibility: 95, 'best-practices': 90, seo: 80 };
const dir = mkdtempSync(join(tmpdir(), 'pw-lh-'));
let failed = false;

for (const preset of ['desktop', 'mobile']) {
  const out = join(dir, `${preset}.json`);
  const args = ['--yes', LIGHTHOUSE, url, '--quiet', '--output=json', `--output-path=${out}`, '--chrome-flags=--headless=new --no-sandbox',
    '--only-categories=performance,accessibility,best-practices,seo', ...(preset === 'desktop' ? ['--preset=desktop'] : [])];
  const r = spawnSync('npx', args, { stdio: ['ignore', 'inherit', 'inherit'], shell: process.platform === 'win32' });
  if (r.status !== 0) { console.error(`Lighthouse (${preset}) did not finish (exit ${r.status}).`); failed = true; continue; }
  const report = JSON.parse(readFileSync(out, 'utf8'));
  console.log(`\n${preset} - ${url}`);
  for (const [id, min] of Object.entries(BUDGET)) {
    const score = Math.round((report.categories[id]?.score ?? 0) * 100);
    const ok = score >= min;
    if (!ok) failed = true;
    console.log(`  ${ok ? 'ok  ' : 'FAIL'} ${id.padEnd(15)} ${String(score).padStart(3)}  (budget ${min})`);
  }
}
rmSync(dir, { recursive: true, force: true });
process.exit(failed ? 1 : 0);