Skip to content

Commit 1f1eb86

Browse files
authored
feat: introduce dumpComponentsInfo option (#830)
1 parent 365b67e commit 1f1eb86

File tree

7 files changed

+64
-2
lines changed

7 files changed

+64
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
.DS_Store
33
dist
44
.idea
5+
.components-info.json
56
components.d.ts

examples/vite-vue2/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config: UserConfig = {
88
Components({
99
transformer: 'vue2',
1010
dts: 'src/components.d.ts',
11+
dumpComponentsInfo: true,
1112
}),
1213
],
1314
build: {

examples/vite-vue3/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const config: UserConfig = {
3737
componentPrefix: 'i',
3838
}),
3939
],
40+
dumpComponentsInfo: true,
4041
}),
4142
],
4243
build: {

src/core/context.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import process from 'node:process'
66
import { slash, throttle, toArray } from '@antfu/utils'
77
import Debug from 'debug'
88
import { DIRECTIVE_IMPORT_PREFIX } from './constants'
9-
import { writeDeclaration } from './declaration'
9+
import { writeComponentsJson, writeDeclaration } from './declaration'
1010
import { searchComponents } from './fs/glob'
1111
import { resolveOptions } from './options'
1212
import 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
}

src/core/declaration.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,22 @@ export async function writeDeclaration(ctx: Context, filepath: string, removeUnu
156156
if (code !== originalContent)
157157
await writeFile(filepath, code)
158158
}
159+
160+
export async function writeComponentsJson(ctx: Context, _removeUnused = false) {
161+
if (!ctx.dumpComponentsInfoPath)
162+
return
163+
164+
const components = [
165+
...Object.entries({
166+
...ctx.componentNameMap,
167+
...ctx.componentCustomMap,
168+
}).map(([_, { name, as, from }]) => ({
169+
name: name || 'default',
170+
as,
171+
from,
172+
})),
173+
...resolveTypeImports(ctx.options.types),
174+
]
175+
176+
await writeFile(ctx.dumpComponentsInfoPath, JSON.stringify(components, null, 2))
177+
}

src/core/unplugin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default createUnplugin<Options>((options = {}) => {
4848
try {
4949
const result = await ctx.transform(code, id)
5050
ctx.generateDeclaration()
51+
ctx.generateComponentsJson()
5152
return result
5253
}
5354
catch (e) {
@@ -69,6 +70,11 @@ export default createUnplugin<Options>((options = {}) => {
6970
ctx.generateDeclaration()
7071
}
7172

73+
if (ctx.options.dumpComponentsInfo && ctx.dumpComponentsInfoPath) {
74+
if (!existsSync(ctx.dumpComponentsInfoPath))
75+
ctx.generateComponentsJson()
76+
}
77+
7278
if (config.build.watch && config.command === 'build')
7379
ctx.setupWatcher(chokidar.watch(ctx.options.globs))
7480
},

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ export interface Options {
205205
* @default true
206206
*/
207207
sourcemap?: boolean
208+
209+
/**
210+
* Save component information into a JSON file for other tools to consume.
211+
* Provide a filepath to save the JSON file.
212+
*
213+
* When set to `true`, it will save to `./.components-info.json`
214+
*
215+
* @default false
216+
*/
217+
dumpComponentsInfo?: boolean | string
208218
}
209219

210220
export type ResolvedOptions = Omit<

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy