-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathdevtools-entry.js
116 lines (104 loc) · 4.58 KB
/
devtools-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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @license Copyright 2016 The Lighthouse Authors. 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';
/* global globalThis */
import {Buffer} from 'buffer';
import lighthouse, {legacyNavigation} from '../../lighthouse-core/index.js';
import {navigation, startTimespan, snapshot} from '../../lighthouse-core/fraggle-rock/api.js';
import {RawConnection} from '../../lighthouse-core/gather/connections/raw.js';
import log from 'lighthouse-logger';
import {lookupLocale} from '../../lighthouse-core/lib/i18n/i18n.js';
import {registerLocaleData, getCanonicalLocales} from '../../shared/localization/format.js';
import * as constants from '../../lighthouse-core/config/constants.js';
/** @typedef {import('../../lighthouse-core/gather/connections/connection.js')} Connection */
// Rollup seems to overlook some references to `Buffer`, so it must be made explicit.
// (`parseSourceMapFromDataUrl` breaks without this)
/** @type {BufferConstructor} */
globalThis.Buffer = Buffer;
/**
* Returns a config, which runs only certain categories.
* Varies the config to use based on device.
* If `lighthouse-plugin-publisher-ads` is in the list of
* `categoryIDs` the plugin will also be run.
* Counterpart to the CDT code that sets flags.
* @see https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/lighthouse/LighthouseController.ts;l=280
* @param {Array<string>} categoryIDs
* @param {string} device
* @return {LH.Config.Json}
*/
function createConfig(categoryIDs, device) {
/** @type {LH.SharedFlagsSettings} */
const settings = {
onlyCategories: categoryIDs,
// In DevTools, emulation is applied _before_ Lighthouse starts (to deal with viewport emulation bugs). go/xcnjf
// As a result, we don't double-apply viewport emulation.
screenEmulation: {disabled: true},
};
if (device === 'desktop') {
settings.throttling = constants.throttling.desktopDense4G;
// UA emulation, however, is lost in the protocol handover from devtools frontend to the lighthouse_worker. So it's always applied.
settings.emulatedUserAgent = constants.userAgents.desktop;
settings.formFactor = 'desktop';
}
return {
extends: 'lighthouse:default',
// TODO(esmodules): re-enable when pubads works again
// plugins: ['lighthouse-plugin-publisher-ads'],
settings,
};
}
/**
* @param {import('../../lighthouse-core/gather/connections/raw.js').Port} port
* @return {RawConnection}
*/
function setUpWorkerConnection(port) {
return new RawConnection(port);
}
/** @param {(status: [string, string, string]) => void} listenCallback */
function listenForStatus(listenCallback) {
log.events.addListener('status', listenCallback);
log.events.addListener('warning', listenCallback);
}
/**
* Does a locale lookup but limits the result to the *canonical* Lighthouse
* locales, which are only the locales with a messages locale file that can
* be downloaded and then used via `registerLocaleData`.
* @param {string|string[]=} locales
* @return {LH.Locale}
*/
function lookupCanonicalLocale(locales) {
return lookupLocale(locales, getCanonicalLocales());
}
// Expose only in DevTools' worker
if (typeof self !== 'undefined') {
// TODO: refactor and delete `global.isDevtools`.
global.isDevtools = true;
// @ts-expect-error
self.setUpWorkerConnection = setUpWorkerConnection;
// @ts-expect-error
self.runLighthouse = legacyNavigation;
// @ts-expect-error
self.runLighthouseNavigation = navigation;
// @ts-expect-error
self.startLighthouseTimespan = startTimespan;
// @ts-expect-error
self.runLighthouseSnapshot = snapshot;
// @ts-expect-error
self.createConfig = createConfig;
// @ts-expect-error
self.listenForStatus = listenForStatus;
// @ts-expect-error
self.registerLocaleData = registerLocaleData;
// TODO: expose as lookupCanonicalLocale in LighthouseService.ts?
// @ts-expect-error
self.lookupLocale = lookupCanonicalLocale;
} else {
// For the bundle smoke test.
// @ts-expect-error
global.runBundledLighthouse = lighthouse;
// @ts-expect-error
global.runBundledLighthouseLegacyNavigation = legacyNavigation;
}