Skip to content

feat: introduce dumpComponentsInfo option #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.DS_Store
dist
.idea
.components-info.json
components.d.ts
1 change: 1 addition & 0 deletions examples/vite-vue2/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const config: UserConfig = {
Components({
transformer: 'vue2',
dts: 'src/components.d.ts',
dumpComponentsInfo: true,
}),
],
build: {
Expand Down
1 change: 1 addition & 0 deletions examples/vite-vue3/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const config: UserConfig = {
componentPrefix: 'i',
}),
],
dumpComponentsInfo: true,
}),
],
build: {
Expand Down
28 changes: 26 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import process from 'node:process'
import { slash, throttle, toArray } from '@antfu/utils'
import Debug from 'debug'
import { DIRECTIVE_IMPORT_PREFIX } from './constants'
import { writeDeclaration } from './declaration'
import { writeComponentsJson, writeDeclaration } from './declaration'
import { searchComponents } from './fs/glob'
import { resolveOptions } from './options'
import transformer from './transformer'
Expand Down Expand Up @@ -34,13 +34,24 @@ export class Context {
root = process.cwd()
sourcemap: string | boolean = true
alias: Record<string, string> = {}
dumpComponentsInfoPath: string | undefined

constructor(
private rawOptions: Options,
) {
this.options = resolveOptions(rawOptions, this.root)
this.sourcemap = rawOptions.sourcemap ?? true
this.generateDeclaration = throttle(500, this._generateDeclaration.bind(this), { noLeading: false })

if (this.options.dumpComponentsInfo) {
const dumpComponentsInfo = this.options.dumpComponentsInfo === true
? './.components-info.json'
: this.options.dumpComponentsInfo ?? false

this.dumpComponentsInfoPath = dumpComponentsInfo
this.generateComponentsJson = throttle(500, this._generateComponentsJson.bind(this), { noLeading: false })
}

this.setTransformer(this.options.transformer)
}

Expand Down Expand Up @@ -169,6 +180,7 @@ export class Context {

onUpdate(path: string) {
this.generateDeclaration()
this.generateComponentsJson()

if (!this._server)
return
Expand Down Expand Up @@ -288,14 +300,26 @@ export class Context {
if (!this.options.dts)
return

debug.declaration('generating')
debug.declaration('generating dts')
return writeDeclaration(this, this.options.dts, removeUnused)
}

generateDeclaration(removeUnused = !this._server): void {
this._generateDeclaration(removeUnused)
}

_generateComponentsJson(removeUnused = !this._server) {
if (!Object.keys(this._componentNameMap).length)
return

debug.components('generating components-info')
return writeComponentsJson(this, removeUnused)
}

generateComponentsJson(removeUnused = !this._server): void {
this._generateComponentsJson(removeUnused)
}

get componentNameMap() {
return this._componentNameMap
}
Expand Down
19 changes: 19 additions & 0 deletions src/core/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,22 @@ export async function writeDeclaration(ctx: Context, filepath: string, removeUnu
if (code !== originalContent)
await writeFile(filepath, code)
}

export async function writeComponentsJson(ctx: Context, _removeUnused = false) {
if (!ctx.dumpComponentsInfoPath)
return

const components = [
...Object.entries({
...ctx.componentNameMap,
...ctx.componentCustomMap,
}).map(([_, { name, as, from }]) => ({
name: name || 'default',
as,
from,
})),
...resolveTypeImports(ctx.options.types),
]

await writeFile(ctx.dumpComponentsInfoPath, JSON.stringify(components, null, 2))
}
6 changes: 6 additions & 0 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default createUnplugin<Options>((options = {}) => {
try {
const result = await ctx.transform(code, id)
ctx.generateDeclaration()
ctx.generateComponentsJson()
return result
}
catch (e) {
Expand All @@ -69,6 +70,11 @@ export default createUnplugin<Options>((options = {}) => {
ctx.generateDeclaration()
}

if (ctx.options.dumpComponentsInfo && ctx.dumpComponentsInfoPath) {
if (!existsSync(ctx.dumpComponentsInfoPath))
ctx.generateComponentsJson()
}

if (config.build.watch && config.command === 'build')
ctx.setupWatcher(chokidar.watch(ctx.options.globs))
},
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ export interface Options {
* @default true
*/
sourcemap?: boolean

/**
* Save component information into a JSON file for other tools to consume.
* Provide a filepath to save the JSON file.
*
* When set to `true`, it will save to `./.components-info.json`
*
* @default false
*/
dumpComponentsInfo?: boolean | string
}

export type ResolvedOptions = Omit<
Expand Down