forked from sitespeedio/plugin-lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunAudit.js
74 lines (61 loc) · 1.62 KB
/
runAudit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const puppeteer = require('puppeteer');
const lighthouse = require('lighthouse');
const merge = require('lodash.merge');
const defaultChromeSettings = {
ignoreHTTPSErrors: true,
headless: true,
args: ['--no-sandbox', '--disable-gpu']
};
const launchBrowser = async puppeteerSettings =>
puppeteer.launch(merge(defaultChromeSettings, puppeteerSettings));
const closeBrowser = async browser => browser.close();
const getPort = browser => {
const browserWSEndpoint = browser.wsEndpoint();
const { port } = require('url').parse(browserWSEndpoint);
return port;
};
const executePreScript = ({ browser, lighthousePreScript }) =>
require(lighthousePreScript)(browser);
const runLighthouse = async ({
url,
port,
lightHouseConfig,
lighthouseFlags
}) => {
if (lightHouseConfig && !lightHouseConfig.extends) {
lightHouseConfig.extends = 'lighthouse:default';
}
const results = await lighthouse(
url,
Object.assign(
{},
{
port
},
lighthouseFlags
),
lightHouseConfig
);
const lhrAndReport = { lhr: results.lhr, report: results.report };
return lhrAndReport;
};
module.exports = async function runAudit({
url,
lightHouseConfig,
lighthouseFlags,
lighthousePreScript
}) {
const puppeteerSettings = lightHouseConfig.puppeteer || {};
const browser = await launchBrowser(puppeteerSettings);
if (lighthousePreScript) {
await executePreScript({ browser, lighthousePreScript });
}
const results = await runLighthouse({
url,
port: getPort(browser),
lightHouseConfig,
lighthouseFlags
});
await closeBrowser(browser);
return results;
};