-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathidux.ts
137 lines (114 loc) · 3.63 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
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
import { resolveModule } from 'local-pkg'
import { compare } from 'compare-versions'
import type { ComponentResolver } from '../../types'
import { getPkgVersion, kebabCase } from '../utils'
// @keep-sorted
const specialComponents: Record<string, string> = {
CdkClickOutside: 'click-outside',
CdkDraggable: 'drag-drop',
CdkResizable: 'resize',
CdkResizableHandle: 'resize',
CdkResizeObserver: 'resize',
CdkVirtualScroll: 'scroll',
IxAutoComplete: 'auto-complete',
IxBackTop: 'back-top',
IxCol: 'grid',
IxDatePicker: 'date-picker',
IxDateRangePicker: 'date-picker',
IxInputNumber: 'input-number',
IxLoadingBar: 'loading-bar',
IxLoadingBarProvider: 'loading-bar',
IxRow: 'grid',
IxTab: 'tabs',
IxTimePicker: 'time-picker',
IxTimeRangePicker: 'time-picker',
IxTreeSelect: 'tree-select',
}
export interface IduxResolverOptions {
/**
* exclude components that do not require automatic import
*
* @default []
*/
exclude?: string[]
/**
* import style along with components
*/
importStyle?: 'css' | 'less'
/**
* theme for import style
*
* @default 'default' for 1.x version
*/
importStyleTheme?: string
/**
* The scope of the packages.
*
* @default '@idux'
*/
scope?: string
/**
* specify idux version to load style
*
* @default installed version
*/
version?: string
}
/**
* Resolver for `@idux/cdk`, `@idux/components` and ``@idux/pro``
*
* @link https://idux.site
*/
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
return {
type: 'component',
resolve: async (name: string) => {
const { importStyle, importStyleTheme, exclude = [], scope = '@idux' } = options
if (exclude.includes(name))
return
const packageName = getPackageName(name)
if (!packageName)
return
const resolvedVersion = await getPkgVersion(`${scope}/${packageName}`, '2.0.0')
let dirname = specialComponents[name]
if (!dirname) {
const nameIndex = packageName === 'pro' ? 2 : 1
dirname = kebabCase(name).split('-')[nameIndex]
}
const path = `${scope}/${packageName}/${dirname}`
const sideEffects = packageName === 'cdk' ? undefined : getSideEffects(resolvedVersion, path, importStyle, importStyleTheme)
return { name, from: 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
}
function getSideEffects(version: string, path: string, importStyle?: 'css' | 'less', importStyleTheme?: string): string | string[] | undefined {
if (!importStyle)
return
if (compare(version, '2.0.0-beta.0', '<'))
return getLegacySideEffects(path, importStyle, importStyleTheme)
const styleRoot = `${path}/style`
const themeRoot = `${path}/theme`
const styleImport = `${styleRoot}/${importStyle === 'css' ? 'index_css' : 'index'}`
if (!resolveModule(styleImport))
return
const themeImport = `${themeRoot}/${importStyleTheme}.css`
if (!importStyleTheme || !resolveModule(themeImport))
return styleImport
return [styleImport, `${themeRoot}/${importStyleTheme}`]
}
function getLegacySideEffects(path: string, importStyle: 'css' | 'less', importStyleTheme: string = 'default'): string | undefined {
const styleImport = `${path}/style/themes/${importStyle === 'css' ? `${importStyleTheme}_css` : importStyleTheme}`
if (!resolveModule(styleImport))
return
return styleImport
}