-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
arco.ts
240 lines (228 loc) · 5.53 KB
/
arco.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import Debug from 'debug'
import type { ComponentInfo, ComponentResolver } from '../../types'
import { isExclude, kebabCase, pascalCase } from '../utils'
const debug = Debug('unplugin-vue-components:resolvers:arco')
const matchComponents = [
{
pattern: /^AnchorLink$/,
componentDir: 'anchor',
},
{
pattern: /^AvatarGroup$/,
componentDir: 'avatar',
},
{
pattern: /^BreadcrumbItem$/,
componentDir: 'breadcrumb',
},
{
pattern: /^ButtonGroup$/,
componentDir: 'button',
},
{
pattern: /^(CardMeta|CardGrid)$/,
componentDir: 'card',
},
{
pattern: /^CarouselItem$/,
componentDir: 'carousel',
},
{
pattern: /^CascaderPanel$/,
componentDir: 'cascader',
},
{
pattern: /^CheckboxGroup$/,
componentDir: 'checkbox',
},
{
pattern: /^CollapseItem$/,
componentDir: 'collapse',
},
{
pattern: /^(WeekPicker|MonthPicker|YearPicker|QuarterPicker|RangePicker)$/,
componentDir: 'date-picker',
},
{
pattern: /^DescriptionsItem$/,
componentDir: 'descriptions',
},
{
pattern: /^(Doption|Dgroup|Dsubmenu|DropdownButton)$/,
componentDir: 'dropdown',
},
{
pattern: /^FormItem$/,
componentDir: 'form',
},
{
pattern: /^(Col|Row|GridItem)$/,
componentDir: 'grid',
},
{
pattern: /^(ImagePreview|ImagePreviewGroup)$/,
componentDir: 'image',
},
{
pattern: /^(InputGroup|InputSearch|InputPassword)$/,
componentDir: 'input',
},
{
pattern: /^(LayoutHeader|LayoutContent|LayoutFooter|LayoutSider)$/,
componentDir: 'layout',
},
{
pattern: /^(ListItem|ListItemMeta)$/,
componentDir: 'list',
},
{
pattern: /^(MenuItem|MenuItemGroup|SubMenu)$/,
componentDir: 'menu',
},
{
pattern: /^RadioGroup$/,
componentDir: 'radio',
},
{
pattern: /^(Option|Optgroup)$/,
componentDir: 'select',
},
{
pattern: /^(SkeletonLine|SkeletonShape)$/,
componentDir: 'skeleton',
},
{
pattern: /^Countdown$/,
componentDir: 'statistic',
},
{
pattern: /^Step$/,
componentDir: 'steps',
},
{
pattern: /^(Thead|Td|Th|Tr|Tbody|TableColumn)$/,
componentDir: 'table',
},
{
pattern: /^TagGroup$/,
componentDir: 'tag',
},
{
pattern: /^TabPane$/,
componentDir: 'tabs',
},
{
pattern: /^TimelineItem$/,
componentDir: 'timeline',
},
{
pattern: /^(TypographyParagraph|TypographyTitle|TypographyText)$/,
componentDir: 'typography',
},
]
function getComponentStyleDir(importName: string, importStyle: boolean | 'css' | 'less') {
if (['ConfigProvider', 'Icon'].includes(importName))
return undefined
let componentDir = kebabCase(importName)
for (const item of matchComponents) {
if (item.pattern.test(importName)) {
componentDir = item.componentDir
break
}
}
if (importStyle === 'less')
return `@arco-design/web-vue/es/${componentDir}/style/index.js`
if (importStyle === 'css' || importStyle)
return `@arco-design/web-vue/es/${componentDir}/style/css.js`
}
function canResolveIcons(options?: ResolveIconsOption): options is AllowResolveIconOption {
if (options === undefined)
return false
if (typeof options === 'boolean')
return options
else
return options.enable
}
function getResolveIconPrefix(options?: ResolveIconsOption) {
if (canResolveIcons(options)) {
if (typeof options === 'boolean' && options)
return ''
else if (options.enable)
return options.iconPrefix ?? ''
else
return ''
}
return ''
}
export type DisallowResolveIconOption = undefined | false | { enable: false }
export type AllowResolveIconOption = true | { enable: true, iconPrefix?: string }
export type ResolveIconsOption = DisallowResolveIconOption | AllowResolveIconOption
export interface ArcoResolverOptions {
/**
* exclude components that do not require automatic import
*
* @default []
*/
exclude?: string | RegExp | (string | RegExp)[]
/**
* import style css or less with components
*
* @default 'css'
*/
importStyle?: boolean | 'css' | 'less'
/**
* resolve icons
*
* @default false
*/
resolveIcons?: ResolveIconsOption
/**
* Control style automatic import
*
* @default true
*/
sideEffect?: boolean
}
/**
* Resolver for Arco Design Vue
*
* Requires arco-design/web-vue@2.11.0 or later
*
* @author @flsion
* @link https://arco.design/ for arco-design
*
*/
export function ArcoResolver(
options: ArcoResolverOptions = {},
): ComponentResolver {
return {
type: 'component',
resolve: (name: string) => {
if (canResolveIcons(options.resolveIcons)) {
const iconPrefix = pascalCase(getResolveIconPrefix(options.resolveIcons))
const newNameRegexp = new RegExp(`^${iconPrefix}Icon`)
if (newNameRegexp.test(name)) {
debug('found icon component name %s', name)
const rawComponentName = name.slice(iconPrefix.length)
debug('found icon component raw name %s', rawComponentName)
return {
name: rawComponentName,
as: name,
from: '@arco-design/web-vue/es/icon',
}
}
}
if (name.match(/^A[A-Z]/) && !isExclude(name, options.exclude)) {
const importStyle = options.importStyle ?? 'css'
const importName = name.slice(1)
const config: ComponentInfo = {
name: importName,
from: '@arco-design/web-vue',
}
if (options.sideEffect !== false)
config.sideEffects = getComponentStyleDir(importName, importStyle)
return config
}
},
}
}