Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients(devtools): require third-party-web to be provided #16166

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build/build-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}';
}
Expand Down
6 changes: 6 additions & 0 deletions cli/test/smokehouse/lighthouse-runners/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -74,6 +75,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: [
Expand Down
36 changes: 5 additions & 31 deletions clients/devtools/devtools-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -68,47 +69,16 @@ 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);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive by removal of unused exports

/**
* 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);
}

/**
* 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
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;
Expand All @@ -119,8 +89,12 @@ 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
global.runBundledLighthouse = lighthouse;
// @ts-expect-error
global.thirdPartyWeb = thirdPartyWeb;
}
14 changes: 13 additions & 1 deletion core/lib/third-party-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
* 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 = thirdPartyWebLib;

/**
* 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 */
Expand Down Expand Up @@ -45,6 +56,7 @@ function isFirstParty(url, mainDocumentEntity) {
}

export default {
provideThirdPartyWeb,
getEntity,
getProduct,
isThirdParty,
Expand Down
Loading