Skip to content
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

feat: introduce dumpUnimportComponents option #830

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
.unimport-components.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',
dumpUnimportComponents: 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',
}),
],
dumpUnimportComponents: true,
}),
],
build: {
Expand Down
27 changes: 25 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,12 +34,23 @@ export class Context {
root = process.cwd()
sourcemap: string | boolean = true
alias: Record<string, string> = {}
dumpUnimportComponentsPath: string | undefined

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

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

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

this.setTransformer(this.options.transformer)
}

Expand Down Expand Up @@ -287,14 +298,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.json')
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.dumpUnimportComponentsPath)
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.dumpUnimportComponentsPath, 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 @@ -43,6 +43,7 @@ export default createUnplugin<Options>((options = {}) => {
try {
const result = await ctx.transform(code, id)
ctx.generateDeclaration()
ctx.generateComponentsJson()
return result
}
catch (e) {
Expand All @@ -64,6 +65,11 @@ export default createUnplugin<Options>((options = {}) => {
ctx.generateDeclaration()
}

if (ctx.options.dumpUnimportComponents && ctx.dumpUnimportComponentsPath) {
if (!existsSync(ctx.dumpUnimportComponentsPath))
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 @@ -198,6 +198,16 @@ export interface Options {
* Vue version of project. It will detect automatically if not specified.
*/
version?: 2 | 2.7 | 3

/**
* Save unimport components 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 `./.unimport-components.json`
*
* @default false
*/
dumpUnimportComponents?: boolean | string
}

export type ResolvedOptions = Omit<
Expand Down