forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css-usage.js
150 lines (127 loc) · 4.2 KB
/
css-usage.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Tracks unused CSS rules.
*/
import log from 'lighthouse-logger';
import BaseGatherer from '../base-gatherer.js';
import {Sentry} from '../../lib/sentry.js';
class CSSUsage extends BaseGatherer {
constructor() {
super();
/** @type {LH.Gatherer.ProtocolSession|undefined} */
this._session = undefined;
/** @type {Map<string, Promise<LH.Artifacts.CSSStyleSheetInfo|Error>>} */
this._sheetPromises = new Map();
/**
* Initialize as undefined so we can assert results are fetched.
* @type {LH.Crdp.CSS.RuleUsage[]|undefined}
*/
this._ruleUsage = undefined;
this._onStylesheetAdded = this._onStylesheetAdded.bind(this);
}
/** @type {LH.Gatherer.GathererMeta} */
meta = {
supportedModes: ['snapshot', 'timespan', 'navigation'],
};
/**
* @param {LH.Crdp.CSS.StyleSheetAddedEvent} event
*/
async _onStylesheetAdded(event) {
if (!this._session) throw new Error('Session not initialized');
const styleSheetId = event.header.styleSheetId;
const sheetPromise = this._session.sendCommand('CSS.getStyleSheetText', {styleSheetId})
.then(content => ({
header: event.header,
content: content.text,
}))
.catch(/** @param {Error} err */ (err) => {
log.warn(
'CSSUsage',
`Error fetching content of stylesheet with URL "${event.header.sourceURL}"`
);
Sentry.captureException(err, {
tags: {
gatherer: 'CSSUsage',
},
extra: {
url: event.header.sourceURL,
},
level: 'error',
});
return err;
});
this._sheetPromises.set(styleSheetId, sheetPromise);
}
/**
* @param {LH.Gatherer.Context} context
*/
async startCSSUsageTracking(context) {
const session = context.driver.defaultSession;
this._session = session;
session.on('CSS.styleSheetAdded', this._onStylesheetAdded);
await session.sendCommand('DOM.enable');
await session.sendCommand('CSS.enable');
await session.sendCommand('CSS.startRuleUsageTracking');
}
/**
* @param {LH.Gatherer.Context} context
*/
async stopCSSUsageTracking(context) {
const session = context.driver.defaultSession;
const coverageResponse = await session.sendCommand('CSS.stopRuleUsageTracking');
this._ruleUsage = coverageResponse.ruleUsage;
session.off('CSS.styleSheetAdded', this._onStylesheetAdded);
}
/**
* @param {LH.Gatherer.Context} context
*/
async startInstrumentation(context) {
if (context.gatherMode !== 'timespan') return;
await this.startCSSUsageTracking(context);
}
/**
* @param {LH.Gatherer.Context} context
*/
async stopInstrumentation(context) {
if (context.gatherMode !== 'timespan') return;
await this.stopCSSUsageTracking(context);
}
/**
* @param {LH.Gatherer.Context} context
* @return {Promise<LH.Artifacts['CSSUsage']>}
*/
async getArtifact(context) {
const session = context.driver.defaultSession;
const executionContext = context.driver.executionContext;
if (context.gatherMode !== 'timespan') {
await this.startCSSUsageTracking(context);
// Force style to recompute.
// Doesn't appear to be necessary in newer versions of Chrome.
await executionContext.evaluateAsync('getComputedStyle(document.body)');
await this.stopCSSUsageTracking(context);
}
/** @type {Map<string, LH.Artifacts.CSSStyleSheetInfo>} */
const dedupedStylesheets = new Map();
const sheets = await Promise.all(this._sheetPromises.values());
for (const sheet of sheets) {
// Erroneous sheets will be reported via sentry and the log.
// We can ignore them here without throwing a fatal error.
if (sheet instanceof Error) {
continue;
}
dedupedStylesheets.set(sheet.content, sheet);
}
await session.sendCommand('CSS.disable');
await session.sendCommand('DOM.disable');
if (!this._ruleUsage) throw new Error('Issue collecting rule usages');
return {
rules: this._ruleUsage,
stylesheets: Array.from(dedupedStylesheets.values()),
};
}
}
export default CSSUsage;