@@ -6,7 +6,7 @@ import process from 'node:process'
66import { slash , throttle , toArray } from '@antfu/utils'
77import Debug from 'debug'
88import { DIRECTIVE_IMPORT_PREFIX } from './constants'
9- import { writeDeclaration } from './declaration'
9+ import { writeComponentsJson , writeDeclaration } from './declaration'
1010import { searchComponents } from './fs/glob'
1111import { resolveOptions } from './options'
1212import transformer from './transformer'
@@ -34,13 +34,24 @@ export class Context {
3434 root = process . cwd ( )
3535 sourcemap : string | boolean = true
3636 alias : Record < string , string > = { }
37+ dumpComponentsInfoPath : string | undefined
3738
3839 constructor (
3940 private rawOptions : Options ,
4041 ) {
4142 this . options = resolveOptions ( rawOptions , this . root )
4243 this . sourcemap = rawOptions . sourcemap ?? true
4344 this . generateDeclaration = throttle ( 500 , this . _generateDeclaration . bind ( this ) , { noLeading : false } )
45+
46+ if ( this . options . dumpComponentsInfo ) {
47+ const dumpComponentsInfo = this . options . dumpComponentsInfo === true
48+ ? './.components-info.json'
49+ : this . options . dumpComponentsInfo ?? false
50+
51+ this . dumpComponentsInfoPath = dumpComponentsInfo
52+ this . generateComponentsJson = throttle ( 500 , this . _generateComponentsJson . bind ( this ) , { noLeading : false } )
53+ }
54+
4455 this . setTransformer ( this . options . transformer )
4556 }
4657
@@ -169,6 +180,7 @@ export class Context {
169180
170181 onUpdate ( path : string ) {
171182 this . generateDeclaration ( )
183+ this . generateComponentsJson ( )
172184
173185 if ( ! this . _server )
174186 return
@@ -288,14 +300,26 @@ export class Context {
288300 if ( ! this . options . dts )
289301 return
290302
291- debug . declaration ( 'generating' )
303+ debug . declaration ( 'generating dts ' )
292304 return writeDeclaration ( this , this . options . dts , removeUnused )
293305 }
294306
295307 generateDeclaration ( removeUnused = ! this . _server ) : void {
296308 this . _generateDeclaration ( removeUnused )
297309 }
298310
311+ _generateComponentsJson ( removeUnused = ! this . _server ) {
312+ if ( ! Object . keys ( this . _componentNameMap ) . length )
313+ return
314+
315+ debug . components ( 'generating components-info' )
316+ return writeComponentsJson ( this , removeUnused )
317+ }
318+
319+ generateComponentsJson ( removeUnused = ! this . _server ) : void {
320+ this . _generateComponentsJson ( removeUnused )
321+ }
322+
299323 get componentNameMap ( ) {
300324 return this . _componentNameMap
301325 }
0 commit comments