Skip to content

Commit 4e7a704

Browse files
authored
feat: use parent directory as name for index components (#2)
1 parent 83c502f commit 4e7a704

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/fs/glob.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import { Context } from '../context'
66

77
const debug = Debug('vite-plugin-components:glob')
88

9+
function getNameFromFilePath(filePath: string): string {
10+
const parsedFilePath = path.parse(filePath)
11+
if (parsedFilePath.name === 'index') {
12+
const filePathSegments = filePath.split(path.sep)
13+
const parentDirName = filePathSegments[filePathSegments.length - 2]
14+
if (parentDirName) {
15+
return parentDirName
16+
}
17+
}
18+
return parsedFilePath.name
19+
}
20+
921
function toArray<T>(arr: T | T[]): T[] {
1022
if (Array.isArray(arr))
1123
return arr
@@ -52,7 +64,7 @@ export async function searchComponents(ctx: Context, force = false) {
5264

5365
const nameSets = new Set<string>()
5466
const components = files
55-
.map((f): ComponentsInfo => [path.parse(f).name, `/${f}`])
67+
.map((f): ComponentsInfo => [getNameFromFilePath(f), `/${f}`])
5668
.filter(([name, path]) => {
5769
if (nameSets.has(name)) {
5870
console.warn(`[vite-plugin-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`)

0 commit comments

Comments
 (0)