Skip to content

Commit ff056b5

Browse files
authored
feat: support antdv css style on demand (#81)
1 parent bcf6694 commit ff056b5

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/resolvers/antdv.ts

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,38 +163,67 @@ const matchComponents: IMatcher[] = [
163163

164164
export interface AntDesignVueResolverOptions {
165165
/**
166-
* import less along with components
166+
* import style along with components
167+
*
168+
* @default true
169+
*/
170+
importStyle?: boolean
171+
/**
172+
* import css along with components
167173
*
168174
* @default true
169175
*/
176+
importCss?: boolean
177+
/**
178+
* import less along with components
179+
*
180+
* @default false
181+
*/
170182
importLess?: boolean
171183
}
172184

185+
const getStyleDir = (compName: string): string => {
186+
let styleDir
187+
const total = matchComponents.length
188+
for (let i = 0; i < total; i++) {
189+
const matcher = matchComponents[i]
190+
if (compName.match(matcher.pattern)) {
191+
styleDir = matcher.styleDir
192+
break
193+
}
194+
}
195+
if (!styleDir) styleDir = kebabCase(compName)
196+
197+
return styleDir
198+
}
199+
200+
const getSideEffects: (
201+
compName: string,
202+
opts: AntDesignVueResolverOptions
203+
) => string | undefined = (compName, opts) => {
204+
const { importStyle = true, importCss = true, importLess = false } = opts
205+
206+
if (importStyle) {
207+
if (importLess) {
208+
const styleDir = getStyleDir(compName)
209+
return `ant-design-vue/es/${styleDir}/style`
210+
}
211+
else if (importCss) {
212+
const styleDir = getStyleDir(compName)
213+
return `ant-design-vue/es/${styleDir}/style/css`
214+
}
215+
}
216+
}
217+
173218
export const AntDesignVueResolver
174219
= (options: AntDesignVueResolverOptions = {}): ComponentResolver =>
175220
(name: string) => {
176221
if (name.match(/^A[A-Z]/)) {
177-
const { importLess = true } = options
178222
const importName = name.slice(1)
179-
let styleDir
180-
if (importLess) {
181-
const total = matchComponents.length
182-
for (let i = 0; i < total; i++) {
183-
const matcher = matchComponents[i]
184-
if (importName.match(matcher.pattern)) {
185-
styleDir = matcher.styleDir
186-
break
187-
}
188-
}
189-
if (!styleDir) styleDir = kebabCase(importName)
190-
}
191-
192223
return {
193224
importName,
194225
path: 'ant-design-vue/es',
195-
sideEffects: importLess
196-
? `ant-design-vue/es/${styleDir}/style`
197-
: undefined,
226+
sideEffects: getSideEffects(importName, options),
198227
}
199228
}
200229
}

0 commit comments

Comments
 (0)