1
1
import { relative } from 'path'
2
2
import Debug from 'debug'
3
- import { ComponentsInfo , ComponentsImportMap , Options , ComponentResolver } from './types'
4
- import { normalize , toArray , getNameFromFilePath , resolveAlias } from './utils'
3
+ import { ComponentInfo , ComponentsImportMap , ResolvedOptions } from './types'
4
+ import { pascalCase , toArray , getNameFromFilePath , resolveAlias , kebabCase } from './utils'
5
5
import { searchComponents } from './fs/glob'
6
6
7
7
const debug = {
@@ -12,17 +12,15 @@ export class Context {
12
12
readonly globs : string [ ]
13
13
14
14
private _componentPaths = new Set < string > ( )
15
- private _componentNameMap : Record < string , ComponentsInfo > = { }
15
+ private _componentNameMap : Record < string , ComponentInfo > = { }
16
16
private _imports : ComponentsImportMap = { }
17
17
private _importsResolveTasks : Record < string , [ ( null | Promise < string [ ] > ) , ( null | ( ( result : string [ ] ) => void ) ) ] > = { }
18
- private _resolvers : ComponentResolver [ ]
19
18
20
19
constructor (
21
- public readonly options : Options ,
20
+ public readonly options : ResolvedOptions ,
22
21
) {
23
- const { extensions, dirs, deep, customComponentResolvers } = options
22
+ const { extensions, dirs, deep } = options
24
23
const exts = toArray ( extensions )
25
- this . _resolvers = toArray ( customComponentResolvers )
26
24
27
25
if ( ! exts . length )
28
26
throw new Error ( '[vite-plugin-components] extensions are required to search for components' )
@@ -76,7 +74,7 @@ export class Context {
76
74
Array
77
75
. from ( this . _componentPaths )
78
76
. forEach ( ( path ) => {
79
- const name = normalize ( getNameFromFilePath ( path , this . options ) )
77
+ const name = pascalCase ( getNameFromFilePath ( path , this . options ) )
80
78
if ( this . _componentNameMap [ name ] ) {
81
79
console . warn ( `[vite-plugin-components] component "${ name } "(${ path } ) has naming conflicts with other components, ignored.` )
82
80
return
@@ -85,17 +83,27 @@ export class Context {
85
83
} )
86
84
}
87
85
88
- findComponent ( name : string , excludePaths : string [ ] = [ ] ) {
86
+ findComponent ( name : string , excludePaths : string [ ] = [ ] ) : ComponentInfo | undefined {
89
87
// resolve from fs
90
88
const info = this . _componentNameMap [ name ]
91
89
if ( info && ! excludePaths . includes ( info . path ) && ! excludePaths . includes ( info . path . slice ( 1 ) ) )
92
90
return info
93
91
94
92
// custom resolvers
95
- for ( const resolver of this . _resolvers ) {
96
- const path = resolver ( name )
97
- if ( path )
98
- return { name, path }
93
+ for ( const resolver of this . options . customComponentResolvers ) {
94
+ const result = resolver ( name )
95
+ if ( result ) {
96
+ if ( typeof result === 'string' ) {
97
+ return { name, path : result }
98
+ }
99
+ else {
100
+ return {
101
+ name,
102
+ path : result . path ,
103
+ importName : result . importName ,
104
+ }
105
+ }
106
+ }
99
107
}
100
108
101
109
return undefined
@@ -104,7 +112,7 @@ export class Context {
104
112
findComponents ( names : string [ ] , excludePaths : string [ ] = [ ] ) {
105
113
return names
106
114
. map ( name => this . findComponent ( name , excludePaths ) )
107
- . filter ( Boolean ) as ComponentsInfo [ ]
115
+ . filter ( Boolean ) as ComponentInfo [ ]
108
116
}
109
117
110
118
normalizePath ( path : string ) {
@@ -122,7 +130,7 @@ export class Context {
122
130
}
123
131
124
132
setImports ( key : string , names : string [ ] ) {
125
- const casedNames = names . map ( name => normalize ( name ) )
133
+ const casedNames = names . map ( name => pascalCase ( name ) )
126
134
this . _imports [ key ] = casedNames
127
135
if ( this . _importsResolveTasks [ key ] )
128
136
this . _importsResolveTasks [ key ] [ 1 ] ?.( casedNames )
0 commit comments