Skip to content

Commit ff0f4d0

Browse files
authored
feat: load style on demand (#78)
1 parent 0026020 commit ff0f4d0

File tree

1 file changed

+187
-7
lines changed

1 file changed

+187
-7
lines changed

src/resolvers/antdv.ts

Lines changed: 187 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ComponentResolver } from '../types'
2+
import { kebabCase } from '../utils'
23

34
/**
45
* Resolver for Ant Design Vue
@@ -10,11 +11,190 @@ import { ComponentResolver } from '../types'
1011
* @author @yangss3
1112
* @link https://antdv.com/
1213
*/
13-
export const AntDesignVueResolver = (): ComponentResolver => (name: string) => {
14-
if (name.match(/^A[A-Z]/))
15-
return {
16-
importName: name.slice(1),
17-
path: 'ant-design-vue/es',
18-
sideEffects: "ant-design-vue/es/style",
19-
}
14+
15+
interface IMatcher {
16+
pattern: RegExp
17+
styleDir: string
2018
}
19+
const matchComponents: IMatcher[] = [
20+
{
21+
pattern: /^Avatar/,
22+
styleDir: 'avatar',
23+
},
24+
{
25+
pattern: /^AutoComplete/,
26+
styleDir: 'auto-complete',
27+
},
28+
{
29+
pattern: /^Anchor/,
30+
styleDir: 'anchor',
31+
},
32+
33+
{
34+
pattern: /^Badge/,
35+
styleDir: 'badge',
36+
},
37+
{
38+
pattern: /^Breadcrumb/,
39+
styleDir: 'breadcrumb',
40+
},
41+
{
42+
pattern: /^Button/,
43+
styleDir: 'button',
44+
},
45+
{
46+
pattern: /^Checkbox/,
47+
styleDir: 'checkbox',
48+
},
49+
{
50+
pattern: /^Card/,
51+
styleDir: 'card',
52+
},
53+
{
54+
pattern: /^Collapse/,
55+
styleDir: 'collapse',
56+
},
57+
{
58+
pattern: /^Descriptions/,
59+
styleDir: 'descriptions',
60+
},
61+
{
62+
pattern: /^RangePicker|^WeekPicker|^MonthPicker/,
63+
styleDir: 'date-picker',
64+
},
65+
{
66+
pattern: /^Dropdown/,
67+
styleDir: 'dropdown',
68+
},
69+
70+
{
71+
pattern: /^Form/,
72+
styleDir: 'form',
73+
},
74+
{
75+
pattern: /^InputNumber/,
76+
styleDir: 'input-number',
77+
},
78+
79+
{
80+
pattern: /^Input|^Textarea/,
81+
styleDir: 'input',
82+
},
83+
{
84+
pattern: /^Statistic/,
85+
styleDir: 'statistic',
86+
},
87+
{
88+
pattern: /^CheckableTag/,
89+
styleDir: 'tag',
90+
},
91+
{
92+
pattern: /^Layout/,
93+
styleDir: 'layout',
94+
},
95+
{
96+
pattern: /^Menu|^SubMenu/,
97+
styleDir: 'menu',
98+
},
99+
100+
{
101+
pattern: /^Table/,
102+
styleDir: 'table',
103+
},
104+
{
105+
pattern: /^Radio/,
106+
styleDir: 'radio',
107+
},
108+
109+
{
110+
pattern: /^Image/,
111+
styleDir: 'image',
112+
},
113+
114+
{
115+
pattern: /^List/,
116+
styleDir: 'list',
117+
},
118+
119+
{
120+
pattern: /^Tab/,
121+
styleDir: 'tabs',
122+
},
123+
{
124+
pattern: /^Mentions/,
125+
styleDir: 'mentions',
126+
},
127+
128+
{
129+
pattern: /^Mentions/,
130+
styleDir: 'mentions',
131+
},
132+
133+
{
134+
pattern: /^Step/,
135+
styleDir: 'steps',
136+
},
137+
{
138+
pattern: /^Skeleton/,
139+
styleDir: 'skeleton',
140+
},
141+
142+
{
143+
pattern: /^Select/,
144+
styleDir: 'select',
145+
},
146+
{
147+
pattern: /^TreeSelect/,
148+
styleDir: 'tree-select',
149+
},
150+
{
151+
pattern: /^Tree|^DirectoryTree/,
152+
styleDir: 'tree',
153+
},
154+
{
155+
pattern: /^Typography/,
156+
styleDir: 'typography',
157+
},
158+
{
159+
pattern: /^Timeline/,
160+
styleDir: 'timeline',
161+
},
162+
]
163+
164+
export interface AntDesignVueResolverOptions {
165+
/**
166+
* import less along with components
167+
*
168+
* @default true
169+
*/
170+
importLess?: boolean
171+
}
172+
173+
export const AntDesignVueResolver
174+
= (options: AntDesignVueResolverOptions = {}): ComponentResolver =>
175+
(name: string) => {
176+
if (name.match(/^A[A-Z]/)) {
177+
const { importLess = true } = options
178+
const importName = name.slice(1)
179+
let styleDir
180+
if (importLess) {
181+
const total = matchComponents.length
182+
for (let i = 0; i < total; i++) {
183+
const matcher = matchComponents[i]
184+
if (importName.match(matcher.pattern)) {
185+
styleDir = matcher.styleDir
186+
break
187+
}
188+
}
189+
if (!styleDir) styleDir = kebabCase(importName)
190+
}
191+
192+
return {
193+
importName,
194+
path: 'ant-design-vue/es',
195+
sideEffects: importLess
196+
? `ant-design-vue/es/${styleDir}/style`
197+
: undefined,
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)