Client - improve zxcvbn packages lazy loading

This commit is contained in:
Sam 2022-12-21 11:02:10 +01:00
parent 62340dc5da
commit 23afc10edc

View File

@ -1,37 +1,38 @@
import { zxcvbnOptions } from '@zxcvbn-ts/core' import { zxcvbnOptions } from '@zxcvbn-ts/core'
export const loadLanguagePackage = async (language: string) => {
// no package available for norwegian bokmal and dutch (Nederlands)
// fallback to english
switch (language) {
case 'fr':
return await import(
/* webpackChunkName: "password.fr" */ '@zxcvbn-ts/language-fr'
)
case 'de':
return await import(
/* webpackChunkName: "password.de" */ '@zxcvbn-ts/language-de'
)
case 'it':
return await import(
/* webpackChunkName: "password.it" */ '@zxcvbn-ts/language-it'
)
default:
return await import(
/* webpackChunkName: "password.en" */ '@zxcvbn-ts/language-en'
)
}
}
export const setZxcvbnOptions = async (language: string) => { export const setZxcvbnOptions = async (language: string) => {
const zxcvbnCommonPackage = await import( const zxcvbnCommonPackage = await import(
/* webpackChunkName: "password" */ '@zxcvbn-ts/language-common' /* webpackChunkName: "password" */ '@zxcvbn-ts/language-common'
) )
const zxcvbnEnPackage = await import( const zxcvbnLanguagePackage = await loadLanguagePackage(language)
/* webpackChunkName: "password" */ '@zxcvbn-ts/language-en'
)
const zxcvbnFrPackage = await import(
/* webpackChunkName: "password" */ '@zxcvbn-ts/language-fr'
)
const zxcvbnDePackage = await import(
/* webpackChunkName: "password" */ '@zxcvbn-ts/language-de'
)
const zxcvbnItPackage = await import(
/* webpackChunkName: "password" */ '@zxcvbn-ts/language-it'
)
const zxcvbnLangPackages: Record<string, typeof zxcvbnEnPackage> = {
de: zxcvbnDePackage,
en: zxcvbnEnPackage,
fr: zxcvbnFrPackage,
it: zxcvbnItPackage,
// no package available for norwegian bokmal, fallback on english
nb: zxcvbnEnPackage,
// no package available for dutch (Nederlands), fallback on english
nl: zxcvbnEnPackage,
}
const zxcvbnPackage = zxcvbnLangPackages[language]
const options = { const options = {
graphs: zxcvbnCommonPackage.default.adjacencyGraphs, graphs: zxcvbnCommonPackage.default.adjacencyGraphs,
dictionary: { dictionary: {
...zxcvbnCommonPackage.default.dictionary, ...zxcvbnCommonPackage.default.dictionary,
...zxcvbnPackage.default.dictionary, ...zxcvbnLanguagePackage.default.dictionary,
}, },
} }
zxcvbnOptions.setOptions(options) zxcvbnOptions.setOptions(options)