Skip to content

Commit

Permalink
core(script-treemap-data): default config (#12494)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored May 18, 2021
1 parent 76e2189 commit 5bf653d
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 48 deletions.
3 changes: 3 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Object {
Object {
"path": "full-page-screenshot",
},
Object {
"path": "script-treemap-data",
},
Object {
"path": "manual/pwa-cross-browser",
},
Expand Down
9 changes: 4 additions & 5 deletions lighthouse-core/audits/script-treemap-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,12 @@ class ScriptTreemapDataAudit extends Audit {
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const treemapData = await ScriptTreemapDataAudit.makeNodes(artifacts, context);
const nodes = await ScriptTreemapDataAudit.makeNodes(artifacts, context);

// TODO: when out of experimental should make a new detail type.
/** @type {LH.Audit.Details.DebugData} */
/** @type {LH.Audit.Details.TreemapData} */
const details = {
type: 'debugdata',
treemapData,
type: 'treemap-data',
nodes,
};

return {
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ const defaultConfig = {
'valid-source-maps',
'preload-lcp-image',
'full-page-screenshot',
'script-treemap-data',
'manual/pwa-cross-browser',
'manual/pwa-page-transitions',
'manual/pwa-each-page-has-url',
Expand Down
1 change: 0 additions & 1 deletion lighthouse-core/config/experimental-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const config = {
audits: [
'autocomplete',
'large-javascript-libraries',
'script-treemap-data',
'csp-xss',
],
categories: {
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/report/html/renderer/details-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DetailsRenderer {
case 'screenshot':
case 'debugdata':
case 'full-page-screenshot':
case 'treemap-data':
return null;

default: {
Expand Down
10 changes: 6 additions & 4 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ class ReportUIFeatures {
* @param {{text: string, icon?: string, onClick: () => void}} opts
*/
addButton(opts) {
const metricsEl = this._dom.find('.lh-audit-group--metrics', this._document);
const metricsEl = this._document.querySelector('.lh-audit-group--metrics');
// Not supported without metrics group.
if (!metricsEl) return;

const classes = [
'lh-button',
];
Expand Down Expand Up @@ -546,9 +549,8 @@ class ReportUIFeatures {
* @param {LH.Result} json
*/
static openTreemap(json) {
const treemapDebugData = /** @type {LH.Audit.Details.DebugData} */ (
json.audits['script-treemap-data'].details);
if (!treemapDebugData) {
const treemapData = json.audits['script-treemap-data'].details;
if (!treemapData) {
throw new Error('no script treemap data found');
}

Expand Down
5 changes: 0 additions & 5 deletions lighthouse-core/report/html/report-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@
// is in the document.
const features = new ReportUIFeatures(dom);
features.initFeatures(window.__LIGHTHOUSE_JSON__);

// No UI for treemap yet. For now, must run this command in console.
globalThis._tmpFeatures = features;
const command = 'ReportUIFeatures.openTreemap(_tmpFeatures.json);';
console.log(`For treemap viewer, run: ${command}`);
}
window.addEventListener('DOMContentLoaded', __initLighthouseReport__);

Expand Down
32 changes: 18 additions & 14 deletions lighthouse-core/test/audits/script-treemap-data-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function generateRecord(url, resourceSize, resourceType) {

describe('ScriptTreemapData audit', () => {
describe('squoosh fixture', () => {
/** @type {LH.Treemap.Node[]} */
/** @type {LH.Audit.Details.TreemapData} */
let treemapData;
beforeAll(async () => {
const context = {computedCache: new Map()};
Expand All @@ -52,21 +52,23 @@ describe('ScriptTreemapData audit', () => {
ScriptElements: [{src: scriptUrl, content}, noSourceMapScript],
};
const results = await ScriptTreemapData.audit(artifacts, context);
if (!results.details || results.details.type !== 'treemap-data') {
throw new Error('should not happen.');
}

// @ts-expect-error: Debug data.
treemapData = results.details.treemapData;
treemapData = results.details;
});

it('has nodes', () => {
expect(treemapData.find(s => s.name === 'https://sqoosh.app/no-map-or-usage.js'))
expect(treemapData.nodes.find(s => s.name === 'https://sqoosh.app/no-map-or-usage.js'))
.toMatchInlineSnapshot(`
Object {
"name": "https://sqoosh.app/no-map-or-usage.js",
"resourceBytes": 37,
}
`);

const bundleNode = treemapData.find(s => s.name === 'https://squoosh.app/main-app.js');
const bundleNode = treemapData.nodes.find(s => s.name === 'https://squoosh.app/main-app.js');
// @ts-expect-error
const unmapped = bundleNode.children[0].children.find(m => m.name === '(unmapped)');
expect(unmapped).toMatchInlineSnapshot(`
Expand All @@ -77,13 +79,13 @@ describe('ScriptTreemapData audit', () => {
}
`);

expect(JSON.stringify(treemapData).length).toMatchInlineSnapshot(`6740`);
expect(treemapData).toMatchSnapshot();
expect(JSON.stringify(treemapData.nodes).length).toMatchInlineSnapshot(`6740`);
expect(treemapData.nodes).toMatchSnapshot();
});
});

describe('coursehero fixture', () => {
/** @type {LH.Treemap.Node[]} */
/** @type {LH.Audit.Details.TreemapData} */
let treemapData;
beforeAll(async () => {
const context = {computedCache: new Map()};
Expand All @@ -106,20 +108,22 @@ describe('ScriptTreemapData audit', () => {
ScriptElements: [{src: scriptUrl1, content}, {src: scriptUrl2, content}],
};
const results = await ScriptTreemapData.audit(artifacts, context);
if (!results.details || results.details.type !== 'treemap-data') {
throw new Error('should not happen.');
}

// @ts-expect-error: Debug data.
treemapData = results.details.treemapData;
treemapData = results.details;
});

it('has nodes', () => {
expect(JSON.stringify(treemapData).length).toMatchInlineSnapshot(`86817`);
expect(treemapData).toMatchSnapshot();
expect(JSON.stringify(treemapData.nodes).length).toMatchInlineSnapshot(`86817`);
expect(treemapData.nodes).toMatchSnapshot();
});

it('finds duplicates', () => {
expect(JSON.stringify(treemapData).length).toMatchInlineSnapshot(`86817`);
expect(JSON.stringify(treemapData.nodes).length).toMatchInlineSnapshot(`86817`);
// @ts-ignore all these children exist.
const leafNode = treemapData[0].
const leafNode = treemapData.nodes[0].
children[0].
children[0].
children[0].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,13 @@ describe('ReportUIFeatures', () => {

describe('treemap button', () => {
it('should only show button if treemap data is available', () => {
expect(sampleResults.audits['script-treemap-data']).toBeUndefined();
expect(render(sampleResults).querySelector('.lh-button.report-icon--treemap')).toBeNull();
const lhr = JSON.parse(JSON.stringify(sampleResults));

expect(lhr.audits['script-treemap-data']).not.toBeUndefined();
expect(render(lhr).querySelector('.lh-button.report-icon--treemap')).toBeTruthy();

sampleResults.audits['script-treemap-data'] = {details: {}};
expect(render(sampleResults).querySelector('.lh-button.report-icon--treemap')).toBeTruthy();
delete lhr.audits['script-treemap-data'];
expect(render(lhr).querySelector('.lh-button.report-icon--treemap')).toBeNull();
});
});

Expand Down
54 changes: 54 additions & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,42 @@
}
}
},
"script-treemap-data": {
"id": "script-treemap-data",
"title": "Script Treemap Data",
"description": "Used for treemap app",
"score": null,
"scoreDisplayMode": "informative",
"details": {
"type": "treemap-data",
"nodes": [
{
"name": "http://localhost:10200/dobetterweb/dbw_tester.html",
"resourceBytes": 5806
},
{
"name": "http://localhost:10200/dobetterweb/dbw_tester.js",
"resourceBytes": 48
},
{
"name": "http://localhost:10200/dobetterweb/empty_module.js?delay=500",
"resourceBytes": 60
},
{
"name": "http://localhost:10200/dobetterweb/fcp-delayer.js?delay=5000",
"resourceBytes": 60
},
{
"name": "http://localhost:10200/zone.js",
"resourceBytes": 30
},
{
"name": "http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js",
"resourceBytes": 63
}
]
}
},
"pwa-cross-browser": {
"id": "pwa-cross-browser",
"title": "Site works cross-browser",
Expand Down Expand Up @@ -6396,6 +6432,24 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:script-treemap-data",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:JSBundles",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:ModuleDuplication",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:pwa-cross-browser",
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-treemap/app/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -12077,8 +12077,8 @@
"score": null,
"scoreDisplayMode": "informative",
"details": {
"type": "debugdata",
"treemapData": [
"type": "treemap-data",
"nodes": [
{
"name": "https://www.coursehero.com/",
"resourceBytes": 69863
Expand Down
14 changes: 5 additions & 9 deletions lighthouse-treemap/app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ class TreemapViewer {
* @param {HTMLElement} el
*/
constructor(options, el) {
const treemapDebugData = /** @type {LH.Audit.Details.DebugData} */ (
options.lhr.audits['script-treemap-data'].details);
if (!treemapDebugData || !treemapDebugData.treemapData) {
const scriptTreemapData = options.lhr.audits['script-treemap-data'].details;
if (!scriptTreemapData || scriptTreemapData.type !== 'treemap-data') {
throw new Error('missing script-treemap-data');
}

/** @type {LH.Treemap.Node[]} */
const scriptNodes = treemapDebugData.treemapData;

/** @type {{[group: string]: LH.Treemap.Node[]}} */
this.depthOneNodesByGroup = {
scripts: scriptNodes,
scripts: scriptTreemapData.nodes,
};

/**
Expand Down Expand Up @@ -76,7 +72,7 @@ class TreemapViewer {
this.viewModes;
/** @type {RenderState=} */
this.previousRenderState;
/** @type {WeakMap<HTMLElement, LH.Treemap.Node>} */
/** @type {WeakMap<HTMLElement, NodeWithElement>} */
this.tableRowToNodeMap = new WeakMap();
/** @type {WebTreeMap} */
this.treemap;
Expand Down Expand Up @@ -408,7 +404,7 @@ class TreemapViewer {
const tableEl = TreemapUtil.find('.lh-table');
tableEl.innerHTML = '';

/** @type {Array<{node: LH.Treemap.Node, name: string, bundleNode?: LH.Treemap.Node, resourceBytes: number, unusedBytes?: number}>} */
/** @type {Array<{node: NodeWithElement, name: string, bundleNode?: LH.Treemap.Node, resourceBytes: number, unusedBytes?: number}>} */
const data = [];
TreemapUtil.walk(this.currentTreemapRoot, (node, path) => {
if (node.children) return;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-treemap/app/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class TreemapUtil {
/**
* @param {LH.Treemap.Node} node
* @param {(node: LH.Treemap.Node, path: string[]) => void} fn
* @param {(node: NodeWithElement, path: string[]) => void} fn
* @param {string[]=} path
*/
static walk(node, fn, path) {
Expand Down
5 changes: 5 additions & 0 deletions lighthouse-treemap/types/treemap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ declare global {
viewMode: LH.Treemap.ViewMode;
}

interface NodeWithElement extends LH.Treemap.Node {
/** webtreemap adds dom to node data. */
dom?: HTMLElement;
}

var webtreemap: {
TreeMap: typeof WebTreeMap;
render(el: HTMLElement, data: any, options: WebTreeMapOptions): void;
Expand Down
6 changes: 6 additions & 0 deletions types/audit-details.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare global {
export type Details =
Details.CriticalRequestChain |
Details.DebugData |
Details.TreemapData |
Details.Filmstrip |
Details.List |
Details.Opportunity |
Expand Down Expand Up @@ -97,6 +98,11 @@ declare global {
[p: string]: any;
}

export interface TreemapData {
type: 'treemap-data';
nodes: LH.Treemap.Node[];
}

/** String enum of possible types of values found within table items. */
type ItemValueType = 'bytes' | 'code' | 'link' | 'ms' | 'multi' | 'node' | 'source-location' | 'numeric' | 'text' | 'thumbnail' | 'timespanMs' | 'url';

Expand Down
2 changes: 1 addition & 1 deletion types/audit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ declare global {
/** The unit of `numericValue`, used when the consumer wishes to convert numericValue to a display string. */
numericUnit?: string;
/** Extra information about the page provided by some types of audits, in one of several possible forms that can be rendered in the HTML report. */
details?: FormattedIcu<Audit.Details>;
details?: FormattedIcu<LH.Audit.Details>;
}

export interface Results {
Expand Down
2 changes: 0 additions & 2 deletions types/treemap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ declare global {
/** If present, this module is a duplicate. String is normalized source path. See ModuleDuplication.normalizeSource */
duplicatedNormalizedModuleName?: string;
children?: Node[];
/** Added by webtreemap lib. */
dom?: HTMLElement;
}
}
}
Expand Down

0 comments on commit 5bf653d

Please sign in to comment.