From e3c4b027cdf5ec2f04f2d11466f50e5e7fee6878 Mon Sep 17 00:00:00 2001 From: Alex Komz Date: Fri, 19 Jan 2024 20:50:20 +0300 Subject: [PATCH] fix(settings): replaces dynamic import with getting a ready static module loader --- src/settings/settings.ts | 5 +- src/utils/index.ts | 1 + src/utils/locales.ts | 152 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 src/utils/locales.ts diff --git a/src/settings/settings.ts b/src/settings/settings.ts index d527b45..b9d1076 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -2,6 +2,7 @@ import cloneDeep from 'lodash/cloneDeep'; import dayjs from '../dayjs'; import {normalizeTimeZone} from '../timeZone'; +import {getLocaleLoader} from '../utils'; import type {UpdateLocaleConfig} from './types'; @@ -22,8 +23,8 @@ class Settings { if (!this.isLocaleLoaded(locale)) { try { const localeInLowerCase = locale.toLocaleLowerCase(); - // https://github.com/iamkun/dayjs/issues/792#issuecomment-639961997 - await import(`dayjs/locale/${localeInLowerCase}.js`); + const localeLoader = getLocaleLoader(localeInLowerCase); + await localeLoader(); this.loadedLocales.add(localeInLowerCase); } catch (error) { throw new Error( diff --git a/src/utils/index.ts b/src/utils/index.ts index b2ea06b..4673258 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './duration'; export * from './utils'; +export * from './locales'; diff --git a/src/utils/locales.ts b/src/utils/locales.ts new file mode 100644 index 0000000..3444033 --- /dev/null +++ b/src/utils/locales.ts @@ -0,0 +1,152 @@ +export type LocalLoader = () => Promise; +export type LocalLoadersDict = Record; + +export const localeLoaders: LocalLoadersDict = { + af: async () => await import('dayjs/locale/af.js'), + am: async () => await import('dayjs/locale/am.js'), + 'ar-dz': async () => await import('dayjs/locale/ar-dz.js'), + 'ar-iq': async () => await import('dayjs/locale/ar-iq.js'), + 'ar-kw': async () => await import('dayjs/locale/ar-kw.js'), + 'ar-ly': async () => await import('dayjs/locale/ar-ly.js'), + 'ar-ma': async () => await import('dayjs/locale/ar-ma.js'), + 'ar-sa': async () => await import('dayjs/locale/ar-sa.js'), + 'ar-tn': async () => await import('dayjs/locale/ar-tn.js'), + ar: async () => await import('dayjs/locale/ar.js'), + az: async () => await import('dayjs/locale/az.js'), + be: async () => await import('dayjs/locale/be.js'), + bg: async () => await import('dayjs/locale/bg.js'), + bi: async () => await import('dayjs/locale/bi.js'), + bm: async () => await import('dayjs/locale/bm.js'), + 'bn-bd': async () => await import('dayjs/locale/bn-bd.js'), + bn: async () => await import('dayjs/locale/bn.js'), + bo: async () => await import('dayjs/locale/bo.js'), + br: async () => await import('dayjs/locale/br.js'), + bs: async () => await import('dayjs/locale/bs.js'), + ca: async () => await import('dayjs/locale/ca.js'), + cs: async () => await import('dayjs/locale/cs.js'), + cv: async () => await import('dayjs/locale/cv.js'), + cy: async () => await import('dayjs/locale/cy.js'), + da: async () => await import('dayjs/locale/da.js'), + 'de-at': async () => await import('dayjs/locale/de-at.js'), + 'de-ch': async () => await import('dayjs/locale/de-ch.js'), + de: async () => await import('dayjs/locale/de.js'), + dv: async () => await import('dayjs/locale/dv.js'), + el: async () => await import('dayjs/locale/el.js'), + 'en-au': async () => await import('dayjs/locale/en-au.js'), + 'en-ca': async () => await import('dayjs/locale/en-ca.js'), + 'en-gb': async () => await import('dayjs/locale/en-gb.js'), + 'en-ie': async () => await import('dayjs/locale/en-ie.js'), + 'en-il': async () => await import('dayjs/locale/en-il.js'), + 'en-in': async () => await import('dayjs/locale/en-in.js'), + 'en-nz': async () => await import('dayjs/locale/en-nz.js'), + 'en-sg': async () => await import('dayjs/locale/en-sg.js'), + 'en-tt': async () => await import('dayjs/locale/en-tt.js'), + en: async () => await import('dayjs/locale/en.js'), + eo: async () => await import('dayjs/locale/eo.js'), + 'es-do': async () => await import('dayjs/locale/es-do.js'), + 'es-mx': async () => await import('dayjs/locale/es-mx.js'), + 'es-pr': async () => await import('dayjs/locale/es-pr.js'), + 'es-us': async () => await import('dayjs/locale/es-us.js'), + es: async () => await import('dayjs/locale/es.js'), + et: async () => await import('dayjs/locale/et.js'), + eu: async () => await import('dayjs/locale/eu.js'), + fa: async () => await import('dayjs/locale/fa.js'), + fi: async () => await import('dayjs/locale/fi.js'), + fo: async () => await import('dayjs/locale/fo.js'), + 'fr-ca': async () => await import('dayjs/locale/fr-ca.js'), + 'fr-ch': async () => await import('dayjs/locale/fr-ch.js'), + fr: async () => await import('dayjs/locale/fr.js'), + fy: async () => await import('dayjs/locale/fy.js'), + ga: async () => await import('dayjs/locale/ga.js'), + gd: async () => await import('dayjs/locale/gd.js'), + gl: async () => await import('dayjs/locale/gl.js'), + 'gom-latn': async () => await import('dayjs/locale/gom-latn.js'), + gu: async () => await import('dayjs/locale/gu.js'), + he: async () => await import('dayjs/locale/he.js'), + hi: async () => await import('dayjs/locale/hi.js'), + hr: async () => await import('dayjs/locale/hr.js'), + ht: async () => await import('dayjs/locale/ht.js'), + hu: async () => await import('dayjs/locale/hu.js'), + 'hy-am': async () => await import('dayjs/locale/hy-am.js'), + id: async () => await import('dayjs/locale/id.js'), + is: async () => await import('dayjs/locale/is.js'), + 'it-ch': async () => await import('dayjs/locale/it-ch.js'), + it: async () => await import('dayjs/locale/it.js'), + ja: async () => await import('dayjs/locale/ja.js'), + jv: async () => await import('dayjs/locale/jv.js'), + ka: async () => await import('dayjs/locale/ka.js'), + kk: async () => await import('dayjs/locale/kk.js'), + km: async () => await import('dayjs/locale/km.js'), + kn: async () => await import('dayjs/locale/kn.js'), + ko: async () => await import('dayjs/locale/ko.js'), + ku: async () => await import('dayjs/locale/ku.js'), + ky: async () => await import('dayjs/locale/ky.js'), + lb: async () => await import('dayjs/locale/lb.js'), + lo: async () => await import('dayjs/locale/lo.js'), + lt: async () => await import('dayjs/locale/lt.js'), + lv: async () => await import('dayjs/locale/lv.js'), + me: async () => await import('dayjs/locale/me.js'), + mi: async () => await import('dayjs/locale/mi.js'), + mk: async () => await import('dayjs/locale/mk.js'), + ml: async () => await import('dayjs/locale/ml.js'), + mn: async () => await import('dayjs/locale/mn.js'), + mr: async () => await import('dayjs/locale/mr.js'), + 'ms-my': async () => await import('dayjs/locale/ms-my.js'), + ms: async () => await import('dayjs/locale/ms.js'), + mt: async () => await import('dayjs/locale/mt.js'), + my: async () => await import('dayjs/locale/my.js'), + nb: async () => await import('dayjs/locale/nb.js'), + ne: async () => await import('dayjs/locale/ne.js'), + 'nl-be': async () => await import('dayjs/locale/nl-be.js'), + nl: async () => await import('dayjs/locale/nl.js'), + nn: async () => await import('dayjs/locale/nn.js'), + 'oc-lnc': async () => await import('dayjs/locale/oc-lnc.js'), + 'pa-in': async () => await import('dayjs/locale/pa-in.js'), + pl: async () => await import('dayjs/locale/pl.js'), + 'pt-br': async () => await import('dayjs/locale/pt-br.js'), + pt: async () => await import('dayjs/locale/pt.js'), + rn: async () => await import('dayjs/locale/rn.js'), + ro: async () => await import('dayjs/locale/ro.js'), + ru: async () => await import('dayjs/locale/ru.js'), + rw: async () => await import('dayjs/locale/rw.js'), + sd: async () => await import('dayjs/locale/sd.js'), + se: async () => await import('dayjs/locale/se.js'), + si: async () => await import('dayjs/locale/si.js'), + sk: async () => await import('dayjs/locale/sk.js'), + sl: async () => await import('dayjs/locale/sl.js'), + sq: async () => await import('dayjs/locale/sq.js'), + 'sr-cyrl': async () => await import('dayjs/locale/sr-cyrl.js'), + sr: async () => await import('dayjs/locale/sr.js'), + ss: async () => await import('dayjs/locale/ss.js'), + 'sv-fi': async () => await import('dayjs/locale/sv-fi.js'), + sv: async () => await import('dayjs/locale/sv.js'), + sw: async () => await import('dayjs/locale/sw.js'), + ta: async () => await import('dayjs/locale/ta.js'), + te: async () => await import('dayjs/locale/te.js'), + tet: async () => await import('dayjs/locale/tet.js'), + tg: async () => await import('dayjs/locale/tg.js'), + th: async () => await import('dayjs/locale/th.js'), + tk: async () => await import('dayjs/locale/tk.js'), + 'tl-ph': async () => await import('dayjs/locale/tl-ph.js'), + tlh: async () => await import('dayjs/locale/tlh.js'), + tr: async () => await import('dayjs/locale/tr.js'), + tzl: async () => await import('dayjs/locale/tzl.js'), + 'tzm-latn': async () => await import('dayjs/locale/tzm-latn.js'), + tzm: async () => await import('dayjs/locale/tzm.js'), + 'ug-cn': async () => await import('dayjs/locale/ug-cn.js'), + uk: async () => await import('dayjs/locale/uk.js'), + ur: async () => await import('dayjs/locale/ur.js'), + 'x-pseudo': async () => await import('dayjs/locale/x-pseudo.js'), + yo: async () => await import('dayjs/locale/yo.js'), + uz: async () => await import('dayjs/locale/uz.js'), + 'zh-cn': async () => await import('dayjs/locale/zh-cn.js'), + 'zh-hk': async () => await import('dayjs/locale/zh-hk.js'), + 'uz-latn': async () => await import('dayjs/locale/uz-latn.js'), + 'zh-tw': async () => await import('dayjs/locale/zh-tw.js'), + zh: async () => await import('dayjs/locale/zh.js'), + vi: async () => await import('dayjs/locale/vi.js'), +}; + +export const getLocaleLoader = (locale: string): LocalLoader => { + return localeLoaders[locale]; +};