Skip to content

feat: add components prefix #850

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 3 commits into from
Jun 29, 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
17 changes: 13 additions & 4 deletions src/core/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ export function parseDeclaration(code: string): DeclarationImports | undefined {
return imports
}

function addComponentPrefix(component: ComponentInfo, prefix?: string) {
if (!component.as || !prefix)
return component

return {
...component,
as: `${prefix}${component.as}`,
}
}

/**
* Converts `ComponentInfo` to an array
*
Expand Down Expand Up @@ -72,11 +82,10 @@ export interface DeclarationImports {
}

export function getDeclarationImports(ctx: Context, filepath: string): DeclarationImports | undefined {
const prefixComponentNameMap = Object.values(ctx.componentNameMap).map(info => addComponentPrefix(info, ctx.options.prefix))
const component = stringifyComponentsInfo(filepath, [
...Object.values({
...ctx.componentNameMap,
...ctx.componentCustomMap,
}),
...Object.values(ctx.componentCustomMap),
...prefixComponentNameMap,
...resolveTypeImports(ctx.options.types),
], ctx.options.importPathTransform)

Expand Down
2 changes: 2 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'ex
importPathTransform: v => v,

allowOverrides: false,

prefix: '',
}

function normalizeResolvers(resolvers: (ComponentResolver | ComponentResolver[])[]): ComponentResolverObject[] {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export interface Options {
*/
directoryAsNamespace?: boolean

/**
* Generate components with prefix.
*/
prefix?: string

/**
* Collapse same prefixes (camel-sensitive) of folders and components
* to prevent duplication inside namespaced component name.
Expand Down
36 changes: 36 additions & 0 deletions test/__snapshots__/dts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ declare module 'vue' {
"
`;

exports[`dts > generate components with prefix 1`] = `
"/* eslint-disable */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
// biome-ignore lint: disable
export {}

/* prettier-ignore */
declare module 'vue' {
export interface GlobalComponents {
CustomPrefixAvatar: typeof import('./examples/vite-vue3/src/components/global/avatar.vue')['default']
CustomPrefixBook: typeof import('./examples/vite-vue3/src/components/book/index.vue')['default']
CustomPrefixButton: typeof import('./examples/vite-vue3/src/components/ui/button.vue')['default']
CustomPrefixCheckbox: typeof import('./examples/vite-vue3/src/components/ui/nested/checkbox.vue')['default']
CustomPrefixCollapseFolderAndComponentFromRoot: typeof import('./examples/vite-vue3/src/components/collapse/collapseFolder/CollapseFolderAndComponentFromRoot.vue')['default']
CustomPrefixComponentA: typeof import('./examples/vite-vue3/src/components/ComponentA.vue')['default']
CustomPrefixComponentAsync: typeof import('./examples/vite-vue3/src/components/ComponentAsync.vue')['default']
CustomPrefixComponentB: typeof import('./examples/vite-vue3/src/components/ComponentB.vue')['default']
CustomPrefixComponentC: typeof import('./examples/vite-vue3/src/components/component-c.vue')['default']
CustomPrefixComponentD: typeof import('./examples/vite-vue3/src/components/ComponentD.vue')['default']
CustomPrefixFolderAndComponentPartially: typeof import('./examples/vite-vue3/src/components/collapse/collapseFolder/FolderAndComponentPartially.vue')['default']
CustomPrefixKebabCaseCollapseFile: typeof import('./examples/vite-vue3/src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue')['default']
CustomPrefixKebabCaseFile: typeof import('./examples/vite-vue3/src/components/kebab-case/KebabCaseFile.vue')['default']
CustomPrefixRecursive: typeof import('./examples/vite-vue3/src/components/Recursive.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TestComp: typeof import('test/component/TestComp')['default']
}
export interface GlobalDirectives {
vLoading: typeof import('test/directive/Loading')['default']
}
}
"
`;

exports[`dts > getDeclaration 1`] = `
"/* eslint-disable */
// @ts-nocheck
Expand Down
18 changes: 18 additions & 0 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it } from 'vitest'
import { Context } from '../src/core/context'
import { getDeclaration, parseDeclaration } from '../src/core/declaration'

const root = path.resolve(__dirname, '../examples/vite-vue3')
const resolver: ComponentResolver[] = [
{
type: 'component',
Expand Down Expand Up @@ -189,4 +190,21 @@ declare module 'vue' {
const imports = parseDeclaration(code)
expect(imports).matchSnapshot()
})

it('generate components with prefix', async () => {
const ctx = new Context({
resolvers: resolver,
directives: true,
prefix: 'CustomPrefix',
dirs: ['src/components'],
})
ctx.setRoot(root)
const code = `
const _component_test_comp = _resolveComponent("test-comp")
const _directive_loading = _resolveDirective("loading")`
await ctx.transform(code, '')

const declarations = getDeclaration(ctx, 'test.d.ts')
expect(declarations).toMatchSnapshot()
})
})