1
+ import Debug from 'debug'
1
2
import type { ComponentInfo , ComponentResolver } from '../../types'
2
- import { kebabCase } from '../utils'
3
+ import { kebabCase , pascalCase } from '../utils'
4
+ const debug = Debug ( 'unplugin-vue-components:resolvers:arco' )
3
5
4
6
const matchComponents = [
5
7
{
@@ -139,6 +141,31 @@ function getComponentStyleDir(importName: string, importStyle: boolean | 'css' |
139
141
return `@arco-design/web-vue/es/${ componentDir } /style/css.js`
140
142
}
141
143
144
+ function canResolveIcons ( options ?: ResolveIconsOption ) : options is AllowResolveIconOption {
145
+ if ( options === undefined )
146
+ return false
147
+ if ( typeof options === 'boolean' )
148
+ return options
149
+ else
150
+ return options . enable
151
+ }
152
+
153
+ function getResolveIconPrefix ( options ?: ResolveIconsOption ) {
154
+ if ( canResolveIcons ( options ) ) {
155
+ if ( typeof options === 'boolean' && options )
156
+ return ''
157
+ else if ( options . enable )
158
+ return options . iconPrefix ?? ''
159
+ else
160
+ return ''
161
+ }
162
+ return ''
163
+ }
164
+
165
+ export type DisallowResolveIconOption = undefined | false | { enable : false }
166
+ export type AllowResolveIconOption = true | { enable : true ; iconPrefix ?: string }
167
+ export type ResolveIconsOption = DisallowResolveIconOption | AllowResolveIconOption
168
+
142
169
export interface ArcoResolverOptions {
143
170
/**
144
171
* import style css or less with components
@@ -151,7 +178,7 @@ export interface ArcoResolverOptions {
151
178
*
152
179
* @default false
153
180
*/
154
- resolveIcons ?: boolean
181
+ resolveIcons ?: ResolveIconsOption
155
182
/**
156
183
* Control style automatic import
157
184
*
@@ -175,10 +202,18 @@ export function ArcoResolver(
175
202
return {
176
203
type : 'component' ,
177
204
resolve : ( name : string ) => {
178
- if ( options . resolveIcons && name . match ( / ^ I c o n / ) ) {
179
- return {
180
- name,
181
- from : '@arco-design/web-vue/es/icon' ,
205
+ if ( canResolveIcons ( options . resolveIcons ) ) {
206
+ const iconPrefix = pascalCase ( getResolveIconPrefix ( options . resolveIcons ) )
207
+ const newNameRegexp = new RegExp ( `^${ iconPrefix } Icon` )
208
+ if ( newNameRegexp . test ( name ) ) {
209
+ debug ( 'found icon component name %s' , name )
210
+ const rawComponentName = name . slice ( iconPrefix . length )
211
+ debug ( 'found icon component raw name %s' , rawComponentName )
212
+ return {
213
+ name : rawComponentName ,
214
+ as : name ,
215
+ from : '@arco-design/web-vue/es/icon' ,
216
+ }
182
217
}
183
218
}
184
219
if ( name . match ( / ^ A [ A - Z ] / ) ) {
0 commit comments