1- import { ComponentResolver } from '../types'
1+ import { ComponentResolver , SideEffectsInfo } from '../types'
22import { kebabCase } from '../utils'
33
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-
154interface IMatcher {
165 pattern : RegExp
176 styleDir : string
187}
8+
199const matchComponents : IMatcher [ ] = [
2010 {
2111 pattern : / ^ A v a t a r / ,
@@ -156,29 +146,17 @@ const matchComponents: IMatcher[] = [
156146 } ,
157147 {
158148 pattern : / ^ U p l o a d / ,
159- styleDir : 'upload'
160- }
149+ styleDir : 'upload' ,
150+ } ,
161151]
162152
163153export interface AntDesignVueResolverOptions {
164154 /**
165155 * import style along with components
166156 *
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'
180158 */
181- importLess ?: boolean
159+ importStyle ?: boolean | 'css' | 'less'
182160 /**
183161 * resolve `ant-design-vue' icons
184162 *
@@ -187,9 +165,18 @@ export interface AntDesignVueResolverOptions {
187165 * @default false
188166 */
189167 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
190177}
191178
192- const getStyleDir = ( compName : string ) : string => {
179+ function getStyleDir ( compName : string ) : string {
193180 let styleDir
194181 const total = matchComponents . length
195182 for ( let i = 0 ; i < total ; i ++ ) {
@@ -199,45 +186,57 @@ const getStyleDir = (compName: string): string => {
199186 break
200187 }
201188 }
202- if ( ! styleDir ) styleDir = kebabCase ( compName )
189+ if ( ! styleDir )
190+ styleDir = kebabCase ( compName )
203191
204192 return styleDir
205193}
206194
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
212200
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`
222211 }
223212}
224213
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' ,
233230 }
231+ }
234232
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 ) ,
242239 }
243240 }
241+ }
242+ }
0 commit comments