Skip to content

Commit 1f8da38

Browse files
authored
feat(resolvers): sync idux (#249)
1 parent 26338f8 commit 1f8da38

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

src/core/resolvers/idux.ts

+52-35
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,79 @@
11
import { ComponentResolver } from '../../types'
22
import { kebabCase } from '../utils'
33

4-
const cdkNames = [
5-
'portal',
6-
'resizable',
7-
'virtual-list',
8-
]
9-
10-
const kebabCaseDirnames = [
11-
'virtual-list',
12-
'auto-complete',
13-
'back-top',
14-
'date-picker',
15-
'input-number',
16-
'time-picker',
17-
'tree-select',
18-
]
4+
const specialComponents: Record<string, string> = {
5+
CdkVirtualScroll: 'scroll',
6+
IxAutoComplete: 'auto-complete',
7+
IxBackTop: 'back-top',
8+
IxDatePicker: 'date-picker',
9+
IxCol: 'grid',
10+
IxRow: 'grid',
11+
IxInputNumber: 'input-number',
12+
IxTab: 'tabs',
13+
IxTreeSelect: 'tree-select',
14+
IxTimePicker: 'time-picker',
15+
}
1916

2017
export interface IduxResolverOptions {
18+
/**
19+
* exclude components that do not require automatic import
20+
*
21+
* @default []
22+
*/
23+
exclude?: string[]
2124
/**
2225
* import style along with components
2326
*/
2427
importStyle?: 'css' | 'less'
2528
}
2629

2730
/**
28-
* Resolver for `@idux/cdk` and `@idux/components`
31+
* Resolver for `@idux/cdk`, `@idux/components` and ``@idux/pro``
2932
*
3033
* @link https://idux.site
3134
*/
3235
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
3336
return {
3437
type: 'component',
3538
resolve: (name: string) => {
36-
if (name.match(/^Ix[A-Z]/)) {
37-
const { importStyle } = options
38-
const compName = name.slice(2)
39-
const kebabCaseName = kebabCase(compName)
40-
const isCdk = cdkNames.includes(kebabCaseName)
41-
const packageName = isCdk ? 'cdk' : 'components'
42-
const dirname = getDirname(kebabCaseName)
43-
const path = `@idux/${packageName}/${dirname}`
44-
const sideEffects = isCdk || !importStyle ? undefined : `${path}/style/${importStyle === 'css' ? 'css' : 'index'}`
45-
46-
return { importName: name, path, sideEffects }
39+
const { importStyle, exclude = [] } = options
40+
if (exclude.includes(name)) {
41+
return
42+
}
43+
44+
const packageName = getPackageName(name)
45+
if (!packageName) {
46+
return
47+
}
48+
49+
let dirname = specialComponents[name]
50+
if (!dirname) {
51+
const nameIndex = packageName === 'pro' ? 2 : 1
52+
dirname = kebabCase(name).split('-')[nameIndex]
53+
}
54+
55+
const path = `@idux/${packageName}/${dirname}`
56+
57+
let sideEffects: string | undefined
58+
if (packageName !== 'cdk' && importStyle) {
59+
sideEffects = `${path}/style/themes/${importStyle === 'css' ? 'default_css' : 'default'}`
4760
}
61+
62+
return { importName: name, path, sideEffects }
4863
},
4964
}
5065
}
5166

52-
function getDirname(compName: string): string {
53-
const dirname = kebabCaseDirnames.find(name => compName.startsWith(name))
54-
if (dirname)
55-
return dirname
67+
function getPackageName(name: string) {
68+
let packageName: 'cdk' | 'components' | 'pro' | undefined
5669

57-
const [first] = compName.split('-')
58-
if (first === 'row' || first === 'col')
59-
return 'grid'
70+
if (name.match(/^Cdk[A-Z]/)) {
71+
packageName = 'cdk'
72+
} else if (name.match(/^IxPro[A-Z]/)) {
73+
packageName = 'pro'
74+
} else if (name.match(/^Ix[A-Z]/)) {
75+
packageName = 'components'
76+
}
6077

61-
return first
78+
return packageName
6279
}

0 commit comments

Comments
 (0)