Open1

lighthose npm module

terrierscriptterrierscript
import lighthouse, { Config } from 'lighthouse'
import * as chromeLauncher from 'chrome-launcher'

const main = async () => {

  const execute = async (url: string) => {
    const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] })
    const flags = { port: chrome.port }
    const config: Config = {
      // logLevel: 'info',
      extends: 'lighthouse:default',
      settings: {
        output: 'json',
        onlyCategories: ['performance'],
      }
    }
    const runnerResult = await lighthouse(url, flags, config)

    const score = runnerResult?.lhr.categories.performance.score
    const details = runnerResult?.lhr.categories.performance.auditRefs
      .filter((audit) => audit.weight > 0)
      .map((audit) => {
        return `${audit.acronym} ${runnerResult.lhr.audits[audit.id].score}`
      }) ?? []
    const result = `Score: ${score} (${details.join('/')})`
    return result
  }

  const start = async (name: string, url: string) => {
    let results = []
    for (let i = 0;i < 5;i++) {
      const result = await execute(url)
      results.push(result)
    }
    console.log([name, ...results].join('\n'))
  }

  await start("sample1", 'http://localhost:3000/sample1')
  await start("sample2", 'http://localhost:3000/sample2')

  process.exit(0)
}
main()