dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Integrate dayjs locales into custom library

Open simonkleinfeld opened this issue 2 years ago • 1 comments

Hi there,

currently we are developing a library which should provide us the opportunity to encapsulate all date and theme specific parts. This is the specific part in the library code. The LocalizationProvider is a material-ui component.

const translateLocale = (): string | undefined => {
    if (locale.startsWith('en'))
      return 'en'
    if (locale.startsWith('de'))
      return 'de'
    if (locale.startsWith('cs'))
      return 'cs'
  }
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale={translateLocale()}>
            {children}
</LocalizationProvider>

But unfortunately we have to import the dayjs locales in all projects that are using our library. Currently it is not possible to import the locales in the library.

import 'dayjs/locale/de'
import 'dayjs/locale/en'
import 'dayjs/locale/cs'

Is there any way to integrate the dayjs library/locales with rollup into the library in such a way, that we don't have to import locales in projects that are using our library?

Thank you a lot,

Simon

simonkleinfeld avatar Jun 23 '22 11:06 simonkleinfeld

It seems like statically importing the locales in a base library does not work. What worked was to use require('dayjs/locale/de'), I guess because it is just a function call which gets evaluated correctly. It might also work with an import('dayjs/locale/de') expression, but I haven't tested that, as we are not targeting es2020 yet.

NiclasThobaben avatar Jul 07 '22 08:07 NiclasThobaben