如何以编程方式在Lighthouse中添加限制?

时间:2018-11-14 10:24:16

标签: node.js lighthouse

我有点困惑如何使用NodeJS通过Lighthouse提供节流选项。我可以通过bash脚本来做到这一点:

lighthouse https://hazemhagrass.com --quiet --chrome-flags='--headless' --output=json --output-path=>hazem.json

1 个答案:

答案 0 :(得分:1)

以下示例显示了如何以编程方式将灯塔作为具有自定义配置的Node模块运行。

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');

function launchChromeAndRunLighthouse(url, opts, config = null) {
  return chromeLauncher.launch({
    chromeFlags: opts.chromeFlags
  }).then(chrome => {
    opts.port = chrome.port;
    const options = Object.assign({}, flags, config);
    return lighthouse(url, opts).then(results => {
      // use results.lhr for the JS-consumeable output
      // https://github.com/GoogleChrome/lighthouse/blob/master/typings/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)
      return chrome.kill().then(() => results.lhr)
    });
  });
}

const opts = {
  chromeFlags: ['--show-paint-rects']
};

// Usage:
const config = {
  throttling: {
    rttMs: 150,
    throughputKbps: 1.6 * 1024,
    cpuSlowdownMultiplier: 4,
  }
};
launchChromeAndRunLighthouse('https://hazemhagrass.com', opts, config).then(results => {
  // Use results!
});