diff --git a/src/scan.ts b/src/scan.ts index 2bdde33..4075017 100644 --- a/src/scan.ts +++ b/src/scan.ts @@ -4,7 +4,13 @@ import { camelCase, kebabCase, upperFirst } from 'lodash' import type { ScanDir, Component } from './types' const LAZY_PREFIX = 'lazy' -const pascalCase = (str: string) => upperFirst(camelCase(str)) +const pascalCase = (str: string) => { + const isFirstCharUppercase = str[0] === str[0].toUpperCase() + const containsHyphens = str.includes('-') + const shouldTransformToPascal = !isFirstCharUppercase || containsHyphens + + return shouldTransformToPascal ? upperFirst(camelCase(str)) : str +} const isWindows = process.platform.startsWith('win') function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDir): number { @@ -14,7 +20,7 @@ function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDi function prefixComponent (prefix: string = '', { pascalName, kebabName, ...rest }: Component): Component { return { pascalName: pascalName.startsWith(prefix) ? pascalName : pascalCase(prefix) + pascalName, - kebabName: kebabName.startsWith(prefix) ? kebabName : kebabCase(prefix) + '-' + kebabName, + kebabName: kebabName.startsWith(prefix) ? kebabName : `${kebabCase(prefix)}-${kebabName}`, ...rest } } @@ -54,6 +60,7 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise< const pascalName = pascalCase(fileName) const kebabName = kebabCase(fileName) + const shortPath = filePath.replace(srcDir, '').replace(/\\/g, '/').replace(/^\//, '') let chunkName = shortPath.replace(extname(shortPath), '') diff --git a/test/fixture/components/PAScal.vue b/test/fixture/components/PAScal.vue new file mode 100644 index 0000000..81f86b0 --- /dev/null +++ b/test/fixture/components/PAScal.vue @@ -0,0 +1,5 @@ + diff --git a/test/fixture/pages/index.vue b/test/fixture/pages/index.vue index 09abd97..4559824 100644 --- a/test/fixture/pages/index.vue +++ b/test/fixture/pages/index.vue @@ -7,5 +7,6 @@ + diff --git a/test/unit/scanner.test.ts b/test/unit/scanner.test.ts index c6c643b..26f45ba 100644 --- a/test/unit/scanner.test.ts +++ b/test/unit/scanner.test.ts @@ -11,6 +11,7 @@ test('scanner', async () => { 'Bar', 'Big', 'Mouse', + 'PAScal', 'Foo', 'Functional', 'FunctionalChild', diff --git a/test/unit/tagExtractor.test.ts b/test/unit/tagExtractor.test.ts index 7a21fce..d8ff3d4 100644 --- a/test/unit/tagExtractor.test.ts +++ b/test/unit/tagExtractor.test.ts @@ -4,7 +4,7 @@ import { extractTags } from '../../src/tagExtractor' test('with template', async () => { const tags = await extractTags(path.resolve('test/fixture/pages/index.vue')) - expect(tags).toEqual(['Header', 'Foo', 'LazyBar', 'BaseButton', 'IconHome', 'MAwesome', 'Functional', 'div']) + expect(tags).toEqual(['Header', 'Foo', 'LazyBar', 'BaseButton', 'IconHome', 'MAwesome', 'Functional', 'PAScal', 'div']) }) test('without template', async () => {