From afc37474570a3ebae71ef163aa5a727b077619f9 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 23 Aug 2024 16:35:55 -0700 Subject: [PATCH 1/3] clients(devtools): require third-party-web to be provided --- build/build-bundle.js | 5 +++++ clients/devtools/devtools-entry.js | 22 +++------------------- core/lib/third-party-web.js | 14 +++++++++++++- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/build/build-bundle.js b/build/build-bundle.js index 51476e0b7bf5..016281619fab 100644 --- a/build/build-bundle.js +++ b/build/build-bundle.js @@ -136,6 +136,11 @@ async function buildBundle(entryPath, distPath, opts = {minify: true}) { shimsObj[`${LH_ROOT}/shared/localization/locales.js`] = 'export const locales = {};'; } + // Don't bundle third-party-web (CDT provides its own copy). This prevents duplications of 40+ KB. + if (isDevtools(entryPath)) { + shimsObj['third-party-web/nostats-subset.js'] = 'export default {};'; + } + for (const modulePath of modulesToIgnore) { shimsObj[modulePath] = 'export default {}'; } diff --git a/clients/devtools/devtools-entry.js b/clients/devtools/devtools-entry.js index 74c231482244..09d1ab9edee6 100644 --- a/clients/devtools/devtools-entry.js +++ b/clients/devtools/devtools-entry.js @@ -14,6 +14,7 @@ import lighthouse, {navigation, startTimespan, snapshot} from '../../core/index. import {lookupLocale} from '../../core/lib/i18n/i18n.js'; import {registerLocaleData, getCanonicalLocales} from '../../shared/localization/format.js'; import * as constants from '../../core/config/constants.js'; +import thirdPartyWeb from '../../core/lib/third-party-web.js'; // Rollup seems to overlook some references to `Buffer`, so it must be made explicit. // (`parseSourceMapFromDataUrl` breaks without this) @@ -68,15 +69,6 @@ function lookupCanonicalLocale(locales) { return lookupLocale(locales, getCanonicalLocales()); } -/** - * TODO: Expose api directly when DevTools usage is updated. - * @param {string} url - * @param {{page: LH.Puppeteer.Page, config?: LH.Config, flags?: LH.Flags}} args - */ -function runLighthouseNavigation(url, {page, ...options}) { - return navigation(page, url, options); -} - /** * TODO: Expose api directly when DevTools usage is updated. * @param {{page: LH.Puppeteer.Page, config?: LH.Config, flags?: LH.Flags}} args @@ -85,21 +77,11 @@ function startLighthouseTimespan({page, ...options}) { return startTimespan(page, options); } -/** - * TODO: Expose api directly when DevTools usage is updated. - * @param {{page: LH.Puppeteer.Page, config?: LH.Config, flags?: LH.Flags}} args - */ -function runLighthouseSnapshot({page, ...options}) { - return snapshot(page, options); -} - // Expose only in DevTools' worker if (typeof self !== 'undefined') { // TODO: refactor and delete `global.isDevtools`. global.isDevtools = true; - // @ts-expect-error - self.runLighthouseNavigation = runLighthouseNavigation; // @ts-expect-error self.navigation = navigation; // @ts-expect-error @@ -119,6 +101,8 @@ if (typeof self !== 'undefined') { // TODO: expose as lookupCanonicalLocale in LighthouseService.ts? // @ts-expect-error self.lookupLocale = lookupCanonicalLocale; + // @ts-expect-error + self.thirdPartyWeb = thirdPartyWeb; } else { // For the bundle smoke test. // @ts-expect-error diff --git a/core/lib/third-party-web.js b/core/lib/third-party-web.js index b144e0fd7855..27fb3dc22ed8 100644 --- a/core/lib/third-party-web.js +++ b/core/lib/third-party-web.js @@ -4,7 +4,18 @@ * SPDX-License-Identifier: Apache-2.0 */ -import thirdPartyWeb from 'third-party-web/nostats-subset.js'; +import thirdPartyWeb_ from 'third-party-web/nostats-subset.js'; + +let thirdPartyWeb = thirdPartyWeb_; + +/** + * For use by DevTools. + * + * @param {typeof import('third-party-web/nostats-subset.js')} providedThirdPartyWeb + */ +function provideThirdPartyWeb(providedThirdPartyWeb) { + thirdPartyWeb = providedThirdPartyWeb; +} /** @typedef {import("third-party-web").IEntity} ThirdPartyEntity */ /** @typedef {import("third-party-web").IProduct} ThirdPartyProduct */ @@ -45,6 +56,7 @@ function isFirstParty(url, mainDocumentEntity) { } export default { + provideThirdPartyWeb, getEntity, getProduct, isThirdParty, From 85e5d20d0f5a288eea2424ad63845adfa7582847 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 23 Aug 2024 18:21:02 -0700 Subject: [PATCH 2/3] lint --- clients/devtools/devtools-entry.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/clients/devtools/devtools-entry.js b/clients/devtools/devtools-entry.js index 09d1ab9edee6..4be54758fc07 100644 --- a/clients/devtools/devtools-entry.js +++ b/clients/devtools/devtools-entry.js @@ -69,14 +69,6 @@ function lookupCanonicalLocale(locales) { return lookupLocale(locales, getCanonicalLocales()); } -/** - * TODO: Expose api directly when DevTools usage is updated. - * @param {{page: LH.Puppeteer.Page, config?: LH.Config, flags?: LH.Flags}} args - */ -function startLighthouseTimespan({page, ...options}) { - return startTimespan(page, options); -} - // Expose only in DevTools' worker if (typeof self !== 'undefined') { // TODO: refactor and delete `global.isDevtools`. @@ -85,12 +77,8 @@ if (typeof self !== 'undefined') { // @ts-expect-error self.navigation = navigation; // @ts-expect-error - self.startLighthouseTimespan = startLighthouseTimespan; - // @ts-expect-error self.startTimespan = startTimespan; // @ts-expect-error - self.runLighthouseSnapshot = runLighthouseSnapshot; - // @ts-expect-error self.snapshot = snapshot; // @ts-expect-error self.createConfig = createConfig; From dd6535d70856445df5f6b77b99217c7b68323cde Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Tue, 3 Sep 2024 12:19:34 -0700 Subject: [PATCH 3/3] fix test-bundle --- cli/test/smokehouse/lighthouse-runners/bundle.js | 6 ++++++ clients/devtools/devtools-entry.js | 2 ++ core/lib/third-party-web.js | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cli/test/smokehouse/lighthouse-runners/bundle.js b/cli/test/smokehouse/lighthouse-runners/bundle.js index b8ca0be49387..1f1ffe355038 100644 --- a/cli/test/smokehouse/lighthouse-runners/bundle.js +++ b/cli/test/smokehouse/lighthouse-runners/bundle.js @@ -18,6 +18,7 @@ import {once} from 'events'; import puppeteer from 'puppeteer-core'; import * as ChromeLauncher from 'chrome-launcher'; +import thirdPartyWebLib from 'third-party-web/nostats-subset.js'; import {LH_ROOT} from '../../../../shared/root.js'; import {loadArtifacts, saveArtifacts} from '../../../../core/lib/asset-saver.js'; @@ -73,6 +74,11 @@ async function runBundledLighthouse(url, config, testRunnerOptions) { // @ts-expect-error - not worth giving test global an actual type. const lighthouse = global.runBundledLighthouse; + /** @type {import('../../../../core/lib/third-party-web.js')['default']} */ + // @ts-expect-error + const thirdPartyWeb = global.thirdPartyWeb; + thirdPartyWeb.provideThirdPartyWeb(thirdPartyWebLib); + // Launch and connect to Chrome. const launchedChrome = await ChromeLauncher.launch({ chromeFlags: [ diff --git a/clients/devtools/devtools-entry.js b/clients/devtools/devtools-entry.js index 4be54758fc07..3839aa1f8440 100644 --- a/clients/devtools/devtools-entry.js +++ b/clients/devtools/devtools-entry.js @@ -95,4 +95,6 @@ if (typeof self !== 'undefined') { // For the bundle smoke test. // @ts-expect-error global.runBundledLighthouse = lighthouse; + // @ts-expect-error + global.thirdPartyWeb = thirdPartyWeb; } diff --git a/core/lib/third-party-web.js b/core/lib/third-party-web.js index 27fb3dc22ed8..32c76053654c 100644 --- a/core/lib/third-party-web.js +++ b/core/lib/third-party-web.js @@ -4,9 +4,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -import thirdPartyWeb_ from 'third-party-web/nostats-subset.js'; +import thirdPartyWebLib from 'third-party-web/nostats-subset.js'; -let thirdPartyWeb = thirdPartyWeb_; +let thirdPartyWeb = thirdPartyWebLib; /** * For use by DevTools.