Skip to content

Commit

Permalink
core: remove util.cjs (#14703)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Jan 25, 2023
1 parent 0c1c381 commit 7351602
Show file tree
Hide file tree
Showing 89 changed files with 3,322 additions and 3,250 deletions.
5 changes: 0 additions & 5 deletions .codecov.yml
Expand Up @@ -20,8 +20,3 @@ coverage:
patch:
default:
if_not_found: success

# docs:
# https://docs.codecov.com/docs/ignoring-paths
ignore:
- "core/util.cjs" # file is a copy of report/renderer/util.js
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -59,7 +59,6 @@ jobs:
- run: yarn test-legacy-javascript
- run: yarn i18n:checks
- run: yarn dogfood-lhci
- run: bash core/scripts/copy-util-commonjs.sh

# Fail if any changes were written to any source files or generated untracked files (ex, from: build/build-cdt-lib.js).
- run: git add -A && git diff --cached --exit-code
Expand Down
2 changes: 1 addition & 1 deletion core/audits/audit.js
Expand Up @@ -7,7 +7,7 @@
import * as LH from '../../types/lh.js';
import {isUnderTest} from '../lib/lh-env.js';
import * as statistics from '../lib/statistics.js';
import {Util} from '../util.cjs';
import {Util} from '../../shared/util.js';

const DEFAULT_PASS = 'defaultPass';

Expand Down
2 changes: 1 addition & 1 deletion core/computed/resource-summary.js
Expand Up @@ -9,7 +9,7 @@ import {makeComputedArtifact} from './computed-artifact.js';
import {NetworkRecords} from './network-records.js';
import {NetworkRequest} from '../lib/network-request.js';
import {Budget} from '../config/budget.js';
import {Util} from '../util.cjs';
import {Util} from '../../shared/util.js';

/** @typedef {{count: number, resourceSize: number, transferSize: number}} ResourceEntry */

Expand Down
2 changes: 1 addition & 1 deletion core/lib/url-utils.js
Expand Up @@ -4,7 +4,7 @@
* 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.
*/

import {Util} from '../util.cjs';
import {Util} from '../../shared/util.js';
import {LighthouseError} from './lh-error.js';

/** @typedef {import('./network-request.js').NetworkRequest} NetworkRequest */
Expand Down
20 changes: 0 additions & 20 deletions core/scripts/copy-util-commonjs.sh

This file was deleted.

2 changes: 1 addition & 1 deletion core/scripts/i18n/collect-strings.js
Expand Up @@ -18,7 +18,7 @@ import MessageParser from 'intl-messageformat-parser';
import esMain from 'es-main';
import isDeepEqual from 'lodash/isEqual.js';

import {Util} from '../../util.cjs';
import {Util} from '../../../shared/util.js';
import {collectAndBakeCtcStrings} from './bake-ctc-to-lhl.js';
import {pruneObsoleteLhlMessages} from './prune-obsolete-lhl-messages.js';
import {countTranslatedMessages} from './count-translated.js';
Expand Down
2 changes: 1 addition & 1 deletion flow-report/src/common.tsx
Expand Up @@ -9,7 +9,7 @@ import {useEffect, useState} from 'preact/hooks';

import {NavigationIcon, SnapshotIcon, TimespanIcon} from './icons';
import {getFilmstripFrames, getScreenDimensions} from './util';
import {Util} from '../../report/renderer/util.js';
import {Util} from '../../shared/util.js';

const ANIMATION_FRAME_DURATION_MS = 500;

Expand Down
33 changes: 17 additions & 16 deletions flow-report/src/i18n/i18n.tsx
Expand Up @@ -12,11 +12,12 @@ import {I18nFormatter} from '../../../report/renderer/i18n-formatter';
import {UIStrings} from './ui-strings';
import {useFlowResult} from '../util';
import strings from './localized-strings.js';
import {Util} from '../../../report/renderer/util';
import {UIStrings as ReportUIStrings} from '../../../report/renderer/report-utils.js';
import {Globals} from '../../../report/renderer/report-globals.js';

const I18nContext = createContext({
formatter: new I18nFormatter('en-US'),
strings: {...Util.UIStrings, ...UIStrings},
strings: {...ReportUIStrings, ...UIStrings},
});

function useLhrLocale() {
Expand Down Expand Up @@ -54,23 +55,23 @@ const I18nProvider: FunctionComponent = ({children}) => {
const {locale, lhrStrings} = useLhrLocale();

const i18n = useMemo(() => {
Util.applyStrings({
// Preload with strings from the first lhr.
// Used for legacy report components imported into the flow report.
...lhrStrings,
// Set any missing flow strings to default (english) values.
...UIStrings,
// `strings` is generated in build/build-report.js
...strings[locale],
Globals.apply({
providedStrings: {
// Preload with strings from the first lhr.
// Used for legacy report components imported into the flow report.
...lhrStrings,
// Set any missing flow strings to default (english) values.
...UIStrings,
// `strings` is generated in build/build-report.js
...strings[locale],
},
i18n: new I18nFormatter(locale),
reportJson: null,
});

// Initialize renderer util i18n for strings rendered in wrapped components.
// TODO: Don't attach global formatter to `Util`.
Util.i18n = new I18nFormatter(locale);

return {
formatter: Util.i18n,
strings: Util.strings as typeof UIStrings & typeof Util.UIStrings,
formatter: Globals.i18n,
strings: Globals.strings as typeof UIStrings & typeof ReportUIStrings,
};
}, [locale, lhrStrings]);

Expand Down
4 changes: 2 additions & 2 deletions flow-report/src/sidebar/sidebar.tsx
Expand Up @@ -6,7 +6,7 @@

import {FunctionComponent} from 'preact';

import {Util} from '../../../report/renderer/util';
import {ReportUtils} from '../../../report/renderer/report-utils.js';
import {Separator} from '../common';
import {useI18n, useLocalizedStrings} from '../i18n/i18n';
import {CpuIcon, EnvIcon, NetworkIcon, SummaryIcon} from '../icons';
Expand Down Expand Up @@ -34,7 +34,7 @@ const SidebarSummary: FunctionComponent = () => {
const SidebarRuntimeSettings: FunctionComponent<{settings: LH.ConfigSettings}> =
({settings}) => {
const strings = useLocalizedStrings();
const env = Util.getEmulationDescriptions(settings);
const env = ReportUtils.getEmulationDescriptions(settings);
const deviceEmulationString = env.screenEmulation ?
`${env.deviceEmulation} - ${env.screenEmulation}` :
env.deviceEmulation;
Expand Down
12 changes: 6 additions & 6 deletions flow-report/src/summary/category.tsx
Expand Up @@ -6,7 +6,7 @@

import {FunctionComponent} from 'preact';

import {Util} from '../../../report/renderer/util';
import {ReportUtils} from '../../../report/renderer/report-utils.js';
import {Separator} from '../common';
import {CategoryScore} from '../wrappers/category-score';
import {useI18n, useStringFormatter, useLocalizedStrings} from '../i18n/i18n';
Expand Down Expand Up @@ -48,7 +48,7 @@ function getOverallSavings(audit: LH.ReportResult.AuditRef): number {
}

const SummaryTooltipAudit: FunctionComponent<{audit: LH.ReportResult.AuditRef}> = ({audit}) => {
const rating = Util.calculateRating(audit.result.score, audit.result.scoreDisplayMode);
const rating = ReportUtils.calculateRating(audit.result.score, audit.result.scoreDisplayMode);
return (
<div className={`SummaryTooltipAudit SummaryTooltipAudit--${rating}`}>
<Markdown text={audit.result.title}/>
Expand All @@ -69,7 +69,7 @@ const SummaryTooltipAudits: FunctionComponent<{category: LH.ReportResult.Categor
// We don't want unweighted audits except for opportunities with potential savings.
(audit.weight > 0 || getOverallSavings(audit) > 0) &&
// Passing audits should never be high impact.
!Util.showAsPassed(audit.result);
!ReportUtils.showAsPassed(audit.result);
}

const audits = category.auditRefs
Expand Down Expand Up @@ -107,14 +107,14 @@ const SummaryTooltip: FunctionComponent<{
numPassableAudits,
numInformative,
totalWeight,
} = Util.calculateCategoryFraction(category);
} = ReportUtils.calculateCategoryFraction(category);

const i18n = useI18n();
const displayAsFraction = Util.shouldDisplayAsFraction(gatherMode);
const displayAsFraction = ReportUtils.shouldDisplayAsFraction(gatherMode);
const score = displayAsFraction ?
numPassed / numPassableAudits :
category.score;
const rating = score === null ? 'error' : Util.calculateRating(score);
const rating = score === null ? 'error' : ReportUtils.calculateRating(score);

return (
<div className="SummaryTooltip">
Expand Down
4 changes: 2 additions & 2 deletions flow-report/src/summary/summary.tsx
Expand Up @@ -9,7 +9,7 @@ import {useMemo} from 'preact/hooks';

import {FlowSegment, FlowStepThumbnail, Separator} from '../common';
import {getModeDescription, useFlowResult} from '../util';
import {Util} from '../../../report/renderer/util';
import {ReportUtils} from '../../../report/renderer/report-utils.js';
import {SummaryCategory} from './category';
import {useStringFormatter, useLocalizedStrings} from '../i18n/i18n';

Expand Down Expand Up @@ -49,7 +49,7 @@ const SummaryFlowStep: FunctionComponent<{
label: string,
hashIndex: number,
}> = ({lhr, label, hashIndex}) => {
const reportResult = useMemo(() => Util.prepareReportResult(lhr), [lhr]);
const reportResult = useMemo(() => ReportUtils.prepareReportResult(lhr), [lhr]);
const strings = useLocalizedStrings();
const modeDescription = getModeDescription(lhr.gatherMode, strings);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"build-smokehouse-bundle": "node ./build/build-smokehouse-bundle.js",
"build-lr": "yarn reset-link && node ./build/build-lightrider-bundles.js",
"build-pack": "bash build/build-pack.sh",
"build-report": "node build/build-report-components.js && bash core/scripts/copy-util-commonjs.sh && node build/build-report.js",
"build-report": "node build/build-report-components.js && node build/build-report.js",
"build-sample-reports": "yarn build-report && node build/build-sample-reports.js",
"build-treemap": "node ./build/build-treemap.js",
"build-viewer": "node ./build/build-viewer.js",
Expand Down
36 changes: 19 additions & 17 deletions report/renderer/category-renderer.js
Expand Up @@ -20,7 +20,8 @@
/** @typedef {import('./details-renderer.js').DetailsRenderer} DetailsRenderer */
/** @typedef {'failed'|'warning'|'manual'|'passed'|'notApplicable'} TopLevelClumpId */

import {Util} from './util.js';
import {ReportUtils} from './report-utils.js';
import {Globals} from './report-globals.js';

export class CategoryRenderer {
/**
Expand All @@ -39,10 +40,10 @@ export class CategoryRenderer {
*/
get _clumpTitles() {
return {
warning: Util.strings.warningAuditsGroupTitle,
manual: Util.strings.manualAuditsGroupTitle,
passed: Util.strings.passedAuditsGroupTitle,
notApplicable: Util.strings.notApplicableAuditsGroupTitle,
warning: Globals.strings.warningAuditsGroupTitle,
manual: Globals.strings.manualAuditsGroupTitle,
passed: Globals.strings.passedAuditsGroupTitle,
notApplicable: Globals.strings.notApplicableAuditsGroupTitle,
};
}

Expand All @@ -62,7 +63,7 @@ export class CategoryRenderer {
* @return {!Element}
*/
populateAuditValues(audit, component) {
const strings = Util.strings;
const strings = Globals.strings;
const auditEl = this.dom.find('.lh-audit', component);
auditEl.id = audit.result.id;
const scoreDisplayMode = audit.result.scoreDisplayMode;
Expand Down Expand Up @@ -185,7 +186,7 @@ export class CategoryRenderer {
* @return {!Element}
*/
_setRatingClass(element, score, scoreDisplayMode) {
const rating = Util.calculateRating(score, scoreDisplayMode);
const rating = ReportUtils.calculateRating(score, scoreDisplayMode);
element.classList.add(`lh-audit--${scoreDisplayMode.toLowerCase()}`);
if (scoreDisplayMode !== 'informative') {
element.classList.add(`lh-audit--${rating}`);
Expand Down Expand Up @@ -334,8 +335,8 @@ export class CategoryRenderer {
el.append(descriptionEl);
}

this.dom.find('.lh-clump-toggletext--show', el).textContent = Util.strings.show;
this.dom.find('.lh-clump-toggletext--hide', el).textContent = Util.strings.hide;
this.dom.find('.lh-clump-toggletext--show', el).textContent = Globals.strings.show;
this.dom.find('.lh-clump-toggletext--hide', el).textContent = Globals.strings.hide;

clumpElement.classList.add(`lh-clump--${clumpId.toLowerCase()}`);
return el;
Expand All @@ -349,7 +350,7 @@ export class CategoryRenderer {
*/
renderCategoryScore(category, groupDefinitions, options) {
let categoryScore;
if (options && Util.shouldDisplayAsFraction(options.gatherMode)) {
if (options && ReportUtils.shouldDisplayAsFraction(options.gatherMode)) {
categoryScore = this.renderCategoryFraction(category);
} else {
categoryScore = this.renderScoreGauge(category, groupDefinitions);
Expand Down Expand Up @@ -377,7 +378,7 @@ export class CategoryRenderer {
const tmpl = this.dom.createComponent('gauge');
const wrapper = this.dom.find('a.lh-gauge__wrapper', tmpl);

if (Util.isPluginCategory(category.id)) {
if (ReportUtils.isPluginCategory(category.id)) {
wrapper.classList.add('lh-gauge__wrapper--plugin');
}

Expand All @@ -393,16 +394,16 @@ export class CategoryRenderer {
percentageEl.textContent = scoreOutOf100.toString();
if (category.score === null) {
percentageEl.textContent = '?';
percentageEl.title = Util.strings.errorLabel;
percentageEl.title = Globals.strings.errorLabel;
}

// Render a numerical score if the category has applicable audits, or no audits whatsoever.
if (category.auditRefs.length === 0 || this.hasApplicableAudits(category)) {
wrapper.classList.add(`lh-gauge__wrapper--${Util.calculateRating(category.score)}`);
wrapper.classList.add(`lh-gauge__wrapper--${ReportUtils.calculateRating(category.score)}`);
} else {
wrapper.classList.add(`lh-gauge__wrapper--not-applicable`);
percentageEl.textContent = '-';
percentageEl.title = Util.strings.notApplicableAuditsGroupTitle;
percentageEl.title = Globals.strings.notApplicableAuditsGroupTitle;
}

this.dom.find('.lh-gauge__label', tmpl).textContent = category.title;
Expand All @@ -417,15 +418,16 @@ export class CategoryRenderer {
const tmpl = this.dom.createComponent('fraction');
const wrapper = this.dom.find('a.lh-fraction__wrapper', tmpl);

const {numPassed, numPassableAudits, totalWeight} = Util.calculateCategoryFraction(category);
const {numPassed, numPassableAudits, totalWeight} =
ReportUtils.calculateCategoryFraction(category);

const fraction = numPassed / numPassableAudits;
const content = this.dom.find('.lh-fraction__content', tmpl);
const text = this.dom.createElement('span');
text.textContent = `${numPassed}/${numPassableAudits}`;
content.append(text);

let rating = Util.calculateRating(fraction);
let rating = ReportUtils.calculateRating(fraction);

// If none of the available audits can affect the score, a rating isn't useful.
// The flow report should display the fraction with neutral icon and coloring in this case.
Expand Down Expand Up @@ -490,7 +492,7 @@ export class CategoryRenderer {
return scoreDisplayMode;
}

if (Util.showAsPassed(auditRef.result)) {
if (ReportUtils.showAsPassed(auditRef.result)) {
if (this._auditHasWarning(auditRef)) {
return 'warning';
} else {
Expand Down
12 changes: 6 additions & 6 deletions report/renderer/crc-details-renderer.js
Expand Up @@ -20,7 +20,7 @@
* critical request chains network tree.
*/

import {Util} from './util.js';
import {Globals} from './report-globals.js';

/** @typedef {import('./dom.js').DOM} DOM */
/** @typedef {import('./details-renderer.js').DetailsRenderer} DetailsRenderer */
Expand Down Expand Up @@ -137,9 +137,9 @@ class CriticalRequestChainRenderer {
const {startTime, endTime, transferSize} = segment.node.request;
const span = dom.createElement('span', 'lh-crc-node__chain-duration');
span.textContent =
' - ' + Util.i18n.formatMilliseconds((endTime - startTime) * 1000) + ', ';
' - ' + Globals.i18n.formatMilliseconds((endTime - startTime) * 1000) + ', ';
const span2 = dom.createElement('span', 'lh-crc-node__chain-duration');
span2.textContent = Util.i18n.formatBytesToKiB(transferSize, 0.01);
span2.textContent = Globals.i18n.formatBytesToKiB(transferSize, 0.01);

treevalEl.append(span, span2);
}
Expand Down Expand Up @@ -178,11 +178,11 @@ class CriticalRequestChainRenderer {
const containerEl = dom.find('.lh-crc', tmpl);

// Fill in top summary.
dom.find('.lh-crc-initial-nav', tmpl).textContent = Util.strings.crcInitialNavigation;
dom.find('.lh-crc-initial-nav', tmpl).textContent = Globals.strings.crcInitialNavigation;
dom.find('.lh-crc__longest_duration_label', tmpl).textContent =
Util.strings.crcLongestDurationLabel;
Globals.strings.crcLongestDurationLabel;
dom.find('.lh-crc__longest_duration', tmpl).textContent =
Util.i18n.formatMilliseconds(details.longestChain.duration);
Globals.i18n.formatMilliseconds(details.longestChain.duration);

// Construct visual tree.
const root = CRCRenderer.initTree(details.chains);
Expand Down

0 comments on commit 7351602

Please sign in to comment.