Skip to content

Commit ac5be69

Browse files
committed
feat: generate components json
1 parent e9233dc commit ac5be69

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,13 +34,24 @@ 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.sourcemap = rawOptions.sourcemap ?? true
4344
this.generateDeclaration = throttle(500, this._generateDeclaration.bind(this), { noLeading: false })
45+
46+
if (this.options.dumpUnimportComponents) {
47+
const dumpUnimportComponents = this.options.dumpUnimportComponents === true
48+
? './.unimport-components.json'
49+
: this.options.dumpUnimportComponents ?? false
50+
51+
this.dumpUnimportComponentsPath = dumpUnimportComponents
52+
this.generateComponentsJson = throttle(500, this._generateComponentsJson.bind(this), { noLeading: false })
53+
}
54+
4455
this.setTransformer(this.options.transformer)
4556
}
4657

@@ -288,14 +299,26 @@ export class Context {
288299
if (!this.options.dts)
289300
return
290301

291-
debug.declaration('generating')
302+
debug.declaration('generating dts')
292303
return writeDeclaration(this, this.options.dts, removeUnused)
293304
}
294305

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

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

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
@@ -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.dumpUnimportComponents && ctx.dumpUnimportComponentsPath) {
74+
if (!existsSync(ctx.dumpUnimportComponentsPath))
75+
ctx.generateComponentsJson()
76+
}
77+
7278
if (config.build.watch && config.command === 'build')
7379
ctx.setupWatcher(chokidar.watch(ctx.options.globs))
7480
},

0 commit comments

Comments
 (0)