Skip to content

Instantly share code, notes, and snippets.

@dmail
Created August 19, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmail/99aaf21384fcf2207f39cc387e9bf4a8 to your computer and use it in GitHub Desktop.
Save dmail/99aaf21384fcf2207f39cc387e9bf4a8 to your computer and use it in GitHub Desktop.
generate lighthouse report
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