diff --git a/src/core/utils.ts b/src/core/utils.ts index 307b7514..76b79fec 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -111,7 +111,7 @@ export function stringifyComponentImport({ as: name, from: path, name: importNam } export function getNameFromFilePath(filePath: string, options: ResolvedOptions): string { - const { resolvedDirs, directoryAsNamespace, globalNamespaces, collapseSamePrefixes, root } = options + const { resolvedDirs, directoryAsNamespace, globalNamespaces, collapseSamePrefixes } = options const parsedFilePath = parse(slash(filePath)) @@ -132,7 +132,7 @@ export function getNameFromFilePath(filePath: string, options: ResolvedOptions): if (filename === 'index' && !directoryAsNamespace) { // when use `globs` option, `resolvedDirs` will always empty, and `folders` will also empty if (isEmpty(folders)) - folders = parsedFilePath.dir.slice(root.length + 1).split('/').filter(Boolean) + folders = parsedFilePath.dir.split('/').filter(Boolean) filename = `${folders.slice(-1)[0]}` return filename diff --git a/test/utils.test.ts b/test/utils.test.ts index 767b9e6d..05658234 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -19,4 +19,14 @@ describe('getNameFromFilePath', () => { const inComponentFilePath = '/src/components/[a1]/b_2/c 3/d.4/[...ef]/ghi.vue' expect(getNameFromFilePath(inComponentFilePath, options as ResolvedOptions)).toBe('a1-b2-c3-d4-ef-ghi') }) + + it('file is out of root path and filename is index', () => { + const options: Partial = { + root: '/root/projects/website', + directoryAsNamespace: false, + resolvedDirs: [], + } + const inComponentFilePath = '/root/libs/ui/Button/index.vue' + expect(getNameFromFilePath(inComponentFilePath, options as ResolvedOptions)).toBe('Button') + }) })