-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Changes from 25 commits
60e38ee
9d0326b
f059abd
da84011
c18bd48
349a655
f6003b6
4c2f18f
27e21f4
1bdd6e3
47e6f15
2003340
9df8212
131ef40
a769ffc
a65420a
67d9565
7a1c42b
b971fb3
e0c958e
0cab294
1364af2
51d832b
ffa6ec3
0dc332f
3d02808
2474bd4
bcdc3d8
21fcf51
77a488d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ const {defaultSettings} = require('../config/constants.js'); | |
const lighthouse = require('../index.js'); | ||
const lhr = /** @type {LH.Result} */ (require('../../lighthouse-core/test/results/sample_v2.json')); | ||
const {LH_ROOT} = require('../../root.js'); | ||
const htmlReportAssets = require('../../report/report-assets.js'); | ||
|
||
|
||
const DIST = path.join(LH_ROOT, `dist/now`); | ||
|
||
|
@@ -29,19 +31,23 @@ const DIST = path.join(LH_ROOT, `dist/now`); | |
'ɑrabic': swapLocale(lhr, 'ar').lhr, | ||
'xl-accented': swapLocale(lhr, 'en-XL').lhr, | ||
'error': errorLhr, | ||
'single-category': tweakLhrForPsi(lhr), | ||
}; | ||
|
||
// Generate and write reports | ||
Object.entries(filenameToLhr).forEach(([filename, lhr]) => { | ||
let html = ReportGenerator.generateReportHtml(lhr); | ||
// TODO: PSI is another variant to consider | ||
for (const variant of ['', '⌣.cdt.']) { | ||
for (const variant of ['', '⌣.cdt.', '⌣.psi.']) { | ||
let html = variant.includes('psi') ? | ||
generatePsiReportHtml() : | ||
ReportGenerator.generateReportHtml(lhr); | ||
|
||
if (variant.includes('cdt')) { | ||
// TODO: Make the DevTools Audits panel "emulation" more comprehensive | ||
// - the parent widget/vbox container with overflow | ||
// - a more constrained/realistic default size | ||
html = html.replace(`"lh-root lh-vars"`, `"lh-root lh-vars lh-devtools"`); | ||
} | ||
|
||
const filepath = `${DIST}/${variant}${filename}/index.html`; | ||
fs.mkdirSync(path.dirname(filepath), {recursive: true}); | ||
fs.writeFileSync(filepath, html, {encoding: 'utf-8'}); | ||
|
@@ -50,6 +56,27 @@ const DIST = path.join(LH_ROOT, `dist/now`); | |
}); | ||
})(); | ||
|
||
|
||
/** | ||
* @return {string} | ||
*/ | ||
function generatePsiReportHtml() { | ||
const sanitizedJson = ReportGenerator.sanitizeJson(tweakLhrForPsi(lhr)); | ||
const PSI_TEMPLATE = fs.readFileSync( | ||
`${LH_ROOT}/report/test-assets/faux-psi-template.html`, 'utf8'); | ||
const PSI_JAVASCRIPT = ` | ||
${fs.readFileSync(`${LH_ROOT}/dist/report/psi.js`, 'utf8')}; | ||
${fs.readFileSync(`${LH_ROOT}/report/test-assets/faux-psi.js`, 'utf8')}; | ||
`; | ||
|
||
const html = ReportGenerator.replaceStrings(PSI_TEMPLATE, [ | ||
{search: '%%LIGHTHOUSE_JSON%%', replacement: sanitizedJson}, | ||
{search: '%%LIGHTHOUSE_JAVASCRIPT%%', replacement: PSI_JAVASCRIPT}, | ||
{search: '/*%%LIGHTHOUSE_CSS%%*/', replacement: htmlReportAssets.REPORT_CSS}, | ||
{search: '%%LIGHTHOUSE_TEMPLATES%%', replacement: htmlReportAssets.REPORT_TEMPLATES}, | ||
]); | ||
return html; | ||
} | ||
/** | ||
* Add a plugin to demo plugin rendering. | ||
* @param {LH.Result} lhr | ||
|
@@ -63,6 +90,24 @@ function addPluginCategory(lhr) { | |
}; | ||
} | ||
|
||
/** | ||
* Drops the LHR to only one, solo category (performance), and removes budgets. | ||
* @param {LH.Result} lhr | ||
*/ | ||
function tweakLhrForPsi(lhr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this matter? Shouldn't it just be able to take a regular LHR and just ignore the stuff it doesn't want? Or is this more a matter of giving it the data it will actually get in practice for a more realistic deployment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldnt matter much, but I just wanted to the data to match what PSI would expect to see. I can't think of any real reasons why it'd matter (like full-page-screenshot being in another category or something). |
||
/** @type {LH.Result} */ | ||
const clone = JSON.parse(JSON.stringify(lhr)); | ||
clone.categories = { | ||
'performance': clone.categories.performance, | ||
}; | ||
// no budgets in PSI | ||
delete clone.audits['performance-budget']; | ||
clone.categories.performance.auditRefs = clone.categories.performance.auditRefs.filter(audit => { | ||
return !audit.id.endsWith('-budget'); | ||
}); | ||
return clone; | ||
} | ||
|
||
/** | ||
* Generate an LHR with errors for the renderer to display. | ||
* We'll write an "empty" artifacts file to disk, only to use it in auditMode. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"build-lr": "yarn reset-link && node ./build/build-lightrider-bundles.js", | ||
"build-pack": "bash build/build-pack.sh", | ||
"build-report": "node build/build-report.js", | ||
"build-dist-reports": "yarn build-report && node lighthouse-core/scripts/build-report-for-autodeployment.js", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bike shed: what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehe me and paul were talking about the naming here...
some renaming is in order all around. |
||
"build-treemap": "node ./build/build-treemap.js", | ||
"build-viewer": "node ./build/build-viewer.js", | ||
"reset-link": "(yarn unlink || true) && yarn link && yarn link lighthouse", | ||
|
@@ -32,7 +33,7 @@ | |
"lint": "[ \"$CI\" = true ] && eslint --quiet -f codeframe . || eslint .", | ||
"smoke": "node lighthouse-cli/test/smokehouse/frontends/smokehouse-bin.js", | ||
"debug": "node --inspect-brk ./lighthouse-cli/index.js", | ||
"start": "yarn build-report --only-standalone && node ./lighthouse-cli/index.js", | ||
"start": "yarn build-report --standalone && node ./lighthouse-cli/index.js", | ||
"jest": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js", | ||
"test": "yarn diff:sample-json && yarn lint --quiet && yarn unit && yarn type-check", | ||
"test-bundle": "yarn smoke --runner bundle -j=1 --retries=2 --invert-match forms", | ||
|
@@ -62,7 +63,7 @@ | |
"fast": "node ./lighthouse-cli/index.js --preset=desktop --throttlingMethod=provided", | ||
"deploy-treemap": "yarn build-treemap --deploy", | ||
"deploy-viewer": "yarn build-viewer --deploy", | ||
"now-build": "yarn build-report && node lighthouse-core/scripts/build-report-for-autodeployment.js && yarn build-viewer && yarn build-treemap && cp -r dist/gh-pages dist/now/gh-pages", | ||
"now-build": "yarn build-dist-reports && yarn build-viewer && yarn build-treemap && cp -r dist/gh-pages dist/now/gh-pages", | ||
"dogfood-lhci": "./lighthouse-core/scripts/dogfood-lhci.sh", | ||
"timing-trace": "node lighthouse-core/scripts/generate-timing-trace.js", | ||
"changelog": "conventional-changelog --config ./build/changelog-generator/index.js --infile changelog.md --same-file", | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,15 +28,23 @@ class ReportGenerator { | |||||
} | ||||||
|
||||||
/** | ||||||
* Returns the report HTML as a string with the report JSON and renderer JS inlined. | ||||||
* @param {Object} object | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @return {string} | ||||||
*/ | ||||||
static sanitizeJson(object) { | ||||||
return JSON.stringify(object) | ||||||
.replace(/</g, '\\u003c') // replaces opening script tags | ||||||
.replace(/\u2028/g, '\\u2028') // replaces line separators () | ||||||
.replace(/\u2029/g, '\\u2029'); // replaces paragraph separators | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns the standaloe report HTML as a string with the report JSON and renderer JS inlined. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @param {LH.Result} lhr | ||||||
* @return {string} | ||||||
*/ | ||||||
static generateReportHtml(lhr) { | ||||||
const sanitizedJson = JSON.stringify(lhr) | ||||||
.replace(/</g, '\\u003c') // replaces opening script tags | ||||||
.replace(/\u2028/g, '\\u2028') // replaces line separators () | ||||||
.replace(/\u2029/g, '\\u2029'); // replaces paragraph separators | ||||||
const sanitizedJson = ReportGenerator.sanitizeJson(lhr); | ||||||
|
||||||
return ReportGenerator.replaceStrings(htmlReportAssets.REPORT_TEMPLATE, [ | ||||||
{search: '%%LIGHTHOUSE_JSON%%', replacement: sanitizedJson}, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<!-- | ||
* 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. | ||
--> | ||
|
||
<!-- | ||
This fake psi page is intended to reproduce the experience of rendering two LHRs within the same DOM, | ||
like in PageSpeed Insights. This helps surface the peculiar behavior around tabs, | ||
expectations of unique IDs, and scoped report containment. | ||
--> | ||
|
||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1"> | ||
<title>Faux PageSpeed Report</title> | ||
<style>/*%%LIGHTHOUSE_CSS%%*/</style> | ||
|
||
<style> | ||
/* Inspired by https://codepen.io/markcaron/pen/MvGRYV?editors=1100 but improved. s*/ | ||
/* | ||
The tab visibility basics | ||
*/ | ||
.tabset > input[type="radio"] { | ||
position: absolute; | ||
left: -200vw; | ||
} | ||
|
||
.tabset .tab-panel { | ||
display: none; | ||
} | ||
|
||
.tabset > input:nth-of-type(1):checked ~ .tab-panels > .tab-panel:nth-of-type(1), | ||
.tabset > input:nth-of-type(2):checked ~ .tab-panels > .tab-panel:nth-of-type(2) { | ||
display: block; | ||
} | ||
|
||
/* | ||
Purdy tabs | ||
*/ | ||
.tabset > label { | ||
position: relative; | ||
display: inline-block; | ||
padding: 15px 55px 15px; | ||
border: 1px solid transparent; | ||
border-bottom: 0; | ||
cursor: pointer; | ||
font-weight: 600; | ||
} | ||
|
||
.tabset > label:hover { | ||
background-color: hsl(0deg 0% 100% / 24%); | ||
} | ||
|
||
.tabset > label:hover, | ||
.tabset > input:focus + label { | ||
color: #06c; | ||
} | ||
|
||
.tabset > input:checked + label { | ||
border-color: #ccc; | ||
border-bottom: 1px solid #fff; | ||
margin-bottom: -1px; | ||
background: white; | ||
} | ||
|
||
.tab-panel { | ||
padding: 30px 0; | ||
border-top: 1px solid #ccc; | ||
} | ||
|
||
body { | ||
padding: 30px; | ||
} | ||
|
||
.tabset { | ||
max-width: 65em; | ||
background: #eee; | ||
} | ||
|
||
.tab-panels { | ||
background: white; | ||
} | ||
|
||
/* extra styles */ | ||
body { | ||
background: hsl(217deg 89% 61%); | ||
} | ||
</style> | ||
</head> | ||
<body > | ||
<noscript>PSI requires JavaScript. Please enable.</noscript> | ||
<div hidden>%%LIGHTHOUSE_TEMPLATES%%</div> | ||
|
||
<div class="element-screenshots-container"></div> | ||
|
||
|
||
<div class="tabset lh-vars lh-root"> | ||
|
||
<input type="radio" name="tabset" id="tab1" aria-controls="mobile" checked> | ||
<label for="tab1">Mobile</label> | ||
|
||
<input type="radio" name="tabset" id="tab2" aria-controls="desktop"> | ||
<label for="tab2">Desktop</label> | ||
|
||
<div class="tab-panels"> | ||
<section id="mobile" class="tab-panel"> | ||
<main></main> | ||
</section> | ||
|
||
<section id="desktop" class="tab-panel"> | ||
<main></main> | ||
</section> | ||
|
||
</div> | ||
</div> | ||
|
||
<script>window.__LIGHTHOUSE_JSON__ = %%LIGHTHOUSE_JSON%%;</script> | ||
<script type="module"> | ||
%%LIGHTHOUSE_JAVASCRIPT%% | ||
//# sourceURL=psi-reportrenderer.js | ||
</script> | ||
<script>console.log('window.__LIGHTHOUSE_JSON__', __LIGHTHOUSE_JSON__);</script> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this definitely seems kind of weird in
build-report-for-autodeployment.js
and possibly hard to keep up to date when changes happen toreport-generator
, but I also like keeping it simple like this while we iterate on report/overall build story