lighthouse 8.5.1-dev.20211012 → 8.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/dist/report/flow.js +149 -0
  2. package/dist/report/standalone.js +228 -0
  3. package/flow-report/assets/styles.css +9 -272
  4. package/flow-report/jest.config.js +1 -1
  5. package/flow-report/src/app.tsx +9 -31
  6. package/flow-report/src/common.tsx +7 -86
  7. package/flow-report/src/icons.tsx +3 -20
  8. package/flow-report/src/sidebar/sidebar.tsx +16 -11
  9. package/flow-report/src/summary/category.tsx +24 -42
  10. package/flow-report/src/summary/summary.tsx +29 -32
  11. package/flow-report/src/topbar.tsx +4 -54
  12. package/flow-report/src/util.ts +1 -26
  13. package/flow-report/src/wrappers/report-renderer.tsx +1 -1
  14. package/flow-report/src/wrappers/report.tsx +21 -6
  15. package/flow-report/test/app-test.tsx +13 -17
  16. package/flow-report/test/setup/env-setup.ts +0 -1
  17. package/flow-report/test/sidebar/flow-test.tsx +12 -6
  18. package/flow-report/test/sidebar/sidebar-test.tsx +13 -7
  19. package/flow-report/test/summary/category-test.tsx +34 -79
  20. package/flow-report/test/summary/summary-test.tsx +17 -11
  21. package/flow-report/test/util-test.tsx +12 -1
  22. package/flow-report/tsconfig.json +0 -1
  23. package/flow-report/types/flow-report.d.ts +1 -2
  24. package/jest.config.js +15 -1
  25. package/lighthouse-cli/bin.js +0 -5
  26. package/lighthouse-cli/cli-flags.js +3 -10
  27. package/lighthouse-cli/commands/commands.js +0 -1
  28. package/lighthouse-cli/run.js +2 -2
  29. package/lighthouse-cli/test/smokehouse/frontends/smokehouse-bin.js +0 -0
  30. package/lighthouse-cli/test/smokehouse/report-assert.js +4 -20
  31. package/lighthouse-core/audits/byte-efficiency/byte-efficiency-audit.js +1 -2
  32. package/lighthouse-core/audits/byte-efficiency/unused-javascript.js +1 -1
  33. package/lighthouse-core/audits/seo/crawlable-anchors.js +12 -10
  34. package/lighthouse-core/computed/js-bundles.js +2 -1
  35. package/lighthouse-core/computed/unused-javascript-summary.js +3 -3
  36. package/lighthouse-core/config/config.js +2 -2
  37. package/lighthouse-core/config/default-config.js +0 -5
  38. package/lighthouse-core/fraggle-rock/api.js +0 -10
  39. package/lighthouse-core/fraggle-rock/config/filters.js +3 -22
  40. package/lighthouse-core/fraggle-rock/gather/driver.js +0 -4
  41. package/lighthouse-core/fraggle-rock/gather/navigation-runner.js +2 -2
  42. package/lighthouse-core/fraggle-rock/user-flow.js +6 -35
  43. package/lighthouse-core/gather/driver/navigation.js +0 -5
  44. package/lighthouse-core/gather/driver/prepare.js +0 -14
  45. package/lighthouse-core/gather/driver/storage.js +0 -5
  46. package/lighthouse-core/gather/gather-runner.js +2 -2
  47. package/lighthouse-core/gather/gatherers/dobetterweb/response-compression.js +1 -5
  48. package/lighthouse-core/gather/gatherers/full-page-screenshot.js +24 -30
  49. package/{shared/localization/i18n-module.js → lighthouse-core/lib/cdt/Common.js} +8 -3
  50. package/lighthouse-core/lib/cdt/SDK.js +123 -3
  51. package/lighthouse-core/lib/cdt/generated/SourceMap.js +312 -148
  52. package/lighthouse-core/lib/csp-evaluator.js +1 -2
  53. package/lighthouse-core/lib/i18n/README.md +6 -6
  54. package/lighthouse-core/lib/i18n/i18n.js +342 -9
  55. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ar-XB.json +0 -0
  56. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ar.json +0 -0
  57. package/{shared/localization → lighthouse-core/lib/i18n}/locales/bg.json +0 -0
  58. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ca.json +0 -0
  59. package/{shared/localization → lighthouse-core/lib/i18n}/locales/cs.json +0 -0
  60. package/{shared/localization → lighthouse-core/lib/i18n}/locales/da.json +0 -0
  61. package/{shared/localization → lighthouse-core/lib/i18n}/locales/de.json +0 -0
  62. package/{shared/localization → lighthouse-core/lib/i18n}/locales/el.json +0 -0
  63. package/{shared/localization → lighthouse-core/lib/i18n}/locales/en-GB.json +0 -0
  64. package/{shared/localization → lighthouse-core/lib/i18n}/locales/en-US.json +0 -111
  65. package/{shared/localization → lighthouse-core/lib/i18n}/locales/en-XA.json +0 -0
  66. package/{shared/localization → lighthouse-core/lib/i18n}/locales/en-XL.json +0 -111
  67. package/{shared/localization → lighthouse-core/lib/i18n}/locales/es-419.json +0 -0
  68. package/{shared/localization → lighthouse-core/lib/i18n}/locales/es.json +0 -0
  69. package/{shared/localization → lighthouse-core/lib/i18n}/locales/fi.json +0 -0
  70. package/{shared/localization → lighthouse-core/lib/i18n}/locales/fil.json +0 -0
  71. package/{shared/localization → lighthouse-core/lib/i18n}/locales/fr.json +0 -0
  72. package/{shared/localization → lighthouse-core/lib/i18n}/locales/he.json +0 -0
  73. package/{shared/localization → lighthouse-core/lib/i18n}/locales/hi.json +0 -0
  74. package/{shared/localization → lighthouse-core/lib/i18n}/locales/hr.json +0 -0
  75. package/{shared/localization → lighthouse-core/lib/i18n}/locales/hu.json +0 -0
  76. package/{shared/localization → lighthouse-core/lib/i18n}/locales/id.json +0 -0
  77. package/{shared/localization → lighthouse-core/lib/i18n}/locales/it.json +0 -0
  78. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ja.json +0 -0
  79. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ko.json +0 -0
  80. package/{shared/localization → lighthouse-core/lib/i18n}/locales/lt.json +0 -0
  81. package/{shared/localization → lighthouse-core/lib/i18n}/locales/lv.json +0 -0
  82. package/{shared/localization → lighthouse-core/lib/i18n}/locales/nl.json +0 -0
  83. package/{shared/localization → lighthouse-core/lib/i18n}/locales/no.json +0 -0
  84. package/{shared/localization → lighthouse-core/lib/i18n}/locales/pl.json +0 -0
  85. package/{shared/localization → lighthouse-core/lib/i18n}/locales/pt-PT.json +0 -0
  86. package/{shared/localization → lighthouse-core/lib/i18n}/locales/pt.json +0 -0
  87. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ro.json +0 -0
  88. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ru.json +0 -0
  89. package/{shared/localization → lighthouse-core/lib/i18n}/locales/sk.json +0 -0
  90. package/{shared/localization → lighthouse-core/lib/i18n}/locales/sl.json +0 -0
  91. package/{shared/localization → lighthouse-core/lib/i18n}/locales/sr-Latn.json +0 -0
  92. package/{shared/localization → lighthouse-core/lib/i18n}/locales/sr.json +0 -0
  93. package/{shared/localization → lighthouse-core/lib/i18n}/locales/sv.json +0 -0
  94. package/{shared/localization → lighthouse-core/lib/i18n}/locales/ta.json +0 -0
  95. package/{shared/localization → lighthouse-core/lib/i18n}/locales/te.json +0 -0
  96. package/{shared/localization → lighthouse-core/lib/i18n}/locales/th.json +0 -0
  97. package/{shared/localization → lighthouse-core/lib/i18n}/locales/tr.json +0 -0
  98. package/{shared/localization → lighthouse-core/lib/i18n}/locales/uk.json +0 -0
  99. package/{shared/localization → lighthouse-core/lib/i18n}/locales/vi.json +0 -0
  100. package/{shared/localization → lighthouse-core/lib/i18n}/locales/zh-HK.json +0 -0
  101. package/{shared/localization → lighthouse-core/lib/i18n}/locales/zh-TW.json +0 -0
  102. package/{shared/localization → lighthouse-core/lib/i18n}/locales/zh.json +0 -0
  103. package/{shared/localization → lighthouse-core/lib/i18n}/locales.js +1 -2
  104. package/{shared/localization → lighthouse-core/lib/i18n}/swap-locale.js +7 -9
  105. package/lighthouse-core/lib/page-functions.js +4 -4
  106. package/lighthouse-core/lib/stack-packs.js +13 -1
  107. package/{shared → lighthouse-core/lib}/type-verifiers.js +0 -0
  108. package/lighthouse-core/runner.js +6 -6
  109. package/lighthouse-core/scripts/manual-chrome-launcher.js +1 -4
  110. package/lighthouse-core/util-commonjs.js +4 -20
  111. package/package.json +16 -19
  112. package/readme.md +1 -1
  113. package/report/assets/standalone-template.html +1 -0
  114. package/report/assets/styles.css +41 -16
  115. package/report/assets/templates.html +4 -42
  116. package/report/clients/psi.js +0 -1
  117. package/report/clients/standalone.js +2 -1
  118. package/report/generator/file-namer.js +8 -17
  119. package/report/generator/report-generator.js +1 -0
  120. package/report/renderer/category-renderer.js +16 -4
  121. package/report/renderer/components.js +71 -107
  122. package/report/renderer/dom.js +0 -19
  123. package/report/renderer/logger.js +2 -35
  124. package/report/renderer/performance-category-renderer.js +21 -23
  125. package/report/renderer/pwa-category-renderer.js +2 -1
  126. package/report/renderer/report-renderer.js +0 -21
  127. package/report/renderer/report-ui-features.js +19 -7
  128. package/report/renderer/util.js +4 -20
  129. package/report/test/clients/psi-test.js +4 -0
  130. package/report/test/generator/file-namer-test.js +2 -2
  131. package/report/test/renderer/category-renderer-test.js +1 -1
  132. package/report/test/renderer/performance-category-renderer-test.js +0 -12
  133. package/report/test/renderer/report-renderer-test.js +55 -0
  134. package/report/test/renderer/util-test.js +8 -68
  135. package/report/test-assets/faux-psi-template.html +1 -0
  136. package/report/tsconfig.json +1 -2
  137. package/report/types/html-renderer.d.ts +1 -2
  138. package/root.js +0 -18
  139. package/third-party/chromium-synchronization/inspector-issueAdded-types-test.js +3 -5
  140. package/third-party/chromium-synchronization/installability-errors-test.js +1 -2
  141. package/tsconfig-all.json +0 -1
  142. package/tsconfig-base.json +1 -1
  143. package/tsconfig.json +3 -2
  144. package/types/config.d.ts +0 -1
  145. package/types/externs.d.ts +0 -2
  146. package/types/lhr/flow.d.ts +0 -7
  147. package/types/lhr/i18n.d.ts +3 -0
  148. package/changelog.md +0 -5395
  149. package/flow-report/.eslintrc.cjs +0 -49
  150. package/flow-report/package.json +0 -4
  151. package/flow-report/src/header.tsx +0 -88
  152. package/flow-report/src/help-dialog.tsx +0 -120
  153. package/flow-report/src/i18n/i18n.tsx +0 -44
  154. package/flow-report/src/i18n/localized-strings.js +0 -11
  155. package/flow-report/src/i18n/ui-strings.js +0 -86
  156. package/flow-report/test/common-test.tsx +0 -89
  157. package/flow-report/test/header-test.tsx +0 -55
  158. package/flow-report/test/sample-flow.ts +0 -17
  159. package/flow-report/test/topbar-test.tsx +0 -68
  160. package/lighthouse-cli/commands/list-locales.js +0 -16
  161. package/lighthouse-core/lib/cdt/Platform.js +0 -55
  162. package/lighthouse-core/scripts/package.json +0 -4
  163. package/report/renderer/swap-locale-feature.js +0 -82
  164. package/report/test/renderer/report-renderer-axe-test.js +0 -86
  165. package/shared/localization/format.js +0 -422
  166. package/shared/localization/swap-flow-locale.js +0 -20
  167. package/shared/test/localization/format-test.js +0 -377
  168. package/shared/test/localization/locales-test.js +0 -47
  169. package/shared/test/localization/swap-locale-test.js +0 -140
  170. package/shared/tsconfig.json +0 -22
  171. package/shared/types/shared.d.ts +0 -24
  172. package/types/es-main.d.ts +0 -18
@@ -1,7 +1,7 @@
1
1
  # Terminology
2
2
 
3
3
  * **CTC format**: The [Chrome extension & Chrome app i18n format](https://developer.chrome.com/extensions/i18n-messages) with some minor changes. JSON with their specified model for declaring placeholders, examples, etc. Used as an interchange data format.
4
- * **LHL syntax** (Lighthouse Localizable syntax): The ICU-friendly string syntax that is used to author `UIStrings` and is seen in the locale files in `shared/localization/locales/*.json`. Lighthouse has a custom syntax these strings combines many ICU message features along with some markdown.
4
+ * **LHL syntax** (Lighthouse Localizable syntax): The ICU-friendly string syntax that is used to author `UIStrings` and is seen in the locale files in `i18n/locales/*.json`. Lighthouse has a custom syntax these strings combines many ICU message features along with some markdown.
5
5
  * **ICU**: ICU (International Components for Unicode) is a localization project and standard defined by the Unicode consortium. In general, we refer to "ICU" as the [ICU message formatting](http://userguide.icu-project.org/formatparse/messages) syntax.
6
6
 
7
7
  # The Lighthouse i18n pipeline
@@ -12,11 +12,11 @@ The collection and translation pipeline:
12
12
  ```
13
13
  Source files: Locale files:
14
14
  +---------------------------+ +----------------------------------------------
15
- | ++ | shared/localization/locales/en-US.json |
16
- | const UIStrings = { ... };|-+ +---> | shared/localization/locales/en-XL.json |
15
+ | ++ | lighthouse-core/lib/i18n/locales/en-US.json |
16
+ | const UIStrings = { ... };|-+ +---> | lighthouse-core/lib/i18n/locales/en-XL.json |
17
17
  | |-| | +----------------------------------------------+
18
18
  +-----------------------------| | | ||
19
- +----------------------------| | | shared/localization/locales/*.json |-<+
19
+ +----------------------------| | | lighthouse-core/lib/i18n/locales/*.json |-<+
20
20
  +---------------------------+ | | || |
21
21
  | | +----------------------------------------------| |
22
22
  $ yarn | | +---------------------------------------------+ |
@@ -218,7 +218,7 @@ CTC is a name that is distinct and identifies this as the Chrome translation for
218
218
  1. String called in `.js` file, converted to `LH.IcuMessage` object.
219
219
 
220
220
  1. Message object is replaced with the localized string via
221
- `format.replaceIcuMessages` and `format.getFormatted`.
221
+ `i18n.replaceIcuMessages` and `i18n.getFormatted`.
222
222
 
223
223
  #### Example:
224
224
 
@@ -251,7 +251,7 @@ CTC is a name that is distinct and identifies this as the Chrome translation for
251
251
  }
252
252
  ```
253
253
 
254
- 3. Lookup in `format.replaceIcuMessages` and `format.getFormatted` will attempt to find the message in this order:
254
+ 3. Lookup in `i18n.replaceIcuMessages` and `i18n.getFormatted` will attempt to find the message in this order:
255
255
 
256
256
  1. `locales/{locale}.json` The best result. `icuMessage.i18nId` is found in the target locale and the resulting string should appear correct.
257
257
 
@@ -5,17 +5,23 @@
5
5
  */
6
6
  'use strict';
7
7
 
8
- /** @typedef {import('../../../shared/localization/locales').LhlMessages} LhlMessages */
8
+ /** @typedef {import('../../lib/i18n/locales').LhlMessages} LhlMessages */
9
9
 
10
10
  const path = require('path');
11
+ const MessageFormat = require('intl-messageformat').default;
11
12
  const lookupClosestLocale = require('lookup-closest-locale');
12
- const {getAvailableLocales} = require('../../../shared/localization/format.js');
13
+ const LOCALES = require('./locales.js');
14
+ const {isObjectOfUnknownValues, isObjectOrArrayOfUnknownValues} = require('../type-verifiers.js');
13
15
  const log = require('lighthouse-logger');
14
16
  const {LH_ROOT} = require('../../../root.js');
15
- const {isIcuMessage, _formatMessage} = require('../../../shared/localization/format.js');
16
17
 
17
18
  const DEFAULT_LOCALE = 'en';
18
19
 
20
+ /** @typedef {import('intl-messageformat-parser').Element} MessageElement */
21
+ /** @typedef {import('intl-messageformat-parser').ArgumentElement} ArgumentElement */
22
+
23
+ const MESSAGE_I18N_ID_REGEX = / | [^\s]+$/;
24
+
19
25
  const UIStrings = {
20
26
  /** Used to show the duration in milliseconds that something lasted. The `{timeInMs}` placeholder will be replaced with the time duration, shown in milliseconds (e.g. 63 ms) */
21
27
  ms: '{timeInMs, number, milliseconds}\xa0ms',
@@ -111,6 +117,27 @@ const UIStrings = {
111
117
  itemSeverityHigh: 'High',
112
118
  };
113
119
 
120
+ const formats = {
121
+ number: {
122
+ bytes: {
123
+ maximumFractionDigits: 0,
124
+ },
125
+ milliseconds: {
126
+ maximumFractionDigits: 0,
127
+ },
128
+ seconds: {
129
+ // Force the seconds to the tenths place for limited output and ease of scanning
130
+ minimumFractionDigits: 1,
131
+ maximumFractionDigits: 1,
132
+ },
133
+ extendedPercent: {
134
+ // Force allow up to two digits after decimal place in percentages. (Intl.NumberFormat options)
135
+ maximumFractionDigits: 2,
136
+ style: 'percent',
137
+ },
138
+ },
139
+ };
140
+
114
141
  /**
115
142
  * Look up the best available locale for the requested language through these fall backs:
116
143
  * - exact match
@@ -133,12 +160,7 @@ function lookupLocale(locales) {
133
160
  // Filter by what's available in this runtime.
134
161
  const availableLocales = Intl.NumberFormat.supportedLocalesOf(canonicalLocales);
135
162
 
136
- // Get available locales and transform into object to match `lookupClosestLocale`'s API.
137
- const localesWithMessages = getAvailableLocales();
138
- const localesWithmessagesObj = /** @type {Record<LH.Locale, LhlMessages>} */ (
139
- Object.fromEntries(localesWithMessages.map(l => [l, {}])));
140
-
141
- const closestLocale = lookupClosestLocale(availableLocales, localesWithmessagesObj);
163
+ const closestLocale = lookupClosestLocale(availableLocales, LOCALES);
142
164
 
143
165
  if (!closestLocale) {
144
166
  // Log extra info if we're pretty sure this version of Node was built with `--with-intl=small-icu`.
@@ -152,6 +174,191 @@ function lookupLocale(locales) {
152
174
  return closestLocale || DEFAULT_LOCALE;
153
175
  }
154
176
 
177
+ /**
178
+ * Function to retrieve all 'argumentElement's from an ICU message. An argumentElement
179
+ * is an ICU element with an argument in it, like '{varName}' or '{varName, number, bytes}'. This
180
+ * differs from 'messageElement's which are just arbitrary text in a message.
181
+ *
182
+ * Notes:
183
+ * This function will recursively inspect plural elements for nested argumentElements.
184
+ *
185
+ * We need to find all the elements from the plural format sections, but
186
+ * they need to be deduplicated. I.e. "=1{hello {icu}} =other{hello {icu}}"
187
+ * the variable "icu" would appear twice if it wasn't de duplicated. And they cannot
188
+ * be stored in a set because they are not equal since their locations are different,
189
+ * thus they are stored via a Map keyed on the "id" which is the ICU varName.
190
+ *
191
+ * @param {Array<MessageElement>} icuElements
192
+ * @param {Map<string, ArgumentElement>} [seenElementsById]
193
+ * @return {Map<string, ArgumentElement>}
194
+ */
195
+ function collectAllCustomElementsFromICU(icuElements, seenElementsById = new Map()) {
196
+ for (const el of icuElements) {
197
+ // We are only interested in elements that need ICU formatting (argumentElements)
198
+ if (el.type !== 'argumentElement') continue;
199
+
200
+ seenElementsById.set(el.id, el);
201
+
202
+ // Plurals need to be inspected recursively
203
+ if (!el.format || el.format.type !== 'pluralFormat') continue;
204
+ // Look at all options of the plural (=1{} =other{}...)
205
+ for (const option of el.format.options) {
206
+ // Run collections on each option's elements
207
+ collectAllCustomElementsFromICU(option.value.elements, seenElementsById);
208
+ }
209
+ }
210
+
211
+ return seenElementsById;
212
+ }
213
+
214
+ /**
215
+ * Returns a copy of the `values` object, with the values formatted based on how
216
+ * they will be used in their icuMessage, e.g. KB or milliseconds. The original
217
+ * object is unchanged.
218
+ * @param {MessageFormat} messageFormatter
219
+ * @param {Readonly<Record<string, string | number>>} values
220
+ * @param {string} lhlMessage Used for clear error logging.
221
+ * @return {Record<string, string | number>}
222
+ */
223
+ function _preformatValues(messageFormatter, values, lhlMessage) {
224
+ const elementMap = collectAllCustomElementsFromICU(messageFormatter.getAst().elements);
225
+ const argumentElements = [...elementMap.values()];
226
+
227
+ /** @type {Record<string, string | number>} */
228
+ const formattedValues = {};
229
+
230
+ for (const {id, format} of argumentElements) {
231
+ // Throw an error if a message's value isn't provided
232
+ if (id && (id in values) === false) {
233
+ throw new Error(`ICU Message "${lhlMessage}" contains a value reference ("${id}") ` +
234
+ `that wasn't provided`);
235
+ }
236
+
237
+ const value = values[id];
238
+
239
+ // Direct `{id}` replacement and non-numeric values need no formatting.
240
+ if (!format || format.type !== 'numberFormat') {
241
+ formattedValues[id] = value;
242
+ continue;
243
+ }
244
+
245
+ if (typeof value !== 'number') {
246
+ throw new Error(`ICU Message "${lhlMessage}" contains a numeric reference ("${id}") ` +
247
+ 'but provided value was not a number');
248
+ }
249
+
250
+ // Format values for known styles.
251
+ if (format.style === 'milliseconds') {
252
+ // Round all milliseconds to the nearest 10.
253
+ formattedValues[id] = Math.round(value / 10) * 10;
254
+ } else if (format.style === 'seconds' && id === 'timeInMs') {
255
+ // Convert all seconds to the correct unit (currently only for `timeInMs`).
256
+ formattedValues[id] = Math.round(value / 100) / 10;
257
+ } else if (format.style === 'bytes') {
258
+ // Replace all the bytes with KB.
259
+ formattedValues[id] = value / 1024;
260
+ } else {
261
+ // For all other number styles, the value isn't changed.
262
+ formattedValues[id] = value;
263
+ }
264
+ }
265
+
266
+ // Throw an error if a value is provided but has no placeholder in the message.
267
+ for (const valueId of Object.keys(values)) {
268
+ if (valueId in formattedValues) continue;
269
+
270
+ // errorCode is a special case always allowed to help LHError ease-of-use.
271
+ if (valueId === 'errorCode') {
272
+ formattedValues.errorCode = values.errorCode;
273
+ continue;
274
+ }
275
+
276
+ throw new Error(`Provided value "${valueId}" does not match any placeholder in ` +
277
+ `ICU message "${lhlMessage}"`);
278
+ }
279
+
280
+ return formattedValues;
281
+ }
282
+
283
+ /**
284
+ * Format string `message` by localizing `values` and inserting them.
285
+ * @param {string} message
286
+ * @param {Record<string, string | number>} values
287
+ * @param {LH.Locale} locale
288
+ * @return {string}
289
+ */
290
+ function _formatMessage(message, values = {}, locale) {
291
+ // When using accented english, force the use of a different locale for number formatting.
292
+ const localeForMessageFormat = (locale === 'en-XA' || locale === 'en-XL') ? 'de-DE' : locale;
293
+
294
+ const formatter = new MessageFormat(message, localeForMessageFormat, formats);
295
+
296
+ // Preformat values for the message format like KB and milliseconds.
297
+ const valuesForMessageFormat = _preformatValues(formatter, values, message);
298
+
299
+ return formatter.format(valuesForMessageFormat);
300
+ }
301
+
302
+ /**
303
+ * Retrieves the localized version of `icuMessage` and formats with any given
304
+ * value replacements.
305
+ * @param {LH.IcuMessage} icuMessage
306
+ * @param {LH.Locale} locale
307
+ * @return {string}
308
+ */
309
+ function _localizeIcuMessage(icuMessage, locale) {
310
+ const localeMessages = LOCALES[locale];
311
+ if (!localeMessages) throw new Error(`Unsupported locale '${locale}'`);
312
+ const localeMessage = localeMessages[icuMessage.i18nId];
313
+
314
+ // Fall back to the default (usually the original english message) if we couldn't find a
315
+ // message in the specified locale. This could be because of string drift between
316
+ // Lighthouse versions or because new strings haven't been updated yet. Better to have
317
+ // an english message than no message at all; in some cases it won't even matter.
318
+ if (!localeMessage) {
319
+ return icuMessage.formattedDefault;
320
+ }
321
+
322
+ return _formatMessage(localeMessage.message, icuMessage.values, locale);
323
+ }
324
+
325
+ /** @param {string[]} pathInLHR */
326
+ function _formatPathAsString(pathInLHR) {
327
+ let pathAsString = '';
328
+ for (const property of pathInLHR) {
329
+ if (/^[a-z]+$/i.test(property)) {
330
+ if (pathAsString.length) pathAsString += '.';
331
+ pathAsString += property;
332
+ } else {
333
+ if (/]|"|'|\s/.test(property)) throw new Error(`Cannot handle "${property}" in i18n`);
334
+ pathAsString += `[${property}]`;
335
+ }
336
+ }
337
+
338
+ return pathAsString;
339
+ }
340
+
341
+ /**
342
+ * @param {LH.Locale} locale
343
+ * @return {Record<string, string>}
344
+ */
345
+ function getRendererFormattedStrings(locale) {
346
+ const localeMessages = LOCALES[locale];
347
+ if (!localeMessages) throw new Error(`Unsupported locale '${locale}'`);
348
+
349
+ const icuMessageIds = Object.keys(localeMessages).filter(f => f.startsWith('report/'));
350
+ /** @type {Record<string, string>} */
351
+ const strings = {};
352
+ for (const icuMessageId of icuMessageIds) {
353
+ const [filename, key] = icuMessageId.split(' | ');
354
+ if (!filename.endsWith('util.js')) throw new Error(`Unexpected message: ${icuMessageId}`);
355
+
356
+ strings[key] = localeMessages[icuMessageId].message;
357
+ }
358
+
359
+ return strings;
360
+ }
361
+
155
362
  /**
156
363
  * Returns a function that generates `LH.IcuMessage` objects to localize the
157
364
  * messages in `fileStrings` and the shared `i18n.UIStrings`.
@@ -189,6 +396,125 @@ function createIcuMessageFn(filename, fileStrings) {
189
396
  return getIcuMessageFn;
190
397
  }
191
398
 
399
+ /**
400
+ * Returns whether `icuMessageOrNot`` is an `LH.IcuMessage` instance.
401
+ * @param {unknown} icuMessageOrNot
402
+ * @return {icuMessageOrNot is LH.IcuMessage}
403
+ */
404
+ function isIcuMessage(icuMessageOrNot) {
405
+ if (!isObjectOfUnknownValues(icuMessageOrNot)) {
406
+ return false;
407
+ }
408
+
409
+ const {i18nId, values, formattedDefault} = icuMessageOrNot;
410
+ if (typeof i18nId !== 'string') {
411
+ return false;
412
+ }
413
+
414
+ // formattedDefault is required.
415
+ if (typeof formattedDefault !== 'string') {
416
+ return false;
417
+ }
418
+
419
+ // Values is optional.
420
+ if (values !== undefined) {
421
+ if (!isObjectOfUnknownValues(values)) {
422
+ return false;
423
+ }
424
+ for (const value of Object.values(values)) {
425
+ if (typeof value !== 'string' && typeof value !== 'number') {
426
+ return false;
427
+ }
428
+ }
429
+ }
430
+
431
+ // Finally return true if i18nId seems correct.
432
+ return MESSAGE_I18N_ID_REGEX.test(i18nId);
433
+ }
434
+
435
+ /**
436
+ * Get the localized and formatted form of `icuMessageOrRawString` if it's an
437
+ * LH.IcuMessage, or get it back directly if it's already a string.
438
+ * Warning: this function throws if `icuMessageOrRawString` is not the expected
439
+ * type (use function from `createIcuMessageFn` to create a valid LH.IcuMessage)
440
+ * or `locale` isn't supported (use `lookupLocale` to find a valid locale).
441
+ * @param {LH.IcuMessage | string} icuMessageOrRawString
442
+ * @param {LH.Locale} locale
443
+ * @return {string}
444
+ */
445
+ function getFormatted(icuMessageOrRawString, locale) {
446
+ if (isIcuMessage(icuMessageOrRawString)) {
447
+ return _localizeIcuMessage(icuMessageOrRawString, locale);
448
+ }
449
+
450
+ if (typeof icuMessageOrRawString === 'string') {
451
+ return icuMessageOrRawString;
452
+ }
453
+
454
+ // Should be impossible from types, but do a strict check in case malformed JSON makes it this far.
455
+ throw new Error('Attempted to format invalid icuMessage type');
456
+ }
457
+
458
+ /**
459
+ * Recursively walk the input object, looking for property values that are
460
+ * `LH.IcuMessage`s and replace them with their localized values. Primarily
461
+ * used with the full LHR or a Config as input.
462
+ * Returns a map of locations that were replaced to the `IcuMessage` that was at
463
+ * that location.
464
+ * @param {unknown} inputObject
465
+ * @param {LH.Locale} locale
466
+ * @return {LH.Result.IcuMessagePaths}
467
+ */
468
+ function replaceIcuMessages(inputObject, locale) {
469
+ /**
470
+ * @param {unknown} subObject
471
+ * @param {LH.Result.IcuMessagePaths} icuMessagePaths
472
+ * @param {string[]} pathInLHR
473
+ */
474
+ function replaceInObject(subObject, icuMessagePaths, pathInLHR = []) {
475
+ if (!isObjectOrArrayOfUnknownValues(subObject)) return;
476
+
477
+ for (const [property, possibleIcuMessage] of Object.entries(subObject)) {
478
+ const currentPathInLHR = pathInLHR.concat([property]);
479
+
480
+ // Replace any IcuMessages with a localized string.
481
+ if (isIcuMessage(possibleIcuMessage)) {
482
+ const formattedString = _localizeIcuMessage(possibleIcuMessage, locale);
483
+ const messageInstancesInLHR = icuMessagePaths[possibleIcuMessage.i18nId] || [];
484
+ const currentPathAsString = _formatPathAsString(currentPathInLHR);
485
+
486
+ messageInstancesInLHR.push(
487
+ possibleIcuMessage.values ?
488
+ {values: possibleIcuMessage.values, path: currentPathAsString} :
489
+ currentPathAsString
490
+ );
491
+
492
+ // @ts-ignore - tsc doesn't like that `property` can be either string key or array index.
493
+ subObject[property] = formattedString;
494
+ icuMessagePaths[possibleIcuMessage.i18nId] = messageInstancesInLHR;
495
+ } else {
496
+ replaceInObject(possibleIcuMessage, icuMessagePaths, currentPathInLHR);
497
+ }
498
+ }
499
+ }
500
+
501
+ /** @type {LH.Result.IcuMessagePaths} */
502
+ const icuMessagePaths = {};
503
+ replaceInObject(inputObject, icuMessagePaths);
504
+ return icuMessagePaths;
505
+ }
506
+
507
+ /**
508
+ * Populate the i18n string lookup dict with locale data
509
+ * Used when the host environment selects the locale and serves lighthouse the intended locale file
510
+ * @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
511
+ * @param {LH.Locale} locale
512
+ * @param {LhlMessages} lhlMessages
513
+ */
514
+ function registerLocaleData(locale, lhlMessages) {
515
+ LOCALES[locale] = lhlMessages;
516
+ }
517
+
192
518
  /**
193
519
  * Returns true if the given value is a string or an LH.IcuMessage.
194
520
  * @param {unknown} value
@@ -199,9 +525,16 @@ function isStringOrIcuMessage(value) {
199
525
  }
200
526
 
201
527
  module.exports = {
528
+ _formatPathAsString,
202
529
  UIStrings,
203
530
  lookupLocale,
531
+ getRendererFormattedStrings,
204
532
  createIcuMessageFn,
533
+ getFormatted,
534
+ replaceIcuMessages,
535
+ isIcuMessage,
536
+ collectAllCustomElementsFromICU,
537
+ registerLocaleData,
205
538
  isStringOrIcuMessage,
206
539
  // TODO: exported for backwards compatibility. Consider removing on future breaking change.
207
540
  createMessageInstanceIdFn: createIcuMessageFn,
@@ -1,115 +1,4 @@
1
1
  {
2
- "flow-report/src/i18n/ui-strings.js | allReports": {
3
- "message": "All Reports"
4
- },
5
- "flow-report/src/i18n/ui-strings.js | categories": {
6
- "message": "Categories"
7
- },
8
- "flow-report/src/i18n/ui-strings.js | categoryAccessibility": {
9
- "message": "Accessibility"
10
- },
11
- "flow-report/src/i18n/ui-strings.js | categoryBestPractices": {
12
- "message": "Best Practices"
13
- },
14
- "flow-report/src/i18n/ui-strings.js | categoryPerformance": {
15
- "message": "Performance"
16
- },
17
- "flow-report/src/i18n/ui-strings.js | categoryProgressiveWebApp": {
18
- "message": "Progressive Web App"
19
- },
20
- "flow-report/src/i18n/ui-strings.js | categorySeo": {
21
- "message": "SEO"
22
- },
23
- "flow-report/src/i18n/ui-strings.js | desktop": {
24
- "message": "Desktop"
25
- },
26
- "flow-report/src/i18n/ui-strings.js | helpDialogTitle": {
27
- "message": "Understanding the Lighthouse Flow Report"
28
- },
29
- "flow-report/src/i18n/ui-strings.js | helpLabel": {
30
- "message": "Understanding Flows"
31
- },
32
- "flow-report/src/i18n/ui-strings.js | helpUseCaseInstructionNavigation": {
33
- "message": "Use Navigation reports to..."
34
- },
35
- "flow-report/src/i18n/ui-strings.js | helpUseCaseInstructionSnapshot": {
36
- "message": "Use Snapshot reports to..."
37
- },
38
- "flow-report/src/i18n/ui-strings.js | helpUseCaseInstructionTimespan": {
39
- "message": "Use Timespan reports to..."
40
- },
41
- "flow-report/src/i18n/ui-strings.js | helpUseCaseNavigation1": {
42
- "message": "Obtain a Lighthouse Performance score."
43
- },
44
- "flow-report/src/i18n/ui-strings.js | helpUseCaseNavigation2": {
45
- "message": "Measure page load Performance metrics such as Largest Contentful Paint and Speed Index."
46
- },
47
- "flow-report/src/i18n/ui-strings.js | helpUseCaseNavigation3": {
48
- "message": "Assess Progressive Web App capabilities."
49
- },
50
- "flow-report/src/i18n/ui-strings.js | helpUseCaseSnapshot1": {
51
- "message": "Find accessibility issues in single page applications or complex forms."
52
- },
53
- "flow-report/src/i18n/ui-strings.js | helpUseCaseSnapshot2": {
54
- "message": "Evaluate best practices of menus and UI elements hidden behind interaction."
55
- },
56
- "flow-report/src/i18n/ui-strings.js | helpUseCaseTimespan1": {
57
- "message": "Measure layout shifts and JavaScript execution time on a series of interactions."
58
- },
59
- "flow-report/src/i18n/ui-strings.js | helpUseCaseTimespan2": {
60
- "message": "Discover performance opportunities to improve the experience for long-lived pages and single-page applications."
61
- },
62
- "flow-report/src/i18n/ui-strings.js | mobile": {
63
- "message": "Mobile"
64
- },
65
- "flow-report/src/i18n/ui-strings.js | navigationDescription": {
66
- "message": "Page load"
67
- },
68
- "flow-report/src/i18n/ui-strings.js | navigationLongDescription": {
69
- "message": "Navigation reports analyze a single page load, exactly like the original Lighthouse reports."
70
- },
71
- "flow-report/src/i18n/ui-strings.js | navigationReport": {
72
- "message": "Navigation report"
73
- },
74
- "flow-report/src/i18n/ui-strings.js | ratingAverage": {
75
- "message": "Average"
76
- },
77
- "flow-report/src/i18n/ui-strings.js | ratingError": {
78
- "message": "Error"
79
- },
80
- "flow-report/src/i18n/ui-strings.js | ratingFail": {
81
- "message": "Poor"
82
- },
83
- "flow-report/src/i18n/ui-strings.js | ratingPass": {
84
- "message": "Good"
85
- },
86
- "flow-report/src/i18n/ui-strings.js | save": {
87
- "message": "Save"
88
- },
89
- "flow-report/src/i18n/ui-strings.js | snapshotDescription": {
90
- "message": "Captured state of page"
91
- },
92
- "flow-report/src/i18n/ui-strings.js | snapshotLongDescription": {
93
- "message": "Snapshot reports analyze the page in a particular state, typically after user interactions."
94
- },
95
- "flow-report/src/i18n/ui-strings.js | snapshotReport": {
96
- "message": "Snapshot report"
97
- },
98
- "flow-report/src/i18n/ui-strings.js | summary": {
99
- "message": "Summary"
100
- },
101
- "flow-report/src/i18n/ui-strings.js | timespanDescription": {
102
- "message": "User interactions"
103
- },
104
- "flow-report/src/i18n/ui-strings.js | timespanLongDescription": {
105
- "message": "Timespan reports analyze an arbitrary period of time, typically containing user interactions."
106
- },
107
- "flow-report/src/i18n/ui-strings.js | timespanReport": {
108
- "message": "Timespan report"
109
- },
110
- "flow-report/src/i18n/ui-strings.js | title": {
111
- "message": "Lighthouse User Flow Report"
112
- },
113
2
  "lighthouse-core/audits/accessibility/accesskeys.js | description": {
114
3
  "message": "Access keys let users quickly focus a part of the page. For proper navigation, each access key must be unique. [Learn more](https://web.dev/accesskeys/)."
115
4
  },