-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clients(devtools): only use locales that have locale files to download #13214
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ const lighthouse = require('../lighthouse-core/index.js'); | |
const RawProtocol = require('../lighthouse-core/gather/connections/raw.js'); | ||
const log = require('lighthouse-logger'); | ||
const {lookupLocale} = require('../lighthouse-core/lib/i18n/i18n.js'); | ||
const {registerLocaleData} = require('../shared/localization/format.js'); | ||
const {registerLocaleData, getCanonicalLocales} = require('../shared/localization/format.js'); | ||
const constants = require('../lighthouse-core/config/constants.js'); | ||
|
||
/** @typedef {import('../lighthouse-core/gather/connections/connection.js')} Connection */ | ||
|
@@ -60,6 +60,17 @@ function listenForStatus(listenCallback) { | |
log.events.addListener('status', listenCallback); | ||
} | ||
|
||
/** | ||
* Does a locale lookup but limits the result to the *canonical* Lighthouse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The names feel a bit messed ATM. shared/localization/format.js has a "canonical locales" that is our "available" locales. the i18n object uses "supportedLocales" as well. i think this naming fits in a few places, since we basically have created a concept that's congruent with (a hypothetical) i'm fine addressing this outside of this PR, but just flagging it now while its fresh. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, agree 100% |
||
* locales, which are only the locales with a messages locale file that can | ||
* be downloaded and then used via `registerLocaleData`. | ||
* @param {string|string[]=} locales | ||
* @return {LH.Locale} | ||
*/ | ||
function lookupCanonicalLocale(locales) { | ||
return lookupLocale(locales, getCanonicalLocales()); | ||
} | ||
|
||
// For the bundle smoke test. | ||
if (typeof module !== 'undefined' && module.exports) { | ||
// Ideally this could be exposed via browserify's `standalone`, but it doesn't | ||
|
@@ -85,6 +96,7 @@ if (typeof self !== 'undefined') { | |
self.listenForStatus = listenForStatus; | ||
// @ts-expect-error | ||
self.registerLocaleData = registerLocaleData; | ||
// TODO: expose as lookupCanonicalLocale in LighthouseService.ts? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how much we care, though long term it's nice when the external thing is named the same as the internal thing for cross referencing |
||
// @ts-expect-error | ||
self.lookupLocale = lookupLocale; | ||
self.lookupLocale = lookupCanonicalLocale; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,12 +120,22 @@ const UIStrings = { | |
* - supported locales in Intl formatters | ||
* | ||
* If `locale` isn't provided or one could not be found, DEFAULT_LOCALE is returned. | ||
* | ||
* By default any of the locales Lighthouse has strings for can be returned, but this | ||
* can be overriden with `possibleLocales`, useful e.g. when Lighthouse is bundled and | ||
* only DEFAULT_LOCALE is available, but `possibleLocales` can be used to select a | ||
* locale available to be downloaded on demand. | ||
* @param {string|string[]=} locales | ||
* @param {Array<string>=} possibleLocales | ||
* @return {LH.Locale} | ||
*/ | ||
function lookupLocale(locales) { | ||
// If Node was built with `--with-intl=none`, `Intl` won't exist. | ||
function lookupLocale(locales, possibleLocales) { | ||
// TODO: lookupLocale may need to be split into two functions, one that canonicalizes | ||
// locales and one that looks up the best locale filename for a given locale. | ||
// e.g. `en-IE` is canonical, but uses `en-GB.json`. See TODO in locales.js | ||
Comment on lines
+133
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulirish depending on |
||
|
||
if (typeof Intl !== 'object') { | ||
// If Node was built with `--with-intl=none`, `Intl` won't exist. | ||
throw new Error('Lighthouse must be run in Node with `Intl` support. See https://nodejs.org/api/intl.html for help'); | ||
} | ||
|
||
|
@@ -135,7 +145,7 @@ function lookupLocale(locales) { | |
const availableLocales = Intl.NumberFormat.supportedLocalesOf(canonicalLocales); | ||
|
||
// Get available locales and transform into object to match `lookupClosestLocale`'s API. | ||
const localesWithMessages = getAvailableLocales(); | ||
const localesWithMessages = possibleLocales || getAvailableLocales(); | ||
const localesWithmessagesObj = /** @type {Record<LH.Locale, LhlMessages>} */ ( | ||
Object.fromEntries(localesWithMessages.map(l => [l, {}]))); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i didnt spot this change in your description. this does mean all brfs'd items get minified yeah?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's still running the minify transform over all the inlined files. The current
readFileTransform
replaces the inlined file with{}
if it's one of the locale files, otherwise it runsminifyFileTransform
. Now we don't need to handle the individual locale files, so we can just always runminifyFileTransform
.More or less reverting that part of #12921 since we can go back to just ignoring all of
locales.js