@@ -2,6 +2,7 @@ import { relative } from 'path'
22import Debug from 'debug'
33import chokidar from 'chokidar'
44import { ResolvedConfig , UpdatePayload , ViteDevServer } from 'vite'
5+ import { throttle } from '@antfu/utils'
56import { Options , ComponentInfo , ResolvedOptions } from './types'
67import { pascalCase , toArray , getNameFromFilePath , resolveAlias , resolveOptions , matchGlobs , slash } from './utils'
78import { searchComponents } from './fs/glob'
@@ -11,6 +12,7 @@ const debug = {
1112 components : Debug ( 'vite-plugin-components:context:components' ) ,
1213 search : Debug ( 'vite-plugin-components:context:search' ) ,
1314 hmr : Debug ( 'vite-plugin-components:context:hmr' ) ,
15+ decleration : Debug ( 'vite-plugin-components:decleration' ) ,
1416}
1517
1618export class Context {
@@ -19,6 +21,7 @@ export class Context {
1921 private _componentPaths = new Set < string > ( )
2022 private _componentNameMap : Record < string , ComponentInfo > = { }
2123 private _componentUsageMap : Record < string , Set < string > > = { }
24+ private _componentCustomMap : Record < string , ComponentInfo > = { }
2225 private _server : ViteDevServer | undefined
2326
2427 constructor (
@@ -44,6 +47,8 @@ export class Context {
4447 }
4548 } )
4649 }
50+
51+ this . generateDeclaration = throttle ( 500 , false , this . generateDeclaration . bind ( this ) )
4752 }
4853
4954 get root ( ) {
@@ -80,6 +85,11 @@ export class Context {
8085 return false
8186 }
8287
88+ addCustomComponents ( info : ComponentInfo ) {
89+ if ( info . name )
90+ this . _componentCustomMap [ info . name ] = info
91+ }
92+
8393 removeComponents ( paths : string | string [ ] ) {
8494 debug . components ( 'remove' , paths )
8595
@@ -144,7 +154,7 @@ export class Context {
144154
145155 findComponent ( name : string , excludePaths : string [ ] = [ ] ) : ComponentInfo | undefined {
146156 // resolve from fs
147- const info = this . _componentNameMap [ name ]
157+ let info = this . _componentNameMap [ name ]
148158 if ( info && ! excludePaths . includes ( info . path ) && ! excludePaths . includes ( info . path . slice ( 1 ) ) )
149159 return info
150160
@@ -153,16 +163,20 @@ export class Context {
153163 const result = resolver ( name )
154164 if ( result ) {
155165 if ( typeof result === 'string' ) {
156- return {
166+ info = {
157167 name,
158168 path : result ,
159169 }
170+ this . addCustomComponents ( info )
171+ return info
160172 }
161173 else {
162- return {
174+ info = {
163175 name,
164176 ...result ,
165177 }
178+ this . addCustomComponents ( info )
179+ return info
166180 }
167181 }
168182 }
@@ -207,11 +221,18 @@ export class Context {
207221 }
208222
209223 generateDeclaration ( ) {
210- if ( this . options . globalComponentsDeclaration )
211- generateDeclaration ( this , this . options . root , this . options . globalComponentsDeclaration )
224+ if ( ! this . options . globalComponentsDeclaration )
225+ return
226+
227+ debug . decleration ( 'generating' )
228+ generateDeclaration ( this , this . options . root , this . options . globalComponentsDeclaration )
212229 }
213230
214231 get componentNameMap ( ) {
215232 return this . _componentNameMap
216233 }
234+
235+ get componentCustomMap ( ) {
236+ return this . _componentCustomMap
237+ }
217238}
0 commit comments