Skip to content

Commit 6d86696

Browse files
ilyaliaowinchesHe
andauthored
feat: add components prefix (#850)
Co-authored-by: winches <wincheshe@gmail.com>
1 parent 949fdb6 commit 6d86696

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

src/core/declaration.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export function parseDeclaration(code: string): DeclarationImports | undefined {
3838
return imports
3939
}
4040

41+
function addComponentPrefix(component: ComponentInfo, prefix?: string) {
42+
if (!component.as || !prefix)
43+
return component
44+
45+
return {
46+
...component,
47+
as: `${prefix}${component.as}`,
48+
}
49+
}
50+
4151
/**
4252
* Converts `ComponentInfo` to an array
4353
*
@@ -72,11 +82,10 @@ export interface DeclarationImports {
7282
}
7383

7484
export function getDeclarationImports(ctx: Context, filepath: string): DeclarationImports | undefined {
85+
const prefixComponentNameMap = Object.values(ctx.componentNameMap).map(info => addComponentPrefix(info, ctx.options.prefix))
7586
const component = stringifyComponentsInfo(filepath, [
76-
...Object.values({
77-
...ctx.componentNameMap,
78-
...ctx.componentCustomMap,
79-
}),
87+
...Object.values(ctx.componentCustomMap),
88+
...prefixComponentNameMap,
8089
...resolveTypeImports(ctx.options.types),
8190
], ctx.options.importPathTransform)
8291

src/core/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'ex
2121
importPathTransform: v => v,
2222

2323
allowOverrides: false,
24+
25+
prefix: '',
2426
}
2527

2628
function normalizeResolvers(resolvers: (ComponentResolver | ComponentResolver[])[]): ComponentResolverObject[] {

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ export interface Options {
114114
*/
115115
directoryAsNamespace?: boolean
116116

117+
/**
118+
* Generate components with prefix.
119+
*/
120+
prefix?: string
121+
117122
/**
118123
* Collapse same prefixes (camel-sensitive) of folders and components
119124
* to prevent duplication inside namespaced component name.

test/__snapshots__/dts.test.ts.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ declare module 'vue' {
3636
"
3737
`;
3838

39+
exports[`dts > generate components with prefix 1`] = `
40+
"/* eslint-disable */
41+
// @ts-nocheck
42+
// Generated by unplugin-vue-components
43+
// Read more: https://github.com/vuejs/core/pull/3399
44+
// biome-ignore lint: disable
45+
export {}
46+
47+
/* prettier-ignore */
48+
declare module 'vue' {
49+
export interface GlobalComponents {
50+
CustomPrefixAvatar: typeof import('./examples/vite-vue3/src/components/global/avatar.vue')['default']
51+
CustomPrefixBook: typeof import('./examples/vite-vue3/src/components/book/index.vue')['default']
52+
CustomPrefixButton: typeof import('./examples/vite-vue3/src/components/ui/button.vue')['default']
53+
CustomPrefixCheckbox: typeof import('./examples/vite-vue3/src/components/ui/nested/checkbox.vue')['default']
54+
CustomPrefixCollapseFolderAndComponentFromRoot: typeof import('./examples/vite-vue3/src/components/collapse/collapseFolder/CollapseFolderAndComponentFromRoot.vue')['default']
55+
CustomPrefixComponentA: typeof import('./examples/vite-vue3/src/components/ComponentA.vue')['default']
56+
CustomPrefixComponentAsync: typeof import('./examples/vite-vue3/src/components/ComponentAsync.vue')['default']
57+
CustomPrefixComponentB: typeof import('./examples/vite-vue3/src/components/ComponentB.vue')['default']
58+
CustomPrefixComponentC: typeof import('./examples/vite-vue3/src/components/component-c.vue')['default']
59+
CustomPrefixComponentD: typeof import('./examples/vite-vue3/src/components/ComponentD.vue')['default']
60+
CustomPrefixFolderAndComponentPartially: typeof import('./examples/vite-vue3/src/components/collapse/collapseFolder/FolderAndComponentPartially.vue')['default']
61+
CustomPrefixKebabCaseCollapseFile: typeof import('./examples/vite-vue3/src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue')['default']
62+
CustomPrefixKebabCaseFile: typeof import('./examples/vite-vue3/src/components/kebab-case/KebabCaseFile.vue')['default']
63+
CustomPrefixRecursive: typeof import('./examples/vite-vue3/src/components/Recursive.vue')['default']
64+
RouterLink: typeof import('vue-router')['RouterLink']
65+
RouterView: typeof import('vue-router')['RouterView']
66+
TestComp: typeof import('test/component/TestComp')['default']
67+
}
68+
export interface GlobalDirectives {
69+
vLoading: typeof import('test/directive/Loading')['default']
70+
}
71+
}
72+
"
73+
`;
74+
3975
exports[`dts > getDeclaration 1`] = `
4076
"/* eslint-disable */
4177
// @ts-nocheck

test/dts.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, expect, it } from 'vitest'
55
import { Context } from '../src/core/context'
66
import { getDeclaration, parseDeclaration } from '../src/core/declaration'
77

8+
const root = path.resolve(__dirname, '../examples/vite-vue3')
89
const resolver: ComponentResolver[] = [
910
{
1011
type: 'component',
@@ -189,4 +190,21 @@ declare module 'vue' {
189190
const imports = parseDeclaration(code)
190191
expect(imports).matchSnapshot()
191192
})
193+
194+
it('generate components with prefix', async () => {
195+
const ctx = new Context({
196+
resolvers: resolver,
197+
directives: true,
198+
prefix: 'CustomPrefix',
199+
dirs: ['src/components'],
200+
})
201+
ctx.setRoot(root)
202+
const code = `
203+
const _component_test_comp = _resolveComponent("test-comp")
204+
const _directive_loading = _resolveDirective("loading")`
205+
await ctx.transform(code, '')
206+
207+
const declarations = getDeclaration(ctx, 'test.d.ts')
208+
expect(declarations).toMatchSnapshot()
209+
})
192210
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy