-
-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathtdesign.ts
73 lines (61 loc) · 1.75 KB
/
tdesign.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
import type { ComponentResolver } from '../../types'
export interface TDesignResolverOptions {
/**
* select the specified library
* @default 'vue'
*/
library?: 'vue' | 'vue-next' | 'react' | 'mobile-vue' | 'mobile-react'
/**
* resolve `tdesign-icons'
* @default false
*/
resolveIcons?: boolean
/**
* whether to import ESM version
* @default false
*/
esm?: boolean
/**
* exclude component name, if match do not resolve the name
*
*/
exclude?: string | RegExp | (string | RegExp)[]
}
export function TDesignResolver(options: TDesignResolverOptions = {}): ComponentResolver {
const pluginList = ['DialogPlugin', 'LoadingPlugin', 'MessagePlugin', 'NotifyPlugin']
return {
type: 'component',
resolve: (name: string) => {
const { library = 'vue', exclude } = options
const importFrom = options.esm ? '/esm' : ''
if (options.exclude && isExclude(name, exclude))
return
if (options.resolveIcons && name.match(/[a-z]Icon$/)) {
return {
name,
from: `tdesign-icons-${library}${importFrom}`,
}
}
if (name.match(/^T[A-Z]/) || pluginList.includes(name)) {
const importName = name.match(/^T[A-Z]/) ? name.slice(1) : name
return {
name: importName,
from: `tdesign-${library}${importFrom}`,
}
}
},
}
}
function isExclude(name: string, exclude: string | RegExp | (string | RegExp)[] | undefined): boolean {
if (typeof exclude === 'string')
return name === exclude
if (exclude instanceof RegExp)
return !!name.match(exclude)
if (Array.isArray(exclude)) {
for (const item of exclude) {
if (name === item || name.match(item))
return true
}
}
return false
}