@@ -2,6 +2,7 @@ import { relative } from 'path'
2
2
import Debug from 'debug'
3
3
import chokidar from 'chokidar'
4
4
import { ResolvedConfig , UpdatePayload , ViteDevServer } from 'vite'
5
+ import { throttle } from '@antfu/utils'
5
6
import { Options , ComponentInfo , ResolvedOptions } from './types'
6
7
import { pascalCase , toArray , getNameFromFilePath , resolveAlias , resolveOptions , matchGlobs , slash } from './utils'
7
8
import { searchComponents } from './fs/glob'
@@ -11,6 +12,7 @@ const debug = {
11
12
components : Debug ( 'vite-plugin-components:context:components' ) ,
12
13
search : Debug ( 'vite-plugin-components:context:search' ) ,
13
14
hmr : Debug ( 'vite-plugin-components:context:hmr' ) ,
15
+ decleration : Debug ( 'vite-plugin-components:decleration' ) ,
14
16
}
15
17
16
18
export class Context {
@@ -19,6 +21,7 @@ export class Context {
19
21
private _componentPaths = new Set < string > ( )
20
22
private _componentNameMap : Record < string , ComponentInfo > = { }
21
23
private _componentUsageMap : Record < string , Set < string > > = { }
24
+ private _componentCustomMap : Record < string , ComponentInfo > = { }
22
25
private _server : ViteDevServer | undefined
23
26
24
27
constructor (
@@ -44,6 +47,8 @@ export class Context {
44
47
}
45
48
} )
46
49
}
50
+
51
+ this . generateDeclaration = throttle ( 500 , false , this . generateDeclaration . bind ( this ) )
47
52
}
48
53
49
54
get root ( ) {
@@ -80,6 +85,11 @@ export class Context {
80
85
return false
81
86
}
82
87
88
+ addCustomComponents ( info : ComponentInfo ) {
89
+ if ( info . name )
90
+ this . _componentCustomMap [ info . name ] = info
91
+ }
92
+
83
93
removeComponents ( paths : string | string [ ] ) {
84
94
debug . components ( 'remove' , paths )
85
95
@@ -144,7 +154,7 @@ export class Context {
144
154
145
155
findComponent ( name : string , excludePaths : string [ ] = [ ] ) : ComponentInfo | undefined {
146
156
// resolve from fs
147
- const info = this . _componentNameMap [ name ]
157
+ let info = this . _componentNameMap [ name ]
148
158
if ( info && ! excludePaths . includes ( info . path ) && ! excludePaths . includes ( info . path . slice ( 1 ) ) )
149
159
return info
150
160
@@ -153,16 +163,20 @@ export class Context {
153
163
const result = resolver ( name )
154
164
if ( result ) {
155
165
if ( typeof result === 'string' ) {
156
- return {
166
+ info = {
157
167
name,
158
168
path : result ,
159
169
}
170
+ this . addCustomComponents ( info )
171
+ return info
160
172
}
161
173
else {
162
- return {
174
+ info = {
163
175
name,
164
176
...result ,
165
177
}
178
+ this . addCustomComponents ( info )
179
+ return info
166
180
}
167
181
}
168
182
}
@@ -207,11 +221,18 @@ export class Context {
207
221
}
208
222
209
223
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 )
212
229
}
213
230
214
231
get componentNameMap ( ) {
215
232
return this . _componentNameMap
216
233
}
234
+
235
+ get componentCustomMap ( ) {
236
+ return this . _componentCustomMap
237
+ }
217
238
}
0 commit comments