Skip to content

Commit d56cff2

Browse files
authored
feat: add excludeNames option
closes #206 closes #678
1 parent 7d89295 commit d56cff2

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Components({
358358
extensions: ['vue'],
359359

360360
// Glob patterns to match file names to be detected as components.
361-
// When specified, the `dirs` and `extensions` options will be ignored.
361+
// When specified, the `dirs`, `extensions`, and `directoryAsNamespace` options will be ignored.
362362
// If you want to exclude components being registered, use negative globs with leading `!`.
363363
globs: ['src/components/*.{vue}'],
364364

@@ -398,10 +398,14 @@ Components({
398398
allowOverrides: false,
399399

400400
// Filters for transforming targets (components to insert the auto import)
401-
// Note these are NOT about including/excluding components registered - use `globs` for that
401+
// Note these are NOT about including/excluding components registered - use `globs` or `excludeNames` for that
402402
include: [/\.vue$/, /\.vue\?vue/],
403403
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
404404

405+
// Filters for component names that will not be imported
406+
// Use for globally imported async components or other conflicts that the plugin cannot detect
407+
excludeNames: [/^Async.+/],
408+
405409
// Vue version of project. It will detect automatically if not specified.
406410
// Acceptable value: 2 | 2.7 | 3
407411
version: 2.7,

src/core/context.ts

+4
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ export class Context {
203203
.from(this._componentPaths)
204204
.forEach((path) => {
205205
const name = pascalCase(getNameFromFilePath(path, this.options))
206+
if (this.options.excludeNames(name)) {
207+
debug.components('exclude', name)
208+
return
209+
}
206210
if (this._componentNameMap[name] && !this.options.allowOverrides) {
207211
console.warn(`[unplugin-vue-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`)
208212
return

src/core/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getPackageInfoSync, isPackageExists } from 'local-pkg'
44
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
55
import { detectTypeImports } from './type-imports/detect'
66

7-
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
7+
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'excludeNames' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
88
dirs: 'src/components',
99
extensions: 'vue',
1010
deep: true,

src/types.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export interface Options {
7171
*/
7272
exclude?: FilterPattern
7373

74+
/**
75+
* RegExp or glob to match component names that will NOT be imported
76+
*/
77+
excludeNames?: FilterPattern
78+
7479
/**
7580
* Relative paths to the directory to search for components.
7681
* @default 'src/components'
@@ -86,7 +91,7 @@ export interface Options {
8691
/**
8792
* Glob patterns to match file names to be detected as components.
8893
*
89-
* When specified, the `dirs` and `extensions` options will be ignored.
94+
* When specified, the `dirs`, `extensions`, and `directoryAsNamespace` options will be ignored.
9095
*/
9196
globs?: string | string[]
9297

@@ -180,7 +185,7 @@ export interface Options {
180185

181186
export type ResolvedOptions = Omit<
182187
Required<Options>,
183-
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration'
188+
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration' | 'excludeNames'
184189
> & {
185190
resolvers: ComponentResolverObject[]
186191
extensions: string[]
@@ -189,6 +194,7 @@ export type ResolvedOptions = Omit<
189194
globs: string[]
190195
dts: string | false
191196
root: string
197+
excludeNames: (id: unknown) => boolean
192198
}
193199

194200
export type ComponentsImportMap = Record<string, string[] | undefined>

0 commit comments

Comments
 (0)