Skip to content

Commit f538eac

Browse files
committed
feat: built-in resolvers for UI libraries
1 parent d1a3c26 commit f538eac

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

examples/vue3/vite.config.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22
import { UserConfig } from 'vite'
33
import Vue from '@vitejs/plugin-vue'
4-
import ViteComponents from 'vite-plugin-components'
4+
import ViteComponents, { VantResolver } from 'vite-plugin-components'
55
import Markdown from 'vite-plugin-md'
66
// @ts-expect-error
77
import SVG from 'vite-plugin-vue-svg'
@@ -27,11 +27,7 @@ const config: UserConfig = {
2727
if (name === 'MyCustom')
2828
return '/src/CustomResolved.vue'
2929
},
30-
// auto import from ui library Vant
31-
(name: string) => {
32-
if (name.startsWith('Van'))
33-
return { importName: name.slice(3), path: 'vant' }
34-
},
30+
VantResolver(),
3531
],
3632
}),
3733
],

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ function VitePluginComponents(options: Options = {}): Plugin {
3838

3939
export * from './helpers/libraryResolver'
4040
export * from './types'
41+
export * from './resolvers'
4142
export { camelCase, pascalCase, kebabCase } from './utils'
4243
export default VitePluginComponents

src/resolvers/antdv.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ComponentResolver } from '../types'
2+
3+
/**
4+
* Resolver for Ant Design Vue
5+
*
6+
* See https://github.com/antfu/vite-plugin-components/issues/26#issuecomment-789767941 for more details
7+
*
8+
* @author @yangss3
9+
* @link https://antdv.com/
10+
*/
11+
export const AntDesignVueResolver = (): ComponentResolver => (name: string) => {
12+
if (name.match(/^A[A-Z]/))
13+
return { importName: name.slice(1), path: 'ant-design-vue/es' }
14+
}

src/resolvers/element-plus.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ComponentResolver } from '../types'
2+
3+
export interface ElementPlusResolverOptions {
4+
/**
5+
* import style along with components
6+
*
7+
* @default true
8+
*/
9+
importStyle?: boolean
10+
}
11+
12+
/**
13+
* Resolver for Element Plus
14+
*
15+
* See https://github.com/antfu/vite-plugin-components/pull/28 for more details
16+
*
17+
* @author @develar
18+
* @link https://element-plus.org/#/en-US
19+
*/
20+
export const ElementPlusResolver = (options: ElementPlusResolverOptions = {}): ComponentResolver => (name: string) => {
21+
const { importStyle = true } = options
22+
if (name.startsWith('El')) {
23+
const partialName = name[2].toLowerCase() + name.substring(3).replace(/[A-Z]/g, l => `-${l.toLowerCase()}`)
24+
return {
25+
path: `element-plus/es/el-${partialName}`,
26+
sideEffects: importStyle ? `element-plus/packages/theme-chalk/src/${partialName}.scss` : undefined,
27+
}
28+
}
29+
}

src/resolvers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './antdv'
2+
export * from './element-plus'
3+
export * from './vant'

src/resolvers/vant.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ComponentResolver } from '../types'
2+
3+
/**
4+
* Resolver for Vant
5+
*
6+
* @link https://github.com/youzan/vant
7+
*/
8+
export const VantResolver = (): ComponentResolver => (name: string) => {
9+
if (name.startsWith('Van'))
10+
return { importName: name.slice(3), path: 'vant' }
11+
}

0 commit comments

Comments
 (0)