Skip to content

Commit cedadb8

Browse files
committed
fix(antdv): restrcuture options api
1 parent 1ed0ad2 commit cedadb8

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

src/resolvers/antdv.ts

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
import { ComponentResolver } from '../types'
1+
import { ComponentResolver, SideEffectsInfo } from '../types'
22
import { kebabCase } from '../utils'
33

4-
/**
5-
* Resolver for Ant Design Vue
6-
*
7-
* Requires [email protected] or later
8-
*
9-
* See https://github.com/antfu/vite-plugin-components/issues/26#issuecomment-789767941 for more details
10-
*
11-
* @author @yangss3
12-
* @link https://antdv.com/
13-
*/
14-
154
interface IMatcher {
165
pattern: RegExp
176
styleDir: string
187
}
8+
199
const matchComponents: IMatcher[] = [
2010
{
2111
pattern: /^Avatar/,
@@ -156,29 +146,17 @@ const matchComponents: IMatcher[] = [
156146
},
157147
{
158148
pattern: /^Upload/,
159-
styleDir: 'upload'
160-
}
149+
styleDir: 'upload',
150+
},
161151
]
162152

163153
export 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(/(Outlined|Filled|TwoTone)$/)) {
229-
return {
230-
importName: name,
231-
path: '@ant-design/icons-vue',
232-
}
214+
/**
215+
* Resolver for Ant Design Vue
216+
*
217+
* Requires [email protected] or later
218+
*
219+
* See https://github.com/antfu/vite-plugin-components/issues/26#issuecomment-789767941 for more details
220+
*
221+
* @author @yangss3
222+
* @link https://antdv.com/
223+
*/
224+
export function AntDesignVueResolver(options: AntDesignVueResolverOptions = {}): ComponentResolver {
225+
return (name: string) => {
226+
if (options.resolveIcons && name.match(/(Outlined|Filled|TwoTone)$/)) {
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

Comments
 (0)