11import { relative } from 'path'
22import 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'
55import { searchComponents } from './fs/glob'
66
77const debug = {
@@ -12,17 +12,15 @@ export class Context {
1212 readonly globs : string [ ]
1313
1414 private _componentPaths = new Set < string > ( )
15- private _componentNameMap : Record < string , ComponentsInfo > = { }
15+ private _componentNameMap : Record < string , ComponentInfo > = { }
1616 private _imports : ComponentsImportMap = { }
1717 private _importsResolveTasks : Record < string , [ ( null | Promise < string [ ] > ) , ( null | ( ( result : string [ ] ) => void ) ) ] > = { }
18- private _resolvers : ComponentResolver [ ]
1918
2019 constructor (
21- public readonly options : Options ,
20+ public readonly options : ResolvedOptions ,
2221 ) {
23- const { extensions, dirs, deep, customComponentResolvers } = options
22+ const { extensions, dirs, deep } = options
2423 const exts = toArray ( extensions )
25- this . _resolvers = toArray ( customComponentResolvers )
2624
2725 if ( ! exts . length )
2826 throw new Error ( '[vite-plugin-components] extensions are required to search for components' )
@@ -76,7 +74,7 @@ export class Context {
7674 Array
7775 . from ( this . _componentPaths )
7876 . forEach ( ( path ) => {
79- const name = normalize ( getNameFromFilePath ( path , this . options ) )
77+ const name = pascalCase ( getNameFromFilePath ( path , this . options ) )
8078 if ( this . _componentNameMap [ name ] ) {
8179 console . warn ( `[vite-plugin-components] component "${ name } "(${ path } ) has naming conflicts with other components, ignored.` )
8280 return
@@ -85,17 +83,27 @@ export class Context {
8583 } )
8684 }
8785
88- findComponent ( name : string , excludePaths : string [ ] = [ ] ) {
86+ findComponent ( name : string , excludePaths : string [ ] = [ ] ) : ComponentInfo | undefined {
8987 // resolve from fs
9088 const info = this . _componentNameMap [ name ]
9189 if ( info && ! excludePaths . includes ( info . path ) && ! excludePaths . includes ( info . path . slice ( 1 ) ) )
9290 return info
9391
9492 // 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+ }
99107 }
100108
101109 return undefined
@@ -104,7 +112,7 @@ export class Context {
104112 findComponents ( names : string [ ] , excludePaths : string [ ] = [ ] ) {
105113 return names
106114 . map ( name => this . findComponent ( name , excludePaths ) )
107- . filter ( Boolean ) as ComponentsInfo [ ]
115+ . filter ( Boolean ) as ComponentInfo [ ]
108116 }
109117
110118 normalizePath ( path : string ) {
@@ -122,7 +130,7 @@ export class Context {
122130 }
123131
124132 setImports ( key : string , names : string [ ] ) {
125- const casedNames = names . map ( name => normalize ( name ) )
133+ const casedNames = names . map ( name => pascalCase ( name ) )
126134 this . _imports [ key ] = casedNames
127135 if ( this . _importsResolveTasks [ key ] )
128136 this . _importsResolveTasks [ key ] [ 1 ] ?.( casedNames )
0 commit comments