Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d86696

Browse files
feat: add components prefix (#850)
Co-authored-by: winches <wincheshe@gmail.com>
1 parent949fdb6 commit6d86696

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
returnimports
3939
}
4040

41+
functionaddComponentPrefix(component:ComponentInfo,prefix?:string){
42+
if(!component.as||!prefix)
43+
returncomponent
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
exportfunctiongetDeclarationImports(ctx:Context,filepath:string):DeclarationImports|undefined{
85+
constprefixComponentNameMap=Object.values(ctx.componentNameMap).map(info=>addComponentPrefix(info,ctx.options.prefix))
7586
constcomponent=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
functionnormalizeResolvers(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+
exportinterfaceGlobalComponents {
50+
CustomPrefixAvatar:typeofimport('./examples/vite-vue3/src/components/global/avatar.vue')['default']
51+
CustomPrefixBook:typeofimport('./examples/vite-vue3/src/components/book/index.vue')['default']
52+
CustomPrefixButton:typeofimport('./examples/vite-vue3/src/components/ui/button.vue')['default']
53+
CustomPrefixCheckbox:typeofimport('./examples/vite-vue3/src/components/ui/nested/checkbox.vue')['default']
54+
CustomPrefixCollapseFolderAndComponentFromRoot:typeofimport('./examples/vite-vue3/src/components/collapse/collapseFolder/CollapseFolderAndComponentFromRoot.vue')['default']
55+
CustomPrefixComponentA:typeofimport('./examples/vite-vue3/src/components/ComponentA.vue')['default']
56+
CustomPrefixComponentAsync:typeofimport('./examples/vite-vue3/src/components/ComponentAsync.vue')['default']
57+
CustomPrefixComponentB:typeofimport('./examples/vite-vue3/src/components/ComponentB.vue')['default']
58+
CustomPrefixComponentC:typeofimport('./examples/vite-vue3/src/components/component-c.vue')['default']
59+
CustomPrefixComponentD:typeofimport('./examples/vite-vue3/src/components/ComponentD.vue')['default']
60+
CustomPrefixFolderAndComponentPartially:typeofimport('./examples/vite-vue3/src/components/collapse/collapseFolder/FolderAndComponentPartially.vue')['default']
61+
CustomPrefixKebabCaseCollapseFile:typeofimport('./examples/vite-vue3/src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue')['default']
62+
CustomPrefixKebabCaseFile:typeofimport('./examples/vite-vue3/src/components/kebab-case/KebabCaseFile.vue')['default']
63+
CustomPrefixRecursive:typeofimport('./examples/vite-vue3/src/components/Recursive.vue')['default']
64+
RouterLink:typeofimport('vue-router')['RouterLink']
65+
RouterView:typeofimport('vue-router')['RouterView']
66+
TestComp:typeofimport('test/component/TestComp')['default']
67+
}
68+
exportinterfaceGlobalDirectives {
69+
vLoading:typeofimport('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+
constroot=path.resolve(__dirname,'../examples/vite-vue3')
89
constresolver:ComponentResolver[]=[
910
{
1011
type:'component',
@@ -189,4 +190,21 @@ declare module 'vue' {
189190
constimports=parseDeclaration(code)
190191
expect(imports).matchSnapshot()
191192
})
193+
194+
it('generate components with prefix',async()=>{
195+
constctx=newContext({
196+
resolvers:resolver,
197+
directives:true,
198+
prefix:'CustomPrefix',
199+
dirs:['src/components'],
200+
})
201+
ctx.setRoot(root)
202+
constcode=`
203+
const _component_test_comp = _resolveComponent("test-comp")
204+
const _directive_loading = _resolveDirective("loading")`
205+
awaitctx.transform(code,'')
206+
207+
constdeclarations=getDeclaration(ctx,'test.d.ts')
208+
expect(declarations).toMatchSnapshot()
209+
})
192210
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp