feat: Disable Lighthouse CI report uploading and generate a local summary table of PageSpeed scores instead.
This commit is contained in:
@@ -79,33 +79,67 @@ async function main() {
|
||||
const chromePath = process.env.CHROME_PATH || process.env.PUPPETEER_EXECUTABLE_PATH;
|
||||
const chromePathArg = chromePath ? `--collect.chromePath="${chromePath}"` : '';
|
||||
|
||||
// Clean up old reports
|
||||
if (fs.existsSync('.lighthouseci')) {
|
||||
fs.rmSync('.lighthouseci', { recursive: true, force: true });
|
||||
}
|
||||
|
||||
// Using a more robust way to execute and capture output
|
||||
const lhciCommand = `npx lhci collect ${urlArgs} ${chromePathArg} --collect.settings.chromeFlags='--no-sandbox --disable-setuid-sandbox' --collect.settings.extraHeaders='${extraHeaders}' && npx lhci assert && npx lhci upload`;
|
||||
// We remove 'npx lhci upload' to keep everything local and avoid Google-hosted reports
|
||||
const lhciCommand = `npx lhci collect ${urlArgs} ${chromePathArg} --collect.settings.chromeFlags='--no-sandbox --disable-setuid-sandbox' --collect.settings.extraHeaders='${extraHeaders}' && npx lhci assert`;
|
||||
|
||||
console.log(`💻 Executing LHCI...`);
|
||||
|
||||
try {
|
||||
const output = execSync(lhciCommand, {
|
||||
execSync(lhciCommand, {
|
||||
encoding: 'utf8',
|
||||
stdio: ['inherit', 'pipe', 'inherit'], // Pipe stdout so we can parse it
|
||||
stdio: 'inherit',
|
||||
});
|
||||
} catch (err: any) {
|
||||
console.warn('⚠️ LHCI assertion finished with warnings or errors.');
|
||||
// We continue to show the table even if assertions failed
|
||||
}
|
||||
|
||||
// 3. Summarize Results (Local & Independent)
|
||||
const manifestPath = path.join(process.cwd(), '.lighthouseci', 'manifest.json');
|
||||
if (fs.existsSync(manifestPath)) {
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
||||
console.log(`\n📊 PageSpeed Summary (FOSS - Local Report):\n`);
|
||||
|
||||
const summaryTable = manifest.map((entry: any) => {
|
||||
const s = entry.summary;
|
||||
return {
|
||||
URL: entry.url.replace(targetUrl, ''),
|
||||
Perf: Math.round(s.performance * 100),
|
||||
Acc: Math.round(s.accessibility * 100),
|
||||
BP: Math.round(s['best-practices'] * 100),
|
||||
SEO: Math.round(s.seo * 100),
|
||||
};
|
||||
});
|
||||
|
||||
console.log(output);
|
||||
console.table(summaryTable);
|
||||
|
||||
// Extract report URL from LHCI output
|
||||
const reportMatch = output.match(
|
||||
/Sent to (https:\/\/storage\.googleapis\.com\/lighthouse-infrastructure\.appspot\.com\/reports\/[^\s]+)/,
|
||||
);
|
||||
if (reportMatch && reportMatch[1]) {
|
||||
const reportUrl = reportMatch[1];
|
||||
console.log(`\n📊 Report URL: ${reportUrl}`);
|
||||
fs.writeFileSync('pagespeed-report-url.txt', reportUrl);
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error('❌ LHCI execution failed.');
|
||||
if (err.stdout) console.log(err.stdout);
|
||||
if (err.stderr) console.error(err.stderr);
|
||||
throw err;
|
||||
// Calculate Average
|
||||
const avg = {
|
||||
Perf: Math.round(
|
||||
summaryTable.reduce((acc: any, curr: any) => acc + curr.Perf, 0) / summaryTable.length,
|
||||
),
|
||||
Acc: Math.round(
|
||||
summaryTable.reduce((acc: any, curr: any) => acc + curr.Acc, 0) / summaryTable.length,
|
||||
),
|
||||
BP: Math.round(
|
||||
summaryTable.reduce((acc: any, curr: any) => acc + curr.BP, 0) / summaryTable.length,
|
||||
),
|
||||
SEO: Math.round(
|
||||
summaryTable.reduce((acc: any, curr: any) => acc + curr.SEO, 0) / summaryTable.length,
|
||||
),
|
||||
};
|
||||
|
||||
console.log(`\n📈 Average Scores:`);
|
||||
console.log(` Performance: ${avg.Perf > 90 ? '✅' : '⚠️'} ${avg.Perf}`);
|
||||
console.log(` Accessibility: ${avg.Acc > 90 ? '✅' : '⚠️'} ${avg.Acc}`);
|
||||
console.log(` Best Practices: ${avg.BP > 90 ? '✅' : '⚠️'} ${avg.BP}`);
|
||||
console.log(` SEO: ${avg.SEO > 90 ? '✅' : '⚠️'} ${avg.SEO}`);
|
||||
}
|
||||
|
||||
console.log(`\n✨ PageSpeed tests completed successfully!`);
|
||||
|
||||
Reference in New Issue
Block a user