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

report: create faux psi report #12815

Merged
merged 30 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
60e38ee
report api. (and psi thinger)
paulirish Jul 9, 2021
9d0326b
cleanup
paulirish Jul 10, 2021
f059abd
'proper' psi variant
paulirish Jul 10, 2021
da84011
Merge remote-tracking branch 'origin/master' into reportapi
paulirish Jul 20, 2021
c18bd48
fixup psi dist
paulirish Jul 20, 2021
349a655
new tabs. distinguished screenshots
paulirish Jul 21, 2021
f6003b6
Merge remote-tracking branch 'origin/master' into reportapi
paulirish Jul 21, 2021
4c2f18f
cleanup psi more
paulirish Jul 21, 2021
27e21f4
report: create faux psi report
paulirish Jul 21, 2021
1bdd6e3
drop types
paulirish Jul 21, 2021
47e6f15
await the buildpsi
paulirish Jul 21, 2021
2003340
no need to doublebuild
paulirish Jul 21, 2021
9df8212
html-report-assets is a runtime dep that depends on a built asset. sq…
paulirish Jul 21, 2021
131ef40
tweak psi lhr
paulirish Jul 21, 2021
a769ffc
revert generateReportHTML changes
paulirish Jul 23, 2021
a65420a
cli flag and other feedback
paulirish Jul 23, 2021
67d9565
move files to test-assets
paulirish Jul 23, 2021
7a1c42b
address remaining feedback
paulirish Jul 23, 2021
b971fb3
adjust build report cmds from feedback
paulirish Aug 3, 2021
e0c958e
Merge remote-tracking branch 'origin/master' into faux-psi
paulirish Aug 3, 2021
0cab294
type-check fixes
paulirish Aug 3, 2021
1364af2
lint
paulirish Aug 3, 2021
51d832b
PrepareLabDataResult
paulirish Aug 3, 2021
ffa6ec3
flags and logs
paulirish Aug 3, 2021
0dc332f
rm watch:report
paulirish Aug 10, 2021
3d02808
brendan's feedback
paulirish Aug 10, 2021
2474bd4
Merge remote-tracking branch 'origin/master' into faux-psi
paulirish Aug 10, 2021
bcdc3d8
Merge remote-tracking branch 'origin/master' into faux-psi
paulirish Aug 11, 2021
21fcf51
Merge remote-tracking branch 'origin/master' into faux-psi
paulirish Aug 11, 2021
77a488d
remove references to LIGHTHOUSE_TEMPLATES as templates.html aint a th…
paulirish Aug 11, 2021
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
9 changes: 8 additions & 1 deletion report/clients/psi.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {PerformanceCategoryRenderer} from '../renderer/performance-category-rend
import {ReportUIFeatures} from '../renderer/report-ui-features.js';
import {Util} from '../renderer/util.js';

/* global window */

/** @typedef {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null, scoreScaleEl: Element, installFeatures: Function}} prepareLabDataData */

/**
* Returns all the elements that PSI needs to render the report
* We expose this helper method to minimize the 'public' API surface of the renderer
Expand All @@ -36,7 +40,7 @@ import {Util} from '../renderer/util.js';
*
* @param {LH.Result | string} LHResult The stringified version of {LH.Result}
* @param {Document} document The host page's window.document
* @return {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null, scoreScaleEl: Element, installFeatures: Function}}
* @return {prepareLabDataData}
*/
export function prepareLabData(LHResult, document) {
const lhResult = (typeof LHResult === 'string') ?
Expand Down Expand Up @@ -147,3 +151,6 @@ function _getFinalScreenshot(perfCategory) {
if (!details || details.type !== 'screenshot') return null;
return details.data;
}

// @ts-expect-error
window.prepareLabData = prepareLabData;
37 changes: 24 additions & 13 deletions report/test-assets/faux-psi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

/** @fileoverview This file is a glorified call of prepareLabData. */

/* global document window prepareLabData */
/* global document window */

(async function __initLighthouseReport__() {
/** @typedef {import('../clients/psi.js').prepareLabDataData} prepareLabDataData */

(async function __initPsiReports__() {
// @ts-expect-error
const mobileLHR = window.__LIGHTHOUSE_JSON__;
const desktopLHR = JSON.parse(JSON.stringify(mobileLHR));

Expand All @@ -21,15 +24,20 @@
for (const [tabId, lhr] of Object.entries(lhrs)) {
await distinguishLHR(lhr, tabId);

// @ts-expect-error
const pldd = /** @type {prepareLabDataData} */ (window.prepareLabData(lhr, document));
const {scoreGaugeEl, perfCategoryEl,
finalScreenshotDataUri, scoreScaleEl, installFeatures} = prepareLabData(lhr, document);
finalScreenshotDataUri, scoreScaleEl, installFeatures} = pldd;

const container = document.querySelector(`#${tabId}`).querySelector('main');
const container = document.querySelector(`#${tabId} main`);
if (!container) throw new Error('Unexpected DOM. Bailing.');
container.append(scoreGaugeEl);
container.append(scoreScaleEl);
const imgEl = document.createElement('img');
imgEl.src = finalScreenshotDataUri;
container.append(imgEl);
if (finalScreenshotDataUri) {
const imgEl = document.createElement('img');
imgEl.src = finalScreenshotDataUri;
container.append(imgEl);
}
container.append(perfCategoryEl);
installFeatures(container);
}
Expand All @@ -39,7 +47,7 @@
/**
* Tweak the LHR to make the desktop and mobile reports easier to identify.
* Adjusted: Perf category name and score, and emoji placed on top of key screenshots.
* @param {LH.Report} lhr
* @param {LH.Result} lhr
* @param {string} tabId
*/
async function distinguishLHR(lhr, tabId) {
Copy link
Member

Choose a reason for hiding this comment

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

can this be combined with what happens in tweakLhrForPsi?

Copy link
Member Author

Choose a reason for hiding this comment

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

i could but then i wouldn't have the single-category dist-report. but i really want that for local report dev. :)

Expand All @@ -48,11 +56,14 @@ async function distinguishLHR(lhr, tabId) {
lhr.categories.performance.score = 0.81;
}

const finalSS = lhr.audits['final-screenshot'].details.data;
lhr.audits['final-screenshot'].details.data = await decorateScreenshot(finalSS, tabId);
const finalSSDetails = lhr.audits['final-screenshot'] && lhr.audits['final-screenshot'].details;
if (!finalSSDetails || finalSSDetails.type !== 'screenshot') throw new Error();
finalSSDetails.data = await decorateScreenshot(finalSSDetails.data, tabId);

const fullPageScreenshot = lhr.audits['full-page-screenshot'].details.screenshot.data;
lhr.audits['full-page-screenshot'].details.screenshot.data = await decorateScreenshot(fullPageScreenshot, tabId); // eslint-disable-line max-len
const fpSSDetails = lhr.audits['full-page-screenshot'] &&
lhr.audits['full-page-screenshot'].details;
if (!fpSSDetails || fpSSDetails.type !== 'full-page-screenshot') throw new Error();
fpSSDetails.screenshot.data = await decorateScreenshot(fpSSDetails.screenshot.data, tabId);
}

/**
Expand All @@ -73,8 +84,8 @@ async function decorateScreenshot(datauri, tabId) {
c.height = img.height;

const ctx = c.getContext('2d');
if (!ctx) throw new Error();
ctx.drawImage(img, 0, 0);
console.log(img.width);
ctx.font = `${img.width / 2}px serif`;
ctx.textAlign = 'center';
ctx.globalAlpha = 0.7;
Expand Down