Как программно добавить регулировку в Lighthouse? - PullRequest
0 голосов
/ 14 ноября 2018

Я немного запутался, как предоставлять опции регулирования через Lighthouse, используя NodeJS. Я могу сделать это через скрипт bash:

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

1 Ответ

0 голосов
/ 14 ноября 2018

В приведенном ниже примере показано, как программно запускать Lighthouse в качестве модуля 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!
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...