我有点困惑如何使用 NodeJS 通过 Lighthouse 提供节流选项。我可以通过 bash 脚本来做到这一点:
lighthouse https://hazemhagrass.com --quiet --chrome-flags='--headless' --output=json --output-path=>hazem.json
我有点困惑如何使用 NodeJS 通过 Lighthouse 提供节流选项。我可以通过 bash 脚本来做到这一点:
lighthouse https://hazemhagrass.com --quiet --chrome-flags='--headless' --output=json --output-path=>hazem.json
下面的示例显示了如何以编程方式将 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!
});