@@ -3,7 +3,12 @@ import { deepmerge, resolve } from '@winner-fed/utils';
33import type { IApi } from '@winner-fed/winjs' ;
44import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers' ;
55
6- function resolveProjectDep ( opts : { pkg : any ; cwd : string ; dep : string } ) {
6+ interface PackageJson {
7+ dependencies ?: Record < string , string > ;
8+ devDependencies ?: Record < string , string > ;
9+ }
10+
11+ function resolveProjectDep ( opts : { pkg : PackageJson ; cwd : string ; dep : string } ) {
712 if (
813 opts . pkg . dependencies ?. [ opts . dep ] ||
914 opts . pkg . devDependencies ?. [ opts . dep ]
@@ -16,37 +21,92 @@ function resolveProjectDep(opts: { pkg: any; cwd: string; dep: string }) {
1621 }
1722}
1823
24+ function compareVersion ( version : string , targetVersion : string ) : number {
25+ const parseVersion = ( v : string ) => {
26+ // 解析版本号,处理 beta/alpha/rc 等预发布版本
27+ const match = v . match ( / ^ ( \d + ) \. ( \d + ) \. ( \d + ) (?: - ( .+ ?) \. ( \d + ) ) ? / ) ;
28+ if ( ! match ) return { major : 0 , minor : 0 , patch : 0 , prerelease : '' , prereleaseNum : 0 } ;
29+
30+ return {
31+ major : parseInt ( match [ 1 ] , 10 ) ,
32+ minor : parseInt ( match [ 2 ] , 10 ) ,
33+ patch : parseInt ( match [ 3 ] , 10 ) ,
34+ prerelease : match [ 4 ] || '' ,
35+ prereleaseNum : parseInt ( match [ 5 ] || '0' , 10 )
36+ } ;
37+ } ;
38+
39+ const v1 = parseVersion ( version ) ;
40+ const v2 = parseVersion ( targetVersion ) ;
41+
42+ // 比较主版本号
43+ if ( v1 . major !== v2 . major ) return v1 . major - v2 . major ;
44+ // 比较次版本号
45+ if ( v1 . minor !== v2 . minor ) return v1 . minor - v2 . minor ;
46+ // 比较修订版本号
47+ if ( v1 . patch !== v2 . patch ) return v1 . patch - v2 . patch ;
48+
49+ // 如果都没有预发布版本,则相等
50+ if ( ! v1 . prerelease && ! v2 . prerelease ) return 0 ;
51+
52+ // 正式版本大于预发布版本
53+ if ( ! v1 . prerelease && v2 . prerelease ) return 1 ;
54+ if ( v1 . prerelease && ! v2 . prerelease ) return - 1 ;
55+
56+ // 比较预发布版本
57+ if ( v1 . prerelease !== v2 . prerelease ) {
58+ return v1 . prerelease . localeCompare ( v2 . prerelease ) ;
59+ }
60+
61+ // 比较预发布版本号
62+ return v1 . prereleaseNum - v2 . prereleaseNum ;
63+ }
64+
65+ function checkVersionCompatibility ( version : string ) {
66+ const minRequiredVersion = '2.2.0-beta.6' ;
67+
68+ // 检查是否满足最低版本要求
69+ if ( compareVersion ( version , minRequiredVersion ) < 0 ) {
70+ console . warn (
71+ `[winjs-plugin-antdv] 警告:当前 ant-design-vue 版本 (${ version } ) 不支持 AntDesignVueResolver。\n` +
72+ `AntDesignVueResolver 需要 ant-design-vue@${ minRequiredVersion } 或更高版本。\n` +
73+ `请升级您的 ant-design-vue 版本:npm install ant-design-vue@latest`
74+ ) ;
75+ return false ;
76+ }
77+
78+ return true ;
79+ }
80+
1981export default ( api : IApi ) => {
2082 let pkgPath : string = '' ;
2183 try {
2284 pkgPath =
2385 resolveProjectDep ( {
2486 pkg : api . pkg ,
2587 cwd : api . cwd ,
26- dep : 'ant-design-vue' ,
88+ dep : 'ant-design-vue'
2789 } ) || dirname ( require . resolve ( 'ant-design-vue/package.json' ) ) ;
28- } catch ( e ) {
29- throw new Error (
30- `Can't find ant-design-vue package. Please install antd first.` ,
31- ) ;
90+ } catch ( _ ) {
3291 }
3392
3493 function checkPkgPath ( ) {
3594 if ( ! pkgPath ) {
3695 throw new Error (
37- `Can't find ant-design-vue package. Please install antd first.` ,
96+ `Can't find ant-design-vue package. Please install antd first.`
3897 ) ;
3998 }
4099 }
41100
42101 const antdvVersion = require ( `${ pkgPath } /package.json` ) . version ;
43- const isAntdv2 = antdvVersion ?. startsWith ( '1.' ) ;
44102
45103 api . modifyAppData ( ( memo ) => {
46104 checkPkgPath ( ) ;
105+ // 检查版本兼容性
106+ checkVersionCompatibility ( antdvVersion ) ;
47107 memo . antdv = {
48108 pkgPath,
49- version : antdvVersion ,
109+ version : antdvVersion
50110 } ;
51111 return memo ;
52112 } ) ;
@@ -55,25 +115,68 @@ export default (api: IApi) => {
55115 key : 'antdv' ,
56116 config : {
57117 schema ( { zod } ) {
58- return zod . object ( { } ) ;
59- } ,
118+ return zod
119+ . object ( {
120+ exclude : zod
121+ . array ( zod . string ( ) )
122+ . describe ( '排除自动导入的组件列表。数组元素为组件名称(不含前缀),如 ["Button", "Input"]。被排除的组件需要手动导入。' )
123+ . default ( [ ] )
124+ . optional ( ) ,
125+ importStyle : zod
126+ . union ( [ zod . boolean ( ) , zod . literal ( 'css' ) , zod . literal ( 'less' ) , zod . literal ( 'css-in-js' ) ] )
127+ . describe ( '样式导入方式。true 或 "css" 导入编译后的 CSS 文件,"less" 导入 Less 源文件以支持主题定制,"css-in-js" 导入 CSS-in-JS 样式,false 不自动导入样式。默认为 "css"。' )
128+ . optional ( ) ,
129+ resolveIcons : zod
130+ . boolean ( )
131+ . describe ( '是否自动解析和导入 Ant Design Vue 的图标组件。设为 true 时,将自动解析并导入图标组件,无需手动导入。需要安装 @ant-design/icons-vue 包。默认为 false。' )
132+ . default ( false )
133+ . optional ( ) ,
134+ cjs : zod
135+ . boolean ( )
136+ . describe ( '是否使用 CommonJS 构建版本。设为 true 时使用 CommonJS 构建,false 使用 ES 模块构建。默认为 false。' )
137+ . default ( false )
138+ . optional ( ) ,
139+ packageName : zod
140+ . string ( )
141+ . describe ( '重命名包名。用于指定 ant-design-vue 包的别名或自定义包名。默认为 "ant-design-vue"。' )
142+ . default ( 'ant-design-vue' )
143+ . optional ( ) ,
144+ prefix : zod
145+ . string ( )
146+ . describe ( '组件名称前缀。默认为 "A",对应 "AButton"、"AInput" 等组件名。如需自定义可修改此配置。' )
147+ . default ( 'A' )
148+ . optional ( )
149+ } )
150+ . describe ( 'Ant Design Vue 自动导入插件配置。集成 unplugin-vue-components 的 AntDesignVueResolver,提供 Ant Design Vue 组件和样式的按需自动导入功能,支持 Vue2 和 Vue3 版本。' )
151+ . optional ( )
152+ . default ( { } ) ;
153+ }
60154 } ,
61- enableBy : api . EnableBy . config ,
155+ enableBy : api . EnableBy . config
62156 } ) ;
63157
158+ // 获取用户配置,如果没有配置则使用默认值
159+ const userConfig = api . config . antdv || { } ;
160+ const resolverConfig = {
161+ // 排除的组件列表:默认为空数组
162+ ...( userConfig . exclude && userConfig . exclude . length > 0 && { exclude : userConfig . exclude } ) ,
163+ // 样式导入方式:用户配置优先,否则使用默认值 'css'
164+ importStyle : userConfig . importStyle !== undefined ? userConfig . importStyle : 'css' ,
165+ // 是否解析图标:默认为 false
166+ resolveIcons : userConfig . resolveIcons !== undefined ? userConfig . resolveIcons : false ,
167+ // 是否使用 CommonJS:默认为 false
168+ ...( userConfig . cjs !== undefined && { cjs : userConfig . cjs } ) ,
169+ // 包名:默认为 'ant-design-vue'
170+ ...( userConfig . packageName && userConfig . packageName !== 'ant-design-vue' && { packageName : userConfig . packageName } ) ,
171+ // 组件前缀:默认为 'A'
172+ ...( userConfig . prefix && userConfig . prefix !== 'A' && { prefix : userConfig . prefix } )
173+ } ;
174+
64175 const unComponents = {
65- resolvers : [
66- AntDesignVueResolver ( {
67- // vue2 为 true
68- importStyle : isAntdv2 ,
69- } ) ,
70- ] ,
176+ resolvers : [ AntDesignVueResolver ( resolverConfig ) ]
71177 } ;
72178
73- api . userConfig . autoImport = deepmerge (
74- {
75- unComponents,
76- } ,
77- api . userConfig . autoImport || { } ,
78- ) ;
79- } ;
179+ api . userConfig . autoImport = deepmerge ( {
180+ unComponents
181+ } , api . userConfig . autoImport || { } ) ;
182+ }
0 commit comments