1
- < % if ( options . methods ) { % >
2
- import { < %= options . methods . join ( ', ' ) % > } from 'date-fns'
3
- < % } else { % >
4
- import * as dateFns from 'date-fns'
5
- < % } % >
6
-
7
- < % if ( options . locales ) { % >
8
- import { < %= options . locales . join ( ', ' ) % > } from 'date-fns/locale'
9
- < % } % >
10
-
11
- export default ( ctx , inject ) = > {
12
- < % if ( options . methods ) { % >
1
+ < % if ( options . methods && options . methods . length ) { % > import { < %= options . methods . join ( ', ' ) % > } from 'date-fns'
2
+ < % } else { % > import * as dateFns from 'date-fns' < % } % >
3
+
4
+ const locales = [ ]
5
+
6
+ const importLocale = async ( locale ) => {
7
+ try {
8
+ locales [ locale ] = await import ( `date-fns/locale/${ locale } /index.js` )
9
+ } catch ( e ) {
10
+ //
11
+ }
12
+ }
13
+
14
+ export default async ( ctx , inject ) => {
15
+ < % if ( options . methods && options . methods . length ) { % >
13
16
const dateFns = { < %= options. methods . join ( ', ' ) % > }
14
17
< % } % >
15
18
const dateObj = { ...dateFns }
16
19
20
+ < % if ( options . locales && options . locales . length ) { % >
21
+ < %= options . locales . map ( l => `await importLocale('${ l } ')` ) . join ( '\n' ) % >
22
+ < % } % >
23
+
17
24
function parsedDate(date) {
18
25
if ( dateFns . parseISO ) {
19
26
return typeof date !== 'string' ? date : dateFns . parseISO ( date )
@@ -77,10 +84,6 @@ export default (ctx, inject) => {
77
84
}
78
85
79
86
function mergeOptions ( options ) {
80
- const locales = [ ]
81
-
82
- < %= options . locales . map ( l => `locales['${ l } '] = ${ l } ` ) . join ( '\n' ) % >
83
-
84
87
< % if ( options . defaultLocale ) { % >
85
88
options = { locale : '<%= options.defaultLocale %>' , ...options }
86
89
< % } % >
@@ -90,13 +93,11 @@ function mergeOptions(options) {
90
93
< % } % >
91
94
92
95
if ( options && typeof options . locale === 'string' ) {
93
- const locale = options . locale . replace ( / [ _ - ] / , '' )
94
- const fallbackLocale = options . fallbackLocale ? options . fallbackLocale . replace ( / [ _ - ] / , '' ) : null
95
- if ( locales [ locale ] ) {
96
- options . locale = locales [ locale ]
97
- } else if (fallbackLocale && locales [ fallbackLocale ] ) {
96
+ if ( locales [ options . locale ] ) {
97
+ options . locale = locales [ options . locale ]
98
+ } else if ( options . fallbackLocale && locales [ options . fallbackLocale ] ) {
98
99
console . warn ( `[date-fns] locale '${ options . locale } ' not found, using fallback locale '${ options . fallbackLocale } '` )
99
- options . locale = locales [ fallbackLocale ]
100
+ options . locale = locales [ options . fallbackLocale ]
100
101
} else {
101
102
console . warn ( `[date-fns] locale '${ options . locale } ' not found.` )
102
103
}
0 commit comments