1
1
import compareVersions from 'compare-versions'
2
- import { ComponentResolver , SideEffectsInfo } from '../../types'
2
+ import { ComponentInfo , ComponentResolver , SideEffectsInfo } from '../../types'
3
3
import { getPkgVersion , kebabCase } from '../utils'
4
4
5
5
export interface ElementPlusResolverOptions {
@@ -21,8 +21,17 @@ export interface ElementPlusResolverOptions {
21
21
* @default installed version
22
22
*/
23
23
version ?: string
24
+
25
+ /**
26
+ * auto import for directives
27
+ *
28
+ * @default true
29
+ */
30
+ directives ?: boolean
24
31
}
25
32
33
+ type ElementPlusResolverOptionsResolved = Required < ElementPlusResolverOptions >
34
+
26
35
/**
27
36
* @deprecated
28
37
* @param partialName
@@ -32,9 +41,9 @@ export interface ElementPlusResolverOptions {
32
41
*/
33
42
function getSideEffectsLegacy (
34
43
partialName : string ,
35
- options : ElementPlusResolverOptions ,
44
+ options : ElementPlusResolverOptionsResolved ,
36
45
) : SideEffectsInfo | undefined {
37
- const { importStyle = 'css' } = options
46
+ const { importStyle } = options
38
47
if ( ! importStyle )
39
48
return
40
49
@@ -52,61 +61,99 @@ function getSideEffectsLegacy(
52
61
}
53
62
}
54
63
55
- function getSideEffects ( dirName : string , options : ElementPlusResolverOptions ) : SideEffectsInfo | undefined {
56
- const { importStyle = 'css' , ssr } = options
64
+ function getSideEffects ( dirName : string , options : ElementPlusResolverOptionsResolved ) : SideEffectsInfo | undefined {
65
+ const { importStyle, ssr } = options
57
66
const themeFolder = 'element-plus/theme-chalk'
58
67
const esComponentsFolder = 'element-plus/es/components'
59
68
60
69
if ( importStyle === 'sass' )
61
70
return ssr ? `${ themeFolder } /src/${ dirName } .scss` : `${ esComponentsFolder } /${ dirName } /style/index`
62
-
63
71
else if ( importStyle === true || importStyle === 'css' )
64
72
return ssr ? `${ themeFolder } /el-${ dirName } .css` : `${ esComponentsFolder } /${ dirName } /style/css`
65
73
}
66
74
75
+ function resolveComponent ( name : string , options : ElementPlusResolverOptionsResolved ) : ComponentInfo | undefined {
76
+ if ( ! name . match ( / ^ E l [ A - Z ] / ) )
77
+ return
78
+
79
+ const partialName = kebabCase ( name . slice ( 2 ) ) // ElTableColumn -> table-column
80
+ const { version, ssr } = options
81
+
82
+ // >=1.1.0-beta.1
83
+ if ( compareVersions . compare ( version , '1.1.0-beta.1' , '>=' ) ) {
84
+ return {
85
+ importName : name ,
86
+ path : `element-plus/${ ssr ? 'lib' : 'es' } ` ,
87
+ sideEffects : getSideEffects ( partialName , options ) ,
88
+ }
89
+ }
90
+ // >=1.0.2-beta.28
91
+ else if ( compareVersions . compare ( version , '1.0.2-beta.28' , '>=' ) ) {
92
+ return {
93
+ path : `element-plus/es/el-${ partialName } ` ,
94
+ sideEffects : getSideEffectsLegacy ( partialName , options ) ,
95
+ }
96
+ }
97
+ // for <=1.0.1
98
+ else {
99
+ return {
100
+ path : `element-plus/lib/el-${ partialName } ` ,
101
+ sideEffects : getSideEffectsLegacy ( partialName , options ) ,
102
+ }
103
+ }
104
+ }
105
+
106
+ function resolveDirective ( name : string , options : ElementPlusResolverOptionsResolved ) : ComponentInfo | undefined {
107
+ if ( ! options . directives ) return
108
+
109
+ const directives : Record < string , { importName : string ; styleName : string } > = {
110
+ Loading : { importName : 'ElLoadingDirective' , styleName : 'loading' } ,
111
+ Popover : { importName : 'ElPopoverDirective' , styleName : 'popover' } ,
112
+ InfiniteScroll : { importName : 'ElInfiniteScroll' , styleName : 'infinite-scroll' } ,
113
+ }
114
+
115
+ const directive = directives [ name ]
116
+ if ( ! directive ) return
117
+
118
+ const { version, ssr } = options
119
+
120
+ // >=1.1.0-beta.1
121
+ if ( compareVersions . compare ( version , '1.1.0-beta.1' , '>=' ) ) {
122
+ return {
123
+ importName : directive . importName ,
124
+ path : `element-plus/${ ssr ? 'lib' : 'es' } ` ,
125
+ sideEffects : getSideEffects ( directive . styleName , options ) ,
126
+ }
127
+ }
128
+ }
129
+
67
130
/**
68
131
* Resolver for Element Plus
69
132
*
70
133
* See https://github.com/antfu/vite-plugin-components/pull/28 for more details
71
134
* See https://github.com/antfu/vite-plugin-components/issues/117 for more details
72
135
*
73
- * @author @develar @nabaonan
74
- * @link https://element-plus.org/#/en-US for element-plus
136
+ * @author @develar @nabaonan @sxzz
137
+ * @link https://element-plus.org/ for element-plus
75
138
*
76
139
*/
77
140
export function ElementPlusResolver (
78
141
options : ElementPlusResolverOptions = { } ,
79
142
) : ComponentResolver {
80
- return ( name : string ) => {
81
- if ( name . match ( / ^ E l [ A - Z ] / ) ) {
82
- const {
83
- ssr,
84
- version = getPkgVersion ( 'element-plus' , '1.0.2' ) ,
85
- } = options
86
- const partialName = kebabCase ( name . slice ( 2 ) ) // ElTableColumn->table-column
87
-
88
- // >=1.1.0-beta.1
89
- if ( compareVersions . compare ( version , '1.1.0-beta.1' , '>=' ) ) {
90
- return {
91
- importName : name ,
92
- path : `element-plus/${ ssr ? 'lib' : 'es' } ` ,
93
- sideEffects : getSideEffects ( partialName , options ) ,
94
- }
95
- }
96
- // >=1.0.2-beta.28
97
- else if ( compareVersions . compare ( version , '1.0.2-beta.28' , '>=' ) ) {
98
- return {
99
- path : `element-plus/es/el-${ partialName } ` ,
100
- sideEffects : getSideEffectsLegacy ( partialName , options ) ,
101
- }
102
- }
103
- // for <=1.0.1
104
- else {
105
- return {
106
- path : `element-plus/lib/el-${ partialName } ` ,
107
- sideEffects : getSideEffectsLegacy ( partialName , options ) ,
108
- }
109
- }
143
+ const optionsResolved : ElementPlusResolverOptionsResolved = {
144
+ ssr : false ,
145
+ version : getPkgVersion ( 'element-plus' , '1.1.0-beta.21' ) ,
146
+ importStyle : 'css' ,
147
+ directives : true ,
148
+ ...options ,
149
+ }
150
+
151
+ return ( name : string , type ) => {
152
+ switch ( type ) {
153
+ case 'component' :
154
+ return resolveComponent ( name , optionsResolved )
155
+ case 'directive' :
156
+ return resolveDirective ( name , optionsResolved )
110
157
}
111
158
}
112
159
}
0 commit comments