Skip to content

Commit 1ed0ad2

Browse files
nabaonanantfu
andauthored
feat: element-ui resolver (#102)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 664daa6 commit 1ed0ad2

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/resolvers/element-ui.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
ComponentResolver,
3+
SideEffectsInfo,
4+
} from '../types'
5+
import { kebabCase } from '../utils'
6+
7+
export interface ElementUiResolverOptions {
8+
/**
9+
* import style css or sass with components
10+
*
11+
* @default 'css'
12+
*/
13+
importStyle?: boolean | 'css' | 'sass'
14+
}
15+
16+
function getSideEffects(
17+
partialName: string,
18+
options: ElementUiResolverOptions,
19+
): SideEffectsInfo | undefined {
20+
const { importStyle = 'css' } = options
21+
22+
if (!importStyle)
23+
return
24+
25+
if (importStyle === 'sass') {
26+
return [
27+
'element-ui/packages/theme-chalk/src/base.scss',
28+
`element-ui/packages/theme-chalk/src/${partialName}.scss`,
29+
]
30+
}
31+
else {
32+
return [
33+
'element-ui/lib/theme-chalk/base.css',
34+
`element-ui/lib/theme-chalk/${partialName}.css`,
35+
]
36+
}
37+
}
38+
39+
/**
40+
* Resolver for Element-UI
41+
* @link https://element.eleme.cn/#/zh-CN
42+
* @version @element-ui ^2.15.3, @vue ^2.6.14
43+
* @author @nabaonan
44+
*/
45+
export const ElementUiResolver = (options: ElementUiResolverOptions = {}): ComponentResolver =>
46+
(name: string) => {
47+
if (name.startsWith('El')) {
48+
const compName = name.slice(2)
49+
const partialName = kebabCase(compName)
50+
return {
51+
path: `element-ui/lib/${partialName}`,
52+
sideEffects: getSideEffects(partialName, options),
53+
}
54+
}
55+
}

src/resolvers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export * from './vuetify'
66
export * from './vueuse'
77
export * from './naive-ui'
88
export * from './varlet-ui'
9+
export * from './element-ui'
910
export * from './prime-vue'
1011
export * from './view-ui'

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ export interface ImportInfo {
66
path: string
77
}
88

9+
export type SideEffectsInfo = (ImportInfo | string)[] | ImportInfo | string
10+
911
export interface ComponentInfo extends ImportInfo {
10-
sideEffects?: (ImportInfo | string)[] | ImportInfo | string
12+
sideEffects?: SideEffectsInfo
1113
}
1214

1315
export type ComponentResolveResult = string | ComponentInfo

0 commit comments

Comments
 (0)