Skip to content

fix: correct prefixed component name #871

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 2 commits into from
Aug 6, 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
5 changes: 4 additions & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ export class Context {
Array
.from(this._componentPaths)
.forEach((path) => {
const name = pascalCase(getNameFromFilePath(path, this.options))
const fileName = getNameFromFilePath(path, this.options)
const name = this.options.prefix
? `${pascalCase(this.options.prefix)}${pascalCase(fileName)}`
: pascalCase(fileName)
if (isExclude(name, this.options.excludeNames)) {
debug.components('exclude', name)
return
Expand Down
17 changes: 4 additions & 13 deletions src/core/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ 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 @@ -82,10 +72,11 @@ 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.componentCustomMap),
...prefixComponentNameMap,
...Object.values({
...ctx.componentNameMap,
...ctx.componentCustomMap,
}),
...resolveTypeImports(ctx.options.types),
], ctx.options.importPathTransform)

Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ import __unplugin_components_0 from 'test/component/ElInfiniteScroll';
}
`;

exports[`prefix transform > transform with prefix should work 1`] = `
{
"code": "/* unplugin-vue-components disabled */import __unplugin_components_2 from 'test/component/test-comp.vue';
import __unplugin_components_1 from 'test/component/test-comp.vue';
import __unplugin_components_0 from 'test/component/test-comp.vue';

const render = (_ctx, _cache) => {
const _component_test_comp = __unplugin_components_0
const _component_testComp = __unplugin_components_1
const _component_testComp = __unplugin_components_2

return _withDirectives(
(_openBlock(),
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
_createBlock(_component_testComp, null, null, 512 /* NEED_PATCH */)),
_createBlock(_component_TestComp, null, null, 512 /* NEED_PATCH */))
)
}
",
}
`;

exports[`transform > vue2 transform should work 1`] = `
{
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';
Expand Down
36 changes: 36 additions & 0 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ComponentResolver } from '../src'
import { describe, expect, it } from 'vitest'
import { pascalCase } from '../src'
import { Context } from '../src/core/context'

const resolver: ComponentResolver[] = [
Expand Down Expand Up @@ -173,3 +174,38 @@ describe('component and directive as same name', () => {
expect(await ctx.transform(code, '')).toMatchSnapshot()
})
})

describe('prefix transform', () => {
it('transform with prefix should work', async () => {
const code = `
const render = (_ctx, _cache) => {
const _component_test_comp = _resolveComponent("custom-prefix-test-comp")
const _component_testComp = _resolveComponent("CustomPrefixTestComp")
const _component_testComp = _resolveComponent("customPrefixTestComp")

return _withDirectives(
(_openBlock(),
_createBlock(_component_test_comp, null, null, 512 /* NEED_PATCH */)),
_createBlock(_component_testComp, null, null, 512 /* NEED_PATCH */)),
_createBlock(_component_TestComp, null, null, 512 /* NEED_PATCH */))
)
}
`

const ctx = new Context({
prefix: 'CustomPrefix',
directives: true,
})
ctx.sourcemap = false
const componentName = 'TestComp'
const name = `${pascalCase(ctx.options.prefix)}${pascalCase(componentName)}`
// @ts-expect-error for test
ctx._componentNameMap = {
[name]: {
as: name,
from: 'test/component/test-comp.vue',
},
}
expect(await ctx.transform(code, '')).toMatchSnapshot()
})
})
Loading