Skip to content

Commit b7a55a1

Browse files
committed
feat: generate components json
1 parent f00471a commit b7a55a1

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Diff for: src/core/context.ts

+25-2
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,12 +34,23 @@ export class Context {
3434
root = process.cwd()
3535
sourcemap: string | boolean = true
3636
alias: Record<string, string> = {}
37+
dumpUnimportComponentsPath: string | undefined
3738

3839
constructor(
3940
private rawOptions: Options,
4041
) {
4142
this.options = resolveOptions(rawOptions, this.root)
4243
this.generateDeclaration = throttle(500, this._generateDeclaration.bind(this), { noLeading: false })
44+
45+
if (this.options.dumpUnimportComponents) {
46+
const dumpUnimportComponents = this.options.dumpUnimportComponents === true
47+
? './.unimport-components.json'
48+
: this.options.dumpUnimportComponents ?? false
49+
50+
this.dumpUnimportComponentsPath = dumpUnimportComponents
51+
this.generateComponentsJson = throttle(500, this._generateComponentsJson.bind(this), { noLeading: false })
52+
}
53+
4354
this.setTransformer(this.options.transformer)
4455
}
4556

@@ -287,14 +298,26 @@ export class Context {
287298
if (!this.options.dts)
288299
return
289300

290-
debug.declaration('generating')
301+
debug.declaration('generating dts')
291302
return writeDeclaration(this, this.options.dts, removeUnused)
292303
}
293304

294305
generateDeclaration(removeUnused = !this._server): void {
295306
this._generateDeclaration(removeUnused)
296307
}
297308

309+
_generateComponentsJson(removeUnused = !this._server) {
310+
if (!Object.keys(this._componentNameMap).length)
311+
return
312+
313+
debug.components('generating components.json')
314+
return writeComponentsJson(this, removeUnused)
315+
}
316+
317+
generateComponentsJson(removeUnused = !this._server): void {
318+
this._generateComponentsJson(removeUnused)
319+
}
320+
298321
get componentNameMap() {
299322
return this._componentNameMap
300323
}

Diff for: src/core/declaration.ts

+19
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.dumpUnimportComponentsPath)
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.dumpUnimportComponentsPath, JSON.stringify(components, null, 2))
177+
}

Diff for: src/core/unplugin.ts

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default createUnplugin<Options>((options = {}) => {
4343
try {
4444
const result = await ctx.transform(code, id)
4545
ctx.generateDeclaration()
46+
ctx.generateComponentsJson()
4647
return result
4748
}
4849
catch (e) {
@@ -64,6 +65,11 @@ export default createUnplugin<Options>((options = {}) => {
6465
ctx.generateDeclaration()
6566
}
6667

68+
if (ctx.options.dumpUnimportComponents && ctx.dumpUnimportComponentsPath) {
69+
if (!existsSync(ctx.dumpUnimportComponentsPath))
70+
ctx.generateComponentsJson()
71+
}
72+
6773
if (config.build.watch && config.command === 'build')
6874
ctx.setupWatcher(chokidar.watch(ctx.options.globs))
6975
},

0 commit comments

Comments
 (0)