-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
stack-packs.js
142 lines (129 loc) · 3.21 KB
/
stack-packs.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
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import log from 'lighthouse-logger';
import stackPacks from 'lighthouse-stack-packs';
import * as i18n from './i18n/i18n.js';
/**
* Pairs consisting of a stack pack's ID and the set of stacks needed to be
* detected in a page to display that pack's advice.
* @type {Array<{packId: string, requiredStacks: Array<string>}>}
*/
const stackPacksToInclude = [
{
packId: 'gatsby',
requiredStacks: ['js:gatsby'],
},
{
packId: 'wordpress',
requiredStacks: ['js:wordpress'],
},
{
packId: 'wix',
requiredStacks: ['js:wix'],
},
{
packId: 'wp-rocket',
requiredStacks: ['js:wp-rocket'],
},
{
packId: 'ezoic',
requiredStacks: ['js:ezoic'],
},
{
packId: 'drupal',
requiredStacks: ['js:drupal'],
},
{
packId: 'nitropack',
requiredStacks: ['js:nitropack'],
},
{
packId: 'amp',
requiredStacks: ['js:amp'],
},
{
packId: 'magento',
requiredStacks: ['js:magento'],
},
{
packId: 'octobercms',
requiredStacks: ['js:octobercms'],
},
{
packId: 'joomla',
requiredStacks: ['js:joomla'],
},
{
packId: 'next.js',
requiredStacks: ['js:next'],
},
{
packId: 'nuxt',
requiredStacks: ['js:nuxt'],
},
{
packId: 'angular',
requiredStacks: ['js:@angular/core'],
},
{
packId: 'react',
requiredStacks: ['js:react'],
},
];
/**
* Returns all packs that match the stacks found in the page.
* @param {LH.Artifacts['Stacks']|undefined} pageStacks
* @return {LH.RawIcu<Array<LH.Result.StackPack>>}
*/
function getStackPacks(pageStacks) {
if (!pageStacks) return [];
/** @type {LH.RawIcu<Array<LH.Result.StackPack>>} */
const packs = [];
for (const pageStack of pageStacks) {
const stackPackToIncl = stackPacksToInclude.find(stackPackToIncl =>
stackPackToIncl.requiredStacks.includes(`${pageStack.detector}:${pageStack.id}`));
if (!stackPackToIncl) {
continue;
}
// Grab the full pack definition.
const matchedPack = stackPacks.find(pack => pack.id === stackPackToIncl.packId);
if (!matchedPack) {
log.warn('StackPacks',
`'${stackPackToIncl.packId}' stack pack was matched but is not found in stack-packs lib`);
continue;
}
// Create i18n handler to get translated strings.
const str_ = i18n.createIcuMessageFn(
`node_modules/lighthouse-stack-packs/packs/${matchedPack.id}.js`,
matchedPack.UIStrings
);
/** @type {Record<string, LH.IcuMessage>} */
const descriptions = {};
/** @type {Record<string, string>} */
const UIStrings = matchedPack.UIStrings;
// Convert all strings into the correct translation.
for (const key in UIStrings) {
if (UIStrings[key]) {
descriptions[key] = str_(UIStrings[key]);
}
}
packs.push({
id: matchedPack.id,
title: matchedPack.title,
iconDataURL: matchedPack.icon,
descriptions,
});
}
return packs.sort((a, b) => {
const aVal = stackPacksToInclude.findIndex(p => p.packId === a.id);
const bVal = stackPacksToInclude.findIndex(p => p.packId === b.id);
return aVal - bVal;
});
}
export {
getStackPacks,
stackPacksToInclude,
};