1
- import { ComponentResolver } from '../types'
1
+ import { ComponentResolver , SideEffectsInfo } from '../types'
2
2
import { kebabCase } from '../utils'
3
3
4
- /**
5
- * Resolver for Ant Design Vue
6
- *
7
- * Requires ant-design-vue@v2.2.0-beta.6 or later
8
- *
9
- * See https://github.com/antfu/vite-plugin-components/issues/26#issuecomment-789767941 for more details
10
- *
11
- * @author @yangss 3
12
- * @link https://antdv.com/
13
- */
14
-
15
4
interface IMatcher {
16
5
pattern : RegExp
17
6
styleDir : string
18
7
}
8
+
19
9
const matchComponents : IMatcher [ ] = [
20
10
{
21
11
pattern : / ^ A v a t a r / ,
@@ -156,29 +146,17 @@ const matchComponents: IMatcher[] = [
156
146
} ,
157
147
{
158
148
pattern : / ^ U p l o a d / ,
159
- styleDir : 'upload'
160
- }
149
+ styleDir : 'upload' ,
150
+ } ,
161
151
]
162
152
163
153
export interface AntDesignVueResolverOptions {
164
154
/**
165
155
* import style along with components
166
156
*
167
- * @default true
168
- */
169
- importStyle ?: boolean
170
- /**
171
- * import css along with components
172
- *
173
- * @default true
174
- */
175
- importCss ?: boolean
176
- /**
177
- * import less along with components
178
- *
179
- * @default false
157
+ * @default 'css'
180
158
*/
181
- importLess ?: boolean
159
+ importStyle ?: boolean | 'css' | 'less'
182
160
/**
183
161
* resolve `ant-design-vue' icons
184
162
*
@@ -187,9 +165,18 @@ export interface AntDesignVueResolverOptions {
187
165
* @default false
188
166
*/
189
167
resolveIcons ?: boolean
168
+
169
+ /**
170
+ * @deprecated use `importStyle: 'css'` instead
171
+ */
172
+ importCss ?: boolean
173
+ /**
174
+ * @deprecated use `importStyle: 'less'` instead
175
+ */
176
+ importLess ?: boolean
190
177
}
191
178
192
- const getStyleDir = ( compName : string ) : string => {
179
+ function getStyleDir ( compName : string ) : string {
193
180
let styleDir
194
181
const total = matchComponents . length
195
182
for ( let i = 0 ; i < total ; i ++ ) {
@@ -199,45 +186,57 @@ const getStyleDir = (compName: string): string => {
199
186
break
200
187
}
201
188
}
202
- if ( ! styleDir ) styleDir = kebabCase ( compName )
189
+ if ( ! styleDir )
190
+ styleDir = kebabCase ( compName )
203
191
204
192
return styleDir
205
193
}
206
194
207
- const getSideEffects : (
208
- compName : string ,
209
- opts : AntDesignVueResolverOptions
210
- ) => string | undefined = ( compName , opts ) => {
211
- const { importStyle = true , importCss = true , importLess = false } = opts
195
+ function getSideEffects ( compName : string , options : AntDesignVueResolverOptions ) : SideEffectsInfo {
196
+ const {
197
+ importStyle = true ,
198
+ importLess = false ,
199
+ } = options
212
200
213
- if ( importStyle ) {
214
- if ( importLess ) {
215
- const styleDir = getStyleDir ( compName )
216
- return `ant-design-vue/es/${ styleDir } /style`
217
- }
218
- else if ( importCss ) {
219
- const styleDir = getStyleDir ( compName )
220
- return `ant-design-vue/es/${ styleDir } /style/css`
221
- }
201
+ if ( ! importStyle )
202
+ return
203
+
204
+ if ( importStyle === 'less' || importLess ) {
205
+ const styleDir = getStyleDir ( compName )
206
+ return `ant-design-vue/es/${ styleDir } /style`
207
+ }
208
+ else {
209
+ const styleDir = getStyleDir ( compName )
210
+ return `ant-design-vue/es/${ styleDir } /style/css`
222
211
}
223
212
}
224
213
225
- export const AntDesignVueResolver
226
- = ( options : AntDesignVueResolverOptions = { } ) : ComponentResolver =>
227
- ( name : string ) => {
228
- if ( options . resolveIcons && name . match ( / ( O u t l i n e d | F i l l e d | T w o T o n e ) $ / ) ) {
229
- return {
230
- importName : name ,
231
- path : '@ant-design/icons-vue' ,
232
- }
214
+ /**
215
+ * Resolver for Ant Design Vue
216
+ *
217
+ * Requires ant-design-vue@v2.2.0-beta.6 or later
218
+ *
219
+ * See https://github.com/antfu/vite-plugin-components/issues/26#issuecomment-789767941 for more details
220
+ *
221
+ * @author @yangss 3
222
+ * @link https://antdv.com/
223
+ */
224
+ export function AntDesignVueResolver ( options : AntDesignVueResolverOptions = { } ) : ComponentResolver {
225
+ return ( name : string ) => {
226
+ if ( options . resolveIcons && name . match ( / ( O u t l i n e d | F i l l e d | T w o T o n e ) $ / ) ) {
227
+ return {
228
+ importName : name ,
229
+ path : '@ant-design/icons-vue' ,
233
230
}
231
+ }
234
232
235
- if ( name . match ( / ^ A [ A - Z ] / ) ) {
236
- const importName = name . slice ( 1 )
237
- return {
238
- importName,
239
- path : 'ant-design-vue/es' ,
240
- sideEffects : getSideEffects ( importName , options ) ,
241
- }
233
+ if ( name . match ( / ^ A [ A - Z ] / ) ) {
234
+ const importName = name . slice ( 1 )
235
+ return {
236
+ importName,
237
+ path : 'ant-design-vue/es' ,
238
+ sideEffects : getSideEffects ( importName , options ) ,
242
239
}
243
240
}
241
+ }
242
+ }
0 commit comments