@@ -17,52 +17,103 @@ function resolveProjectDep(opts: { pkg: any; cwd: string; dep: string }) {
1717}
1818
1919export default ( api : IApi ) => {
20- let pkgPath : string ;
20+ let pkgPath : string = '' ;
2121 try {
2222 pkgPath =
2323 resolveProjectDep ( {
2424 pkg : api . pkg ,
2525 cwd : api . cwd ,
2626 dep : 'element-plus' ,
2727 } ) || dirname ( require . resolve ( 'element-plus/package.json' ) ) ;
28- } catch ( e ) { }
28+ } catch ( _ ) { }
2929
3030 function checkPkgPath ( ) {
3131 if ( ! pkgPath ) {
3232 throw new Error (
33- `Can't find element-plus package. Please install antd first.` ,
33+ `Can't find element-plus package. Please install element-plus first.`
3434 ) ;
3535 }
3636 }
3737
38+ api . describe ( {
39+ key : 'elementPlus' ,
40+ config : {
41+ schema ( { zod } ) {
42+ return zod
43+ . object ( {
44+ importStyle : zod
45+ . union ( [ zod . boolean ( ) , zod . literal ( 'css' ) , zod . literal ( 'sass' ) ] )
46+ . describe ( '样式导入方式。true 或 "css" 导入编译后的 CSS 文件,"sass" 导入 Sass 源文件以支持主题定制,false 不自动导入样式。推荐使用 "sass" 以获得更好的主题定制能力。' )
47+ . default ( true )
48+ . optional ( ) ,
49+ version : zod
50+ . string ( )
51+ . describe ( '手动指定 Element Plus 版本,用于覆盖自动检测的版本。格式如 "2.4.1"、"2.3.8" 等。通常无需配置,插件会自动从 package.json 检测版本。' )
52+ . optional ( ) ,
53+ prefix : zod
54+ . string ( )
55+ . describe ( '组件名称前缀。默认为 "El",对应 "ElButton"、"ElInput" 等组件名。如需自定义可修改此配置,但需确保与实际使用的组件名一致。' )
56+ . default ( 'El' )
57+ . optional ( ) ,
58+ exclude : zod
59+ . array ( zod . string ( ) )
60+ . describe ( '排除自动导入的组件列表。数组元素为组件名称(不含前缀),如 ["Button", "Input"]。被排除的组件需要手动导入。适用于需要自定义导入逻辑的场景。' )
61+ . optional ( ) ,
62+ noStylesComponents : zod
63+ . array ( zod . string ( ) )
64+ . describe ( '无需导入样式的组件列表。这些组件不会自动导入对应的样式文件,适用于一些功能性组件(如 ElMessage、ElNotification 等),它们的样式通常已全局引入。' )
65+ . default ( [ 'ElMessage' , 'ElNotification' , 'ElMessageBox' , 'ElLoading' ] )
66+ . optional ( ) ,
67+ directives : zod
68+ . boolean ( )
69+ . describe ( '是否启用 Element Plus 指令的自动导入。设为 true 时会自动导入 v-loading、v-infinite-scroll 等指令。默认启用以提供完整的 Element Plus 功能。' )
70+ . default ( true )
71+ . optional ( )
72+ } )
73+ . describe ( 'Element Plus 自动导入插件配置。集成 unplugin-vue-components 的 ElementPlusResolver,提供 Element Plus 组件、样式和指令的按需自动导入功能,支持主题定制和性能优化,适用于 Vue 3 项目。' )
74+ . optional ( )
75+ . default ( { } ) ;
76+ }
77+ } ,
78+ enableBy : api . EnableBy . config
79+ } ) ;
80+
81+ checkPkgPath ( ) ;
82+ const detectedVersion = require ( `${ pkgPath } /package.json` ) . version ;
83+
3884 api . modifyAppData ( ( memo ) => {
39- checkPkgPath ( ) ;
40- const version = require ( ` ${ pkgPath } /package.json` ) . version ;
85+ // 使用用户配置的版本,如果没有配置则使用检测到的版本
86+ const finalVersion = api . userConfig . elementPlus ?. version || detectedVersion ;
4187 memo . elementPlus = {
4288 pkgPath,
43- version,
89+ version : finalVersion ,
90+ detectedVersion
4491 } ;
4592 return memo ;
4693 } ) ;
4794
48- api . describe ( {
49- key : 'elementPlus' ,
50- config : {
51- schema ( { zod } ) {
52- return zod . object ( { } ) ;
53- } ,
54- } ,
55- enableBy : api . EnableBy . config ,
56- } ) ;
95+ // 获取用户配置,如果没有配置则使用默认值
96+ const userConfig = api . userConfig . elementPlus || { } ;
97+ const resolverConfig = {
98+ // 样式导入方式:默认为 true
99+ importStyle : userConfig . importStyle !== undefined ? userConfig . importStyle : true ,
100+ // 组件前缀:默认为 'El'
101+ prefix : userConfig . prefix || 'El' ,
102+ // 排除的组件列表
103+ ...( userConfig . exclude && { exclude : userConfig . exclude } ) ,
104+ // 无需导入样式的组件
105+ noStylesComponents : userConfig . noStylesComponents || [ 'ElMessage' , 'ElNotification' , 'ElMessageBox' , 'ElLoading' ] ,
106+ // 是否启用指令
107+ directives : userConfig . directives !== false
108+ } ;
57109
58110 const unComponents = {
59- resolvers : [ ElementPlusResolver ( ) ] ,
111+ resolvers : [
112+ ElementPlusResolver ( resolverConfig )
113+ ]
60114 } ;
61115
62- api . userConfig . autoImport = deepmerge (
63- {
64- unComponents,
65- } ,
66- api . userConfig . autoImport || { } ,
67- ) ;
68- } ;
116+ api . userConfig . autoImport = deepmerge ( {
117+ unComponents
118+ } , api . userConfig . autoImport || { } ) ;
119+ }
0 commit comments