forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlarge-javascript-libraries.js
166 lines (139 loc) · 6.85 KB
/
large-javascript-libraries.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
/**
* @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.
*/
/**
* @fileoverview This audit checks a page for any large JS libraries with smaller alternatives.
* These libraries can be replaced with functionally equivalent, smaller ones.
* @see https://docs.google.com/document/d/1TgKO3cWqMpcS4dn0xbjDG5fyuqgVvYYoXg--knaxJnM
*/
'use strict';
/** @typedef {{repository: string, lastScraped: number|'Error', versions: Record<string, {gzip: number}>}} BundlePhobiaLibrary */
/** @typedef {{gzip: number, name: string, repository: string}} MinifiedBundlePhobiaLibrary */
/** @type {Record<string, BundlePhobiaLibrary>} */
const libStats = require('../lib/large-javascript-libraries/bundlephobia-database.json');
/** @type {Record<string, string[]>} */
const librarySuggestions = require('../lib/large-javascript-libraries/library-suggestions.js')
.suggestions;
const Audit = require('./audit.js');
const i18n = require('../lib/i18n/i18n.js');
const UIStrings = {
/** Title of a Lighthouse audit that provides detail on large Javascript libraries that are used on the page that have better alternatives. This descriptive title is shown when to users when no known unnecessarily large libraries are detected on the page.*/
title: 'Avoids large JavaScript libraries with smaller alternatives',
/** Title of a Lighthouse audit that provides detail on large Javascript libraries that are used on the page that have better alternatives. This descriptive title is shown when to users when some known unnecessarily large libraries are detected on the page.*/
failureTitle: 'Replace unnecessarily large JavaScript libraries',
/** Description of a Lighthouse audit that tells the user why they should care about the large Javascript libraries that have better alternatives. This is displayed after a user expands the section to see more. No character length limits. */
description: 'Large JavaScript libraries can lead to poor performance. ' +
'Prefer smaller, functionally equivalent libraries to reduce your bundle size.' +
' [Learn more](https://developers.google.com/web/fundamentals/performance/webpack/decrease-frontend-size#optimize_dependencies).',
/** Label for a column in a data table. Entries will be names of large JavaScript libraries that could be replaced. */
columnLibraryName: 'Library',
/** [ICU Syntax] Label for the Large JavaScrip Libraries audit identifying how many large libraries were found. */
displayValue: `{libraryCount, plural,
=1 {1 large library found}
other {# large libraries found}
}`,
};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
class LargeJavascriptLibraries extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'large-javascript-libraries',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['Stacks'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
/** @type {Array<{original: MinifiedBundlePhobiaLibrary, suggestions: MinifiedBundlePhobiaLibrary[]}>} */
const libraryPairings = [];
const detectedLibs = artifacts.Stacks;
const seenLibraries = new Set();
for (const detectedLib of detectedLibs) {
if (!detectedLib.npm || !libStats[detectedLib.npm]) continue;
const suggestions = librarySuggestions[detectedLib.npm];
if (!suggestions) continue;
if (seenLibraries.has(detectedLib.npm)) continue;
seenLibraries.add(detectedLib.npm);
let version = 'latest';
if (detectedLib.version && libStats[detectedLib.npm].versions[detectedLib.version]) {
version = detectedLib.version;
}
const originalLib = libStats[detectedLib.npm].versions[version];
/** @type {Array<{name: string, repository: string, gzip: number}>} */
const smallerSuggestions = [];
for (const suggestion of suggestions) {
if (libStats[suggestion].versions['latest'].gzip >= originalLib.gzip) continue;
smallerSuggestions.push({
name: suggestion,
repository: libStats[suggestion].repository,
gzip: libStats[suggestion].versions['latest'].gzip,
});
}
smallerSuggestions.sort((a, b) => a.gzip - b.gzip);
if (!smallerSuggestions.length) continue;
libraryPairings.push({
original: {
gzip: originalLib.gzip,
name: detectedLib.npm,
repository: libStats[detectedLib.npm].repository,
},
suggestions: smallerSuggestions,
});
}
/** @type {LH.Audit.Details.Table['items']} */
const tableDetails = libraryPairings.map(libraryPairing => {
const original = libraryPairing.original;
const suggestions = libraryPairing.suggestions;
const suggestionItems = suggestions.map(suggestion => {
return {
suggestion: {
type: /** @type {'link'} */ ('link'),
text: suggestion.name,
url: suggestion.repository,
},
transferSize: suggestion.gzip,
wastedBytes: original.gzip - suggestion.gzip,
};
});
return {
name: {
type: /** @type {'link'} */ ('link'),
text: original.name,
url: original.repository,
},
transferSize: original.gzip,
subItems: {
type: /** @type {'subitems'} */ ('subitems'),
items: suggestionItems,
},
};
});
/** @type {LH.Audit.Details.TableColumnHeading[]} */
const headings = [
/* eslint-disable max-len */
{key: 'name', itemType: 'text', subItemsHeading: {key: 'suggestion'}, text: str_(UIStrings.columnLibraryName)},
{key: 'transferSize', itemType: 'bytes', subItemsHeading: {key: 'transferSize'}, text: str_(i18n.UIStrings.columnTransferSize)},
{key: null, itemType: 'bytes', subItemsHeading: {key: 'wastedBytes'}, text: str_(i18n.UIStrings.columnWastedBytes)},
/* eslint-enable max-len */
];
const displayValue = str_(UIStrings.displayValue, {libraryCount: tableDetails.length});
const details = Audit.makeTableDetails(headings, tableDetails, {});
return {
score: libraryPairings.length > 0 ? 0 : 1,
displayValue,
details,
};
}
}
module.exports = LargeJavascriptLibraries;
module.exports.UIStrings = UIStrings;