forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-source-maps.js
155 lines (134 loc) · 6.48 KB
/
valid-source-maps.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
/**
* @license Copyright 2020 Google Inc. 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';
const thirdPartyWeb = require('../lib/third-party-web.js');
const Audit = require('./audit.js');
const i18n = require('../lib/i18n/i18n.js');
const UIStrings = {
/** Title of a Lighthouse audit that provides detail on HTTP to HTTPS redirects. This descriptive title is shown to users when HTTP traffic is redirected to HTTPS. */
title: 'Page has valid source maps',
/** Title of a Lighthouse audit that provides detail on HTTP to HTTPS redirects. This descriptive title is shown to users when HTTP traffic is not redirected to HTTPS. */
failureTitle: 'Missing source maps for large first-party JavaScript',
/** Description of a Lighthouse audit that tells the user that their JavaScript source maps are invalid or missing. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */
description: 'Source maps translate minified code to the original source code. This helps ' +
'developers debug in production. In addition, Lighthouse is able to provide further ' +
'insights. Consider deploying source maps to take advantage of these benefits. ' +
'[Learn more](https://developers.google.com/web/tools/chrome-devtools/javascript/source-maps).',
/** Label for a column in a data table. Entries will be URLs to JavaScript source maps. */
columnMapURL: 'Map URL',
/** Label for a possible error message indicating that a source map for a large, first-party JavaScript script is missing. */
missingSourceMapErrorMessage: 'Large JavaScript file is missing a source map',
/** Label for a possible error message indicating that the content of a source map is invalid because it is missing items in the sourcesContent attribute. */
missingSourceMapItemsWarningMesssage: `{missingItems, plural,
=1 {Warning: missing 1 item in \`.sourcesContent\`}
other {Warning: missing # items in \`.sourcesContent\`}
}`,
};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
const LARGE_JS_BYTE_THRESHOLD = 500 * 1024;
class ValidSourceMaps extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'valid-source-maps',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['Scripts', 'SourceMaps', 'URL'],
};
}
/**
* Returns true if the size of the script exceeds a static threshold.
* @param {LH.Artifacts.Script} script
* @param {string} finalURL
* @return {boolean}
*/
static isLargeFirstPartyJS(script, finalURL) {
if (!script.length) return false;
const isLargeJS = script.length >= LARGE_JS_BYTE_THRESHOLD;
const isFirstPartyJS = script.url ?
thirdPartyWeb.isFirstParty(script.url, thirdPartyWeb.getEntity(finalURL)) : false;
return isLargeJS && isFirstPartyJS;
}
/**
* @param {LH.Artifacts} artifacts
*/
static async audit(artifacts) {
const {SourceMaps} = artifacts;
/** @type {Set<string>} */
const isMissingMapForLargeFirstPartyScriptUrl = new Set();
let missingMapsForLargeFirstPartyFile = false;
const results = [];
for (const script of artifacts.Scripts) {
const sourceMap = SourceMaps.find(m => m.scriptId === script.scriptId);
const errors = [];
const isLargeFirstParty = this.isLargeFirstPartyJS(script, artifacts.URL.finalUrl);
if (isLargeFirstParty && (!sourceMap || !sourceMap.map)) {
missingMapsForLargeFirstPartyFile = true;
isMissingMapForLargeFirstPartyScriptUrl.add(script.url);
errors.push({error: str_(UIStrings.missingSourceMapErrorMessage)});
}
if (sourceMap && !sourceMap.map) {
errors.push({error: sourceMap.errorMessage});
}
// Sources content errors.
if (sourceMap?.map) {
const sourcesContent = sourceMap.map.sourcesContent || [];
let missingSourcesContentCount = 0;
for (let i = 0; i < sourceMap.map.sources.length; i++) {
if (sourcesContent.length < i || !sourcesContent[i]) missingSourcesContentCount += 1;
}
if (missingSourcesContentCount > 0) {
errors.push({error: str_(UIStrings.missingSourceMapItemsWarningMesssage,
{missingItems: missingSourcesContentCount})});
}
}
if (sourceMap || errors.length) {
results.push({
scriptUrl: script.url,
sourceMapUrl: sourceMap?.sourceMapUrl,
subItems: {
type: /** @type {const} */ ('subitems'),
items: errors,
},
});
}
}
/** @type {LH.Audit.Details.TableColumnHeading[]} */
const headings = [
/* eslint-disable max-len */
{
key: 'scriptUrl',
itemType: 'url',
subItemsHeading: {key: 'error'},
text: str_(i18n.UIStrings.columnURL),
},
{key: 'sourceMapUrl', itemType: 'url', text: str_(UIStrings.columnMapURL)},
/* eslint-enable max-len */
];
results.sort((a, b) => {
// Show the items that can fail the audit first.
const missingMapA = isMissingMapForLargeFirstPartyScriptUrl.has(a.scriptUrl);
const missingMapB = isMissingMapForLargeFirstPartyScriptUrl.has(b.scriptUrl);
if (missingMapA && !missingMapB) return -1;
if (!missingMapA && missingMapB) return 1;
// Then sort by whether one has errors and the other doesn't.
if (a.subItems.items.length && !b.subItems.items.length) return -1;
if (!a.subItems.items.length && b.subItems.items.length) return 1;
// Then sort by script url.
return b.scriptUrl.localeCompare(a.scriptUrl);
});
// Only fails if `missingMapsForLargeFirstPartyFile` is true. All other errors are diagnostical.
return {
score: missingMapsForLargeFirstPartyFile ? 0 : 1,
details: Audit.makeTableDetails(headings, results),
};
}
}
module.exports = ValidSourceMaps;
module.exports.UIStrings = UIStrings;