Created
August 19, 2019 15:36
-
-
Save dmail/99aaf21384fcf2207f39cc387e9bf4a8 to your computer and use it in GitHub Desktop.
generate lighthouse report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lighthouse = require("lighthouse") | |
const chromeLauncher = require("chrome-launcher") | |
const launchChromeAndRunLighthouse = async ({ url, opts, config = null }) => { | |
const chrome = await chromeLauncher.launch({ chromeFlags: opts.chromeFlags }) | |
opts.port = chrome.port | |
const results = await lighthouse(url, opts, config) | |
// use results.lhr for the JS-consumeable output | |
// https://github.com/GoogleChrome/lighthouse/blob/master/types/lhr.d.ts | |
// use results.report for the HTML/JSON/CSV output as a string | |
// use results.artifacts for the trace/screenshots/other specific case you need (rarer) | |
await chrome.kill() | |
const { lhr } = results | |
const { categories } = lhr | |
const scoreMap = {} | |
Object.keys(categories).forEach((categoryName) => { | |
scoreMap[categoryName] = categories[categoryName].score | |
}) | |
return scoreMap | |
} | |
launchChromeAndRunLighthouse({ | |
url: "https://google.com", | |
opts: { | |
chromeFlags: ["--show-paint-rects"], | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically