-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
lightrider-entry.js
89 lines (76 loc) · 3.38 KB
/
lightrider-entry.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
const lighthouse = require('../lighthouse-core/index.js');
const assetSaver = require('../lighthouse-core/lib/asset-saver.js');
const LHError = require('../lighthouse-core/lib/lh-error.js');
const preprocessor = require('../lighthouse-core/lib/proto-preprocessor.js');
/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */
const LR_PRESETS = {
mobile: require('../lighthouse-core/config/lr-mobile-config.js'),
desktop: require('../lighthouse-core/config/lr-desktop-config.js'),
};
/** @typedef {import('../lighthouse-core/gather/connections/connection.js')} Connection */
/**
* Run lighthouse for connection and provide similar results as in CLI.
* @param {Connection} connection
* @param {string} url
* @param {LH.Flags} flags Lighthouse flags, including `output`
* @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array<string>, logAssets: boolean}} lrOpts Options coming from Lightrider
* @return {Promise<string|Array<string>|void>}
*/
async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs, logAssets}) {
// Certain fixes need to kick in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839
global.isLightRider = true;
// disableStorageReset because it causes render server hang
flags.disableStorageReset = true;
flags.logLevel = flags.logLevel || 'info';
const config = lrDevice === 'desktop' ? LR_PRESETS.desktop : LR_PRESETS.mobile;
if (categoryIDs) {
config.settings = config.settings || {};
config.settings.onlyCategories = categoryIDs;
}
try {
const results = await lighthouse(url, flags, config, connection);
if (!results) return;
if (logAssets) {
await assetSaver.logAssets(results.artifacts, results.lhr.audits);
}
// pre process the LHR for proto
if (flags.output === 'json' && typeof results.report === 'string') {
return preprocessor.processForProto(results.report);
}
return results.report;
} catch (err) {
// If an error ruined the entire lighthouse run, attempt to return a meaningful error.
let runtimeError;
if (!(err instanceof LHError) || !err.lhrRuntimeError) {
runtimeError = {
code: LHError.UNKNOWN_ERROR,
message: `Unknown error encountered with message '${err.message}'`,
};
} else {
runtimeError = {
code: err.code,
message: err.friendlyMessage ?
`${err.friendlyMessage} (${err.message})` :
err.message,
};
}
return JSON.stringify({runtimeError}, null, 2);
}
}
if (typeof module !== 'undefined' && module.exports) {
// Export for require()ing into unit tests.
module.exports = {
runLighthouseInLR,
};
}
// Expose on window for browser-residing consumers of file.
if (typeof window !== 'undefined') {
// @ts-ignore
window.runLighthouseInLR = runLighthouseInLR;
}