Skip to content

Commit 099d52f

Browse files
committed
chore: refactoring
1 parent f461a31 commit 099d52f

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

src/context.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ComponentsInfo, ComponentsImportMap, Options } from './types'
2+
import { normalize } from './utils'
23

34
export class Context {
45
importMap: ComponentsImportMap = {}
@@ -16,7 +17,11 @@ export class Context {
1617
}
1718

1819
set components(components: ComponentsInfo[]) {
19-
this._components = components.map(([name, path]) => [capitalize(camelize(name)), path])
20+
this._components = components.map(([name, path]) => [normalize(name), path])
21+
}
22+
23+
searchForComponents(names: string[], excludePaths: string[] = []) {
24+
return this.components.filter(i => !excludePaths.includes(i[1]) && names.includes(i[0]))
2025
}
2126

2227
async getImportMap(key: string) {
@@ -35,17 +40,9 @@ export class Context {
3540
}
3641

3742
setImportMap(key: string, names: string[]) {
38-
const casedNames = names.map(name => capitalize(camelize(name)))
43+
const casedNames = names.map(name => normalize(name))
3944
this.importMap[key] = casedNames
4045
if (this.importMapPromises[key])
4146
this.importMapPromises[key][1]?.(casedNames)
4247
}
4348
}
44-
45-
function camelize(str: string) {
46-
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
47-
}
48-
49-
function capitalize(str: string) {
50-
return str.charAt(0).toUpperCase() + str.slice(1)
51-
}

src/fs/glob.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ function getNameFromFilePath(filePath: string): string {
1111
if (parsedFilePath.name === 'index') {
1212
const filePathSegments = filePath.split(path.sep)
1313
const parentDirName = filePathSegments[filePathSegments.length - 2]
14-
if (parentDirName) {
14+
if (parentDirName)
1515
return parentDirName
16-
}
1716
}
1817
return parsedFilePath.name
1918
}

src/generator/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function isResolverPath(reqPath: string) {
1111
export async function generateResolver(ctx: Context, reqPath: string) {
1212
const sfcPath = reqPath.slice(0, -RESOLVER_EXT.length)
1313
const names = await ctx.getImportMap(sfcPath) || []
14-
const components = ctx.components.filter(i => names.includes(i[0]) && i[1] !== sfcPath)
14+
const components = ctx.searchForComponents(names, [sfcPath])
1515

1616
debug(sfcPath)
1717
debug('using', names, 'imported', components.map(i => i[0]))

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function normalize(str: string) {
2+
return capitalize(camelize(str))
3+
}
4+
5+
export function camelize(str: string) {
6+
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
7+
}
8+
9+
export function capitalize(str: string) {
10+
return str.charAt(0).toUpperCase() + str.slice(1)
11+
}

test/fixture/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"dev": "vite",
6+
"dev": "vite --open",
77
"build": "vite build"
88
},
99
"dependencies": {

0 commit comments

Comments
 (0)