-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathscript-treemap-data.js
265 lines (228 loc) · 8.9 KB
/
script-treemap-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @license Copyright 2020 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.
*/
'use strict';
/**
* @fileoverview
* Creates nodes for treemap app.
* Example output: lighthouse-treemap/app/debug.json
*/
/**
* @typedef {Omit<LH.Treemap.Node, 'name'|'children'>} SourceData
*/
const Audit = require('./audit.js');
const JsBundles = require('../computed/js-bundles.js');
const UnusedJavaScriptSummary = require('../computed/unused-javascript-summary.js');
const ModuleDuplication = require('../computed/module-duplication.js');
class ScriptTreemapDataAudit extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'script-treemap-data',
scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE,
title: 'Script Treemap Data',
description: 'Used for treemap app',
requiredArtifacts:
['traces', 'devtoolsLogs', 'SourceMaps', 'ScriptElements', 'JsUsage', 'URL'],
};
}
/**
* Returns a tree data structure where leaf nodes are sources (ie. real files from source tree)
* from a source map, and non-leaf nodes are directories. Leaf nodes have data
* for bytes, coverage, etc., when available, and non-leaf nodes have the
* same data as the sum of all descendant leaf nodes.
* @param {string} src
* @param {string} sourceRoot
* @param {Record<string, SourceData>} sourcesData
* @return {LH.Treemap.Node}
*/
static makeScriptNode(src, sourceRoot, sourcesData) {
/**
* @param {string} name
* @return {LH.Treemap.Node}
*/
function newNode(name) {
return {
name,
resourceBytes: 0,
};
}
const sourceRootNode = newNode(sourceRoot);
/**
* Given a slash-delimited path, traverse the Node structure and increment
* the data provided for each node in the chain. Creates nodes as needed.
* Ex: path/to/file.js will find or create "path" on `node`, increment the data fields,
* and continue with "to", and so on.
* @param {string} source
* @param {SourceData} data
*/
function addAllNodesInSourcePath(source, data) {
let node = sourceRootNode;
// Apply the data to the sourceRootNode.
sourceRootNode.resourceBytes += data.resourceBytes;
if (data.unusedBytes) {
sourceRootNode.unusedBytes = (sourceRootNode.unusedBytes || 0) + data.unusedBytes;
}
// Strip off the shared root.
const sourcePathSegments = source.replace(sourceRoot, '').split(/\/+/);
sourcePathSegments.forEach((sourcePathSegment, i) => {
const isLeaf = i === sourcePathSegments.length - 1;
let child = node.children && node.children.find(child => child.name === sourcePathSegment);
if (!child) {
child = newNode(sourcePathSegment);
node.children = node.children || [];
node.children.push(child);
}
node = child;
// Now that we've found or created the next node in the path, apply the data.
node.resourceBytes += data.resourceBytes;
if (data.unusedBytes) node.unusedBytes = (node.unusedBytes || 0) + data.unusedBytes;
// Only leaf nodes might have duplication data.
if (isLeaf && data.duplicatedNormalizedModuleName !== undefined) {
node.duplicatedNormalizedModuleName = data.duplicatedNormalizedModuleName;
}
});
}
// For every source file, apply the data to all components
// of the source path, creating nodes as necessary.
for (const [source, data] of Object.entries(sourcesData)) {
addAllNodesInSourcePath(source, data);
}
/**
* Collapse nodes that have only one child.
* @param {LH.Treemap.Node} node
*/
function collapseAll(node) {
while (node.children && node.children.length === 1) {
node.name += '/' + node.children[0].name;
node.children = node.children[0].children;
}
if (node.children) {
for (const child of node.children) {
collapseAll(child);
}
}
}
collapseAll(sourceRootNode);
// Script node should be just the script src.
const scriptNode = {...sourceRootNode};
scriptNode.name = src;
scriptNode.children = [sourceRootNode];
return scriptNode;
}
/**
* Returns nodes where the first level of nodes are URLs.
* Every external script has a node.
* All inline scripts are combined into a single node.
* If a script has a source map, that node will be set by makeNodeFromSourceMapData.
*
* Example return result:
- index.html (inlines scripts)
- main.js
- - webpack://
- - - react.js
- - - app.js
- i-have-no-map.js
*
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Treemap.Node[]>}
*/
static async makeNodes(artifacts, context) {
/** @type {LH.Treemap.Node[]} */
const nodes = [];
let inlineScriptLength = 0;
for (const scriptElement of artifacts.ScriptElements) {
// No src means script is inline.
// Combine these ScriptElements so that inline scripts show up as a single root node.
if (!scriptElement.src) {
inlineScriptLength += (scriptElement.content || '').length;
}
}
if (inlineScriptLength) {
const name = artifacts.URL.finalUrl;
nodes.push({
name,
resourceBytes: inlineScriptLength,
});
}
const bundles = await JsBundles.request(artifacts, context);
const duplicationByPath = await ModuleDuplication.request(artifacts, context);
for (const scriptElement of artifacts.ScriptElements) {
if (!scriptElement.src) continue;
const name = scriptElement.src;
const bundle = bundles.find(bundle => scriptElement.src === bundle.script.src);
const scriptCoverages = artifacts.JsUsage[scriptElement.src] || [];
if (!bundle && scriptCoverages.length === 0) {
// No bundle and no coverage information, so simply make a single node
// detailing how big the script is.
nodes.push({
name,
resourceBytes: scriptElement.src.length,
});
continue;
}
const unusedJavascriptSummary = await UnusedJavaScriptSummary.request(
{url: scriptElement.src, scriptCoverages, bundle}, context);
/** @type {LH.Treemap.Node} */
let node;
if (bundle && !('errorMessage' in bundle.sizes)) {
// Create nodes for each module in a bundle.
/** @type {Record<string, SourceData>} */
const sourcesData = {};
for (const source of Object.keys(bundle.sizes.files)) {
/** @type {SourceData} */
const sourceData = {
resourceBytes: bundle.sizes.files[source],
};
if (unusedJavascriptSummary.sourcesWastedBytes) {
sourceData.unusedBytes = unusedJavascriptSummary.sourcesWastedBytes[source];
}
// ModuleDuplication uses keys without the source root prepended, but
// bundle.sizes uses keys with it prepended, so we remove the source root before
// using it with duplicationByPath.
let sourceWithoutSourceRoot = source;
if (bundle.rawMap.sourceRoot && source.startsWith(bundle.rawMap.sourceRoot)) {
sourceWithoutSourceRoot = source.replace(bundle.rawMap.sourceRoot, '');
}
const key = ModuleDuplication.normalizeSource(sourceWithoutSourceRoot);
if (duplicationByPath.has(key)) sourceData.duplicatedNormalizedModuleName = key;
sourcesData[source] = sourceData;
}
node = this.makeScriptNode(scriptElement.src, bundle.rawMap.sourceRoot || '', sourcesData);
} else {
// No valid source map for this script, so we can only produce a single node.
node = {
name,
resourceBytes: unusedJavascriptSummary.totalBytes,
unusedBytes: unusedJavascriptSummary.wastedBytes,
};
}
nodes.push(node);
}
return nodes;
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const treemapData = await ScriptTreemapDataAudit.makeNodes(artifacts, context);
// TODO: when out of experimental should make a new detail type.
/** @type {LH.Audit.Details.DebugData} */
const details = {
type: 'debugdata',
treemapData,
};
return {
score: 1,
details,
};
}
}
module.exports = ScriptTreemapDataAudit;