From d8422c8b53a3f1c54a07bded7cf5974b919c6996 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Mon, 10 May 2021 15:19:05 -0400 Subject: [PATCH 1/3] core(fr): convert css-usage gatherer --- lighthouse-core/gather/gatherers/css-usage.js | 40 ++++--- .../test/gather/gatherers/css-usage-test.js | 112 ++++++++++++++++++ 2 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 lighthouse-core/test/gather/gatherers/css-usage-test.js diff --git a/lighthouse-core/gather/gatherers/css-usage.js b/lighthouse-core/gather/gatherers/css-usage.js index cb2c8244ee4e..400b81b9a50e 100644 --- a/lighthouse-core/gather/gatherers/css-usage.js +++ b/lighthouse-core/gather/gatherers/css-usage.js @@ -5,35 +5,45 @@ */ 'use strict'; -const Gatherer = require('./gatherer.js'); +const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js'); /** * @fileoverview Tracks unused CSS rules. */ -class CSSUsage extends Gatherer { +class CSSUsage extends FRGatherer { + /** @type {LH.Gatherer.GathererMeta} */ + meta = { + supportedModes: ['snapshot', 'navigation'], + }; + /** - * @param {LH.Gatherer.PassContext} passContext + * @param {LH.Gatherer.FRTransitionalContext} context * @return {Promise} */ - async afterPass(passContext) { - const driver = passContext.driver; + async getArtifact(context) { + const session = context.driver.defaultSession; + const executionContext = context.driver.executionContext; /** @type {Array} */ const stylesheets = []; /** @param {LH.Crdp.CSS.StyleSheetAddedEvent} sheet */ const onStylesheetAdded = sheet => stylesheets.push(sheet); - driver.on('CSS.styleSheetAdded', onStylesheetAdded); + session.on('CSS.styleSheetAdded', onStylesheetAdded); + + await session.sendCommand('DOM.enable'); + await session.sendCommand('CSS.enable'); + await session.sendCommand('CSS.startRuleUsageTracking'); + + // Force style to recompute. + // Doesn't appear to be necessary in newer versions of Chrome. + await executionContext.evaluateAsync('getComputedStyle(document.body)'); - await driver.sendCommand('DOM.enable'); - await driver.sendCommand('CSS.enable'); - await driver.sendCommand('CSS.startRuleUsageTracking'); - await driver.executionContext.evaluateAsync('getComputedStyle(document.body)'); - driver.off('CSS.styleSheetAdded', onStylesheetAdded); + session.off('CSS.styleSheetAdded', onStylesheetAdded); // Fetch style sheet content in parallel. const promises = stylesheets.map(sheet => { const styleSheetId = sheet.header.styleSheetId; - return driver.sendCommand('CSS.getStyleSheetText', {styleSheetId}).then(content => { + return session.sendCommand('CSS.getStyleSheetText', {styleSheetId}).then(content => { return { header: sheet.header, content: content.text, @@ -42,9 +52,9 @@ class CSSUsage extends Gatherer { }); const styleSheetInfo = await Promise.all(promises); - const ruleUsageResponse = await driver.sendCommand('CSS.stopRuleUsageTracking'); - await driver.sendCommand('CSS.disable'); - await driver.sendCommand('DOM.disable'); + const ruleUsageResponse = await session.sendCommand('CSS.stopRuleUsageTracking'); + await session.sendCommand('CSS.disable'); + await session.sendCommand('DOM.disable'); const dedupedStylesheets = new Map(styleSheetInfo.map(sheet => { return [sheet.content, sheet]; diff --git a/lighthouse-core/test/gather/gatherers/css-usage-test.js b/lighthouse-core/test/gather/gatherers/css-usage-test.js new file mode 100644 index 000000000000..b52c60dc7794 --- /dev/null +++ b/lighthouse-core/test/gather/gatherers/css-usage-test.js @@ -0,0 +1,112 @@ +/** + * @license Copyright 2021 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'; + +/* eslint-env jest */ + +const CSSUsage = require('../../../gather/gatherers/css-usage.js'); +const {createMockDriver} = require('../../fraggle-rock/gather/mock-driver.js'); + +describe('.getArtifact', () => { + it('gets CSS usage', async () => { + const driver = createMockDriver(); + driver.defaultSession.on + .mockEvent('CSS.styleSheetAdded', {header: {styleSheetId: '1'}}) + .mockEvent('CSS.styleSheetAdded', {header: {styleSheetId: '2'}}); + driver.defaultSession.sendCommand + .mockResponse('DOM.enable') + .mockResponse('CSS.enable', undefined, 1) // Force events to emit + .mockResponse('CSS.startRuleUsageTracking') + .mockResponse('CSS.getStyleSheetText', {text: 'CSS text 1'}) + .mockResponse('CSS.getStyleSheetText', {text: 'CSS text 2'}) + .mockResponse('CSS.stopRuleUsageTracking', { + ruleUsage: [ + {styleSheetId: '1', used: true}, + {styleSheetId: '2', used: false}, + ], + }) + .mockResponse('CSS.disable') + .mockResponse('DOM.disable'); + + /** @type {LH.Gatherer.FRTransitionalContext} */ + const context = { + driver: driver.asDriver(), + url: 'https://example.com', + gatherMode: 'snapshot', + dependencies: {}, + }; + const gatherer = new CSSUsage(); + const artifact = await gatherer.getArtifact(context); + + expect(artifact).toEqual({ + stylesheets: [ + { + header: {styleSheetId: '1'}, + content: 'CSS text 1', + }, + { + header: {styleSheetId: '2'}, + content: 'CSS text 2', + }, + ], + rules: [ + { + styleSheetId: '1', + used: true, + }, + { + styleSheetId: '2', + used: false, + }, + ], + }); + }); + + it('dedupes stylesheets', async () => { + const driver = createMockDriver(); + driver.defaultSession.on + .mockEvent('CSS.styleSheetAdded', {header: {styleSheetId: '1'}}) + .mockEvent('CSS.styleSheetAdded', {header: {styleSheetId: '1'}}); + driver.defaultSession.sendCommand + .mockResponse('DOM.enable') + .mockResponse('CSS.enable', undefined, 1) // Force events to emit + .mockResponse('CSS.startRuleUsageTracking') + .mockResponse('CSS.getStyleSheetText', {text: 'CSS text 1'}) + .mockResponse('CSS.getStyleSheetText', {text: 'CSS text 1'}) + .mockResponse('CSS.stopRuleUsageTracking', { + ruleUsage: [ + {styleSheetId: '1', used: true}, + ], + }) + .mockResponse('CSS.disable') + .mockResponse('DOM.disable'); + + /** @type {LH.Gatherer.FRTransitionalContext} */ + const context = { + driver: driver.asDriver(), + url: 'https://example.com', + gatherMode: 'snapshot', + dependencies: {}, + }; + const gatherer = new CSSUsage(); + const artifact = await gatherer.getArtifact(context); + + expect(artifact).toEqual({ + stylesheets: [ + { + header: {styleSheetId: '1'}, + content: 'CSS text 1', + }, + ], + rules: [ + { + styleSheetId: '1', + used: true, + }, + ], + }); + }); +}); From 55706e9fc70d7df9e9dde7b8ab65e662acba91d2 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Mon, 10 May 2021 15:27:14 -0400 Subject: [PATCH 2/3] add to config --- lighthouse-core/fraggle-rock/config/default-config.js | 3 +++ types/artifacts.d.ts | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lighthouse-core/fraggle-rock/config/default-config.js b/lighthouse-core/fraggle-rock/config/default-config.js index 38f3eb305c4d..ac8d8e6d13e5 100644 --- a/lighthouse-core/fraggle-rock/config/default-config.js +++ b/lighthouse-core/fraggle-rock/config/default-config.js @@ -17,6 +17,7 @@ const artifacts = { AppCacheManifest: '', CacheContents: '', ConsoleMessages: '', + CSSUsage: '', Doctype: '', DOMStats: '', EmbeddedContent: '', @@ -56,6 +57,7 @@ const defaultConfig = { {id: artifacts.AppCacheManifest, gatherer: 'dobetterweb/appcache'}, {id: artifacts.CacheContents, gatherer: 'cache-contents'}, {id: artifacts.ConsoleMessages, gatherer: 'console-messages'}, + {id: artifacts.CSSUsage, gatherer: 'css-usage'}, {id: artifacts.Doctype, gatherer: 'dobetterweb/doctype'}, {id: artifacts.DOMStats, gatherer: 'dobetterweb/domstats'}, {id: artifacts.EmbeddedContent, gatherer: 'seo/embedded-content'}, @@ -93,6 +95,7 @@ const defaultConfig = { artifacts.AppCacheManifest, artifacts.CacheContents, artifacts.ConsoleMessages, + artifacts.CSSUsage, artifacts.Doctype, artifacts.DOMStats, artifacts.EmbeddedContent, diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index b9a45a342aa5..dd63c33a1cdf 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -20,7 +20,6 @@ declare global { export interface Artifacts extends BaseArtifacts, GathererArtifacts {} export type FRArtifacts = StrictOmit Date: Mon, 10 May 2021 16:38:29 -0400 Subject: [PATCH 3/3] comment --- lighthouse-core/gather/gatherers/css-usage.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lighthouse-core/gather/gatherers/css-usage.js b/lighthouse-core/gather/gatherers/css-usage.js index 400b81b9a50e..c83a0d8b0ff7 100644 --- a/lighthouse-core/gather/gatherers/css-usage.js +++ b/lighthouse-core/gather/gatherers/css-usage.js @@ -13,6 +13,7 @@ const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js'); class CSSUsage extends FRGatherer { /** @type {LH.Gatherer.GathererMeta} */ meta = { + // TODO(FR-COMPAT): Add support for timespan. supportedModes: ['snapshot', 'navigation'], };