Skip to content

Commit 336b862

Browse files
authored
fix: correct prefixed component name (#871)
1 parent 7b2ead2 commit 336b862

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/core/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ export class Context {
215215
Array
216216
.from(this._componentPaths)
217217
.forEach((path) => {
218-
const name = pascalCase(getNameFromFilePath(path, this.options))
218+
const fileName = getNameFromFilePath(path, this.options)
219+
const name = this.options.prefix
220+
? `${pascalCase(this.options.prefix)}${pascalCase(fileName)}`
221+
: pascalCase(fileName)
219222
if (isExclude(name, this.options.excludeNames)) {
220223
debug.components('exclude', name)
221224
return

src/core/declaration.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ 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-
5141
/**
5242
* Converts `ComponentInfo` to an array
5343
*
@@ -82,10 +72,11 @@ export interface DeclarationImports {
8272
}
8373

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

test/__snapshots__/transform.test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ import __unplugin_components_0 from 'test/component/ElInfiniteScroll';
6363
}
6464
`;
6565

66+
exports[`prefix transform > transform with prefix should work 1`] = `
67+
{
68+
"code": "/* unplugin-vue-components disabled */import __unplugin_components_2 from 'test/component/test-comp.vue';
69+
import __unplugin_components_1 from 'test/component/test-comp.vue';
70+
import __unplugin_components_0 from 'test/component/test-comp.vue';
71+
72+
const render = (_ctx, _cache) => {
73+
const _component_test_comp = __unplugin_components_0
74+
const _component_testComp = __unplugin_components_1
75+
const _component_testComp = __unplugin_components_2
76+
77+
return _withDirectives(
78+
(_openBlock(),
79+
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
80+
_createBlock(_component_testComp, null, null, 512 /* NEED_PATCH */)),
81+
_createBlock(_component_TestComp, null, null, 512 /* NEED_PATCH */))
82+
)
83+
}
84+
",
85+
}
86+
`;
87+
6688
exports[`transform > vue2 transform should work 1`] = `
6789
{
6890
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';

test/transform.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ComponentResolver } from '../src'
22
import { describe, expect, it } from 'vitest'
3+
import { pascalCase } from '../src'
34
import { Context } from '../src/core/context'
45

56
const resolver: ComponentResolver[] = [
@@ -173,3 +174,38 @@ describe('component and directive as same name', () => {
173174
expect(await ctx.transform(code, '')).toMatchSnapshot()
174175
})
175176
})
177+
178+
describe('prefix transform', () => {
179+
it('transform with prefix should work', async () => {
180+
const code = `
181+
const render = (_ctx, _cache) => {
182+
const _component_test_comp = _resolveComponent("custom-prefix-test-comp")
183+
const _component_testComp = _resolveComponent("CustomPrefixTestComp")
184+
const _component_testComp = _resolveComponent("customPrefixTestComp")
185+
186+
return _withDirectives(
187+
(_openBlock(),
188+
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
189+
_createBlock(_component_testComp, null, null, 512 /* NEED_PATCH */)),
190+
_createBlock(_component_TestComp, null, null, 512 /* NEED_PATCH */))
191+
)
192+
}
193+
`
194+
195+
const ctx = new Context({
196+
prefix: 'CustomPrefix',
197+
directives: true,
198+
})
199+
ctx.sourcemap = false
200+
const componentName = 'TestComp'
201+
const name = `${pascalCase(ctx.options.prefix)}${pascalCase(componentName)}`
202+
// @ts-expect-error for test
203+
ctx._componentNameMap = {
204+
[name]: {
205+
as: name,
206+
from: 'test/component/test-comp.vue',
207+
},
208+
}
209+
expect(await ctx.transform(code, '')).toMatchSnapshot()
210+
})
211+
})

0 commit comments

Comments
 (0)