-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathidux.ts
79 lines (68 loc) · 1.9 KB
/
idux.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
import { ComponentResolver } from '../../types'
import { kebabCase } from '../utils'
const specialComponents: Record<string, string> = {
CdkVirtualScroll: 'scroll',
IxAutoComplete: 'auto-complete',
IxBackTop: 'back-top',
IxDatePicker: 'date-picker',
IxCol: 'grid',
IxRow: 'grid',
IxInputNumber: 'input-number',
IxTab: 'tabs',
IxTreeSelect: 'tree-select',
IxTimePicker: 'time-picker',
}
export interface IduxResolverOptions {
/**
* exclude components that do not require automatic import
*
* @default []
*/
exclude?: string[]
/**
* import style along with components
*/
importStyle?: 'css' | 'less'
}
/**
* Resolver for `@idux/cdk`, `@idux/components` and ``@idux/pro``
*
* @link https://idux.site
*/
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
return {
type: 'component',
resolve: (name: string) => {
const { importStyle, exclude = [] } = options
if (exclude.includes(name)) {
return
}
const packageName = getPackageName(name)
if (!packageName) {
return
}
let dirname = specialComponents[name]
if (!dirname) {
const nameIndex = packageName === 'pro' ? 2 : 1
dirname = kebabCase(name).split('-')[nameIndex]
}
const path = `@idux/${packageName}/${dirname}`
let sideEffects: string | undefined
if (packageName !== 'cdk' && importStyle) {
sideEffects = `${path}/style/themes/${importStyle === 'css' ? 'default_css' : 'default'}`
}
return { importName: name, path, sideEffects }
},
}
}
function getPackageName(name: string) {
let packageName: 'cdk' | 'components' | 'pro' | undefined
if (name.match(/^Cdk[A-Z]/)) {
packageName = 'cdk'
} else if (name.match(/^IxPro[A-Z]/)) {
packageName = 'pro'
} else if (name.match(/^Ix[A-Z]/)) {
packageName = 'components'
}
return packageName
}