Skip to content

Commit 6165baa

Browse files
authored
feat(antdv): support custom date library (#908)
1 parent eaab6c2 commit 6165baa

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/core/resolvers/antdv.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ const matchComponents: IMatcher[] = [
212212
},
213213
]
214214

215+
export type AntDesignVueResolveDateLibrary = 'dayjs' | 'date-fns' | 'moment'
216+
215217
export interface AntDesignVueResolverOptions {
216218
/**
217219
* exclude components that do not require automatic import
@@ -259,6 +261,13 @@ export interface AntDesignVueResolverOptions {
259261
* @default 'A'
260262
*/
261263
prefix?: string
264+
265+
/**
266+
* Custom date library.
267+
*
268+
* @default 'dayjs'
269+
*/
270+
resolveDateLibrary?: AntDesignVueResolveDateLibrary
262271
}
263272

264273
function getStyleDir(compName: string): string {
@@ -320,6 +329,15 @@ function getImportName(compName: string): string {
320329
return compName
321330
}
322331

332+
function getDateLibraryComponent(compName: string) {
333+
if (compName.match(/^Calendar$/))
334+
return 'calendar'
335+
if (compName.match(/^DatePicker$/) || compName.match(/^(?:Month|Week|Range|Quarter)Picker$/))
336+
return 'date-picker'
337+
if (compName.match(/^Time(?:Range)?Picker$/))
338+
return 'time-picker'
339+
}
340+
323341
/**
324342
* Resolver for Ant Design Vue
325343
*
@@ -348,6 +366,20 @@ export function AntDesignVueResolver(options: AntDesignVueResolverOptions = {
348366
if (prefix === originPrefix && isAntdv(compName) && !options?.exclude?.includes(compName)) {
349367
const { cjs = false, packageName = 'ant-design-vue' } = options
350368
const path = `${packageName}/${cjs ? 'lib' : 'es'}`
369+
const dateComponent = getDateLibraryComponent(compName)
370+
if (dateComponent) {
371+
const dateLib = options.resolveDateLibrary ?? 'dayjs'
372+
const dateLibPath = `${path}/${dateComponent}/${dateLib}`
373+
const sideEffects = getSideEffects(compName, options)
374+
if (compName === 'Calendar' || compName === 'DatePicker') {
375+
return { from: dateLibPath, sideEffects }
376+
}
377+
return {
378+
name: getImportName(compName),
379+
from: dateLibPath,
380+
sideEffects,
381+
}
382+
}
351383
return {
352384
name: getImportName(compName),
353385
from: path,

test/resolvers/antdv.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { ComponentResolveResult, ComponentResolverObject } from '../../src'
2+
import { describe, expect, it } from 'vitest'
3+
4+
import { AntDesignVueResolver } from '../../src/resolvers'
5+
6+
describe('antdvResolver', () => {
7+
it('defaults resolveDateLibrary to dayjs for date components', () => {
8+
const resolver = AntDesignVueResolver() as ComponentResolverObject
9+
10+
expect(resolver.resolve('AButton')).toEqual<ComponentResolveResult>({
11+
name: 'Button',
12+
from: 'ant-design-vue/es',
13+
sideEffects: 'ant-design-vue/es/button/style/css',
14+
})
15+
16+
expect(resolver.resolve('ADatePicker')).toEqual<ComponentResolveResult>({
17+
from: 'ant-design-vue/es/date-picker/dayjs',
18+
sideEffects: 'ant-design-vue/es/date-picker/style/css',
19+
})
20+
21+
expect(resolver.resolve('ARangePicker')).toEqual<ComponentResolveResult>({
22+
name: 'RangePicker',
23+
from: 'ant-design-vue/es/date-picker/dayjs',
24+
sideEffects: 'ant-design-vue/es/date-picker/style/css',
25+
})
26+
27+
expect(resolver.resolve('ATimePicker')).toEqual<ComponentResolveResult>({
28+
name: 'TimePicker',
29+
from: 'ant-design-vue/es/time-picker/dayjs',
30+
sideEffects: 'ant-design-vue/es/time-picker/style/css',
31+
})
32+
})
33+
34+
it('resolveDateLibrary: date-fns uses calendar and date-picker subpaths', () => {
35+
const resolver = AntDesignVueResolver({ resolveDateLibrary: 'date-fns' }) as ComponentResolverObject
36+
37+
expect(resolver.resolve('ADatePicker')).toEqual<ComponentResolveResult>({
38+
from: 'ant-design-vue/es/date-picker/date-fns',
39+
sideEffects: 'ant-design-vue/es/date-picker/style/css',
40+
})
41+
42+
expect(resolver.resolve('ACalendar')).toEqual<ComponentResolveResult>({
43+
from: 'ant-design-vue/es/calendar/date-fns',
44+
sideEffects: 'ant-design-vue/es/calendar/style/css',
45+
})
46+
})
47+
48+
it('resolveDateLibrary with cjs uses lib/ and moment subpath', () => {
49+
const resolver = AntDesignVueResolver({ resolveDateLibrary: 'moment', cjs: true }) as ComponentResolverObject
50+
expect(resolver.resolve('ADatePicker')).toEqual<ComponentResolveResult>({
51+
from: 'ant-design-vue/lib/date-picker/moment',
52+
sideEffects: 'ant-design-vue/lib/date-picker/style/css',
53+
})
54+
})
55+
})

0 commit comments

Comments
 (0)