Skip to content

Commit 6a0fb54

Browse files
committed
fix: warning for components with same name
1 parent b6c3dcb commit 6a0fb54

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The following show the default values of the configuration
7878

7979
```ts
8080
ViteComponents({
81-
// Relative path to the directory to search for components.
81+
// Relative paths to the directory to search for components.
8282
dirs: ['src/components'],
8383
// Valid file extensions for components.
8484
extensions: ['vue'],

src/glob.ts renamed to src/fs/glob.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'path'
22
import fg from 'fast-glob'
33
import Debug from 'debug'
4-
import { ComponentsInfo } from './types'
5-
import { Context } from './context'
4+
import { ComponentsInfo } from '../types'
5+
import { Context } from '../context'
66

77
const debug = Debug('vite-plugin-components:glob')
88

@@ -50,7 +50,19 @@ export async function searchComponents(ctx: Context, force = false) {
5050
if (!files.length)
5151
console.warn('[vite-plugin-components] no components found')
5252

53-
const components: ComponentsInfo[] = files.map(f => [path.parse(f).name, `/${f}`])
53+
const nameSets = new Set<string>()
54+
const components = files
55+
.map((f): ComponentsInfo => [path.parse(f).name, `/${f}`])
56+
.filter(([name, path]) => {
57+
if (nameSets.has(name)) {
58+
console.warn(`[vite-plugin-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`)
59+
return false
60+
}
61+
else {
62+
nameSets.add(name)
63+
return true
64+
}
65+
})
5466

5567
debug(`${components.length} components found.`)
5668
debug(components.map(i => i[0]))

src/plugins/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Plugin } from 'rollup'
22
import { isResolverPath, generateResolver } from '../generator/resolver'
3-
import { Context } from '../types'
4-
import { searchComponents } from '../glob'
3+
import { searchComponents } from '../fs/glob'
4+
import { Context } from '../context'
55

66
export function createRollupPlugin(ctx: Context): Plugin {
77
return {

src/plugins/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ServerPlugin } from 'vite'
2-
import { Context } from '../types'
32
import { isResolverPath, generateResolver } from '../generator/resolver'
4-
import { searchComponents } from '../glob'
3+
import { searchComponents } from '../fs/glob'
4+
import { Context } from '../context'
55

66
export function createServerPlugin(context: Context): ServerPlugin {
77
return ({ app, watcher }) => {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
export interface Options {
55
/**
6-
* Relative path to the directory to search for components.
6+
* Relative paths to the directory to search for components.
77
* @default 'src/components'
88
*/
99
dirs: string | string[]

0 commit comments

Comments
 (0)