Skip to content

Commit

Permalink
feat: add excludeNames option
Browse files Browse the repository at this point in the history
closes #206
closes #678
  • Loading branch information
KaelWD authored Aug 4, 2024
1 parent 7d89295 commit d56cff2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Components({
extensions: ['vue'],

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

Expand Down Expand Up @@ -398,10 +398,14 @@ Components({
allowOverrides: false,

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

// Filters for component names that will not be imported
// Use for globally imported async components or other conflicts that the plugin cannot detect
excludeNames: [/^Async.+/],

// Vue version of project. It will detect automatically if not specified.
// Acceptable value: 2 | 2.7 | 3
version: 2.7,
Expand Down
4 changes: 4 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export class Context {
.from(this._componentPaths)
.forEach((path) => {
const name = pascalCase(getNameFromFilePath(path, this.options))
if (this.options.excludeNames(name)) {

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/search.test.ts > search > should work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:19:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/search.test.ts > search > should with namespace

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:30:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/search.test.ts > search > should with namespace & collapse

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:42:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/search.test.ts > search > should globs exclude work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:55:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/search.test.ts > search > should globs exclude work with dirs

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:68:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/search.test.ts > search > should work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:19:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/search.test.ts > search > should with namespace

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:30:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/search.test.ts > search > should with namespace & collapse

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:42:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/search.test.ts > search > should globs exclude work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:55:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/search.test.ts > search > should globs exclude work with dirs

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:68:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/search.test.ts > search > should work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:19:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/search.test.ts > search > should with namespace

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:30:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/search.test.ts > search > should with namespace & collapse

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:42:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/search.test.ts > search > should globs exclude work

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:55:9

Check failure on line 206 in src/core/context.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/search.test.ts > search > should globs exclude work with dirs

TypeError: this.options.excludeNames is not a function ❯ src/core/context.ts:206:26 ❯ Context.updateComponentNameMap src/core/context.ts:204:8 ❯ Context.addComponents src/core/context.ts:141:12 ❯ Module.searchComponents src/core/fs/glob.ts:23:7 ❯ Context.searchGlob src/core/context.ts:281:5 ❯ test/search.test.ts:68:9
debug.components('exclude', name)
return
}
if (this._componentNameMap[name] && !this.options.allowOverrides) {
console.warn(`[unplugin-vue-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`)
return
Expand Down
2 changes: 1 addition & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getPackageInfoSync, isPackageExists } from 'local-pkg'
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
import { detectTypeImports } from './type-imports/detect'

export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'excludeNames' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
dirs: 'src/components',
extensions: 'vue',
deep: true,
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export interface Options {
*/
exclude?: FilterPattern

/**
* RegExp or glob to match component names that will NOT be imported
*/
excludeNames?: FilterPattern

/**
* Relative paths to the directory to search for components.
* @default 'src/components'
Expand All @@ -86,7 +91,7 @@ export interface Options {
/**
* Glob patterns to match file names to be detected as components.
*
* When specified, the `dirs` and `extensions` options will be ignored.
* When specified, the `dirs`, `extensions`, and `directoryAsNamespace` options will be ignored.
*/
globs?: string | string[]

Expand Down Expand Up @@ -180,7 +185,7 @@ export interface Options {

export type ResolvedOptions = Omit<
Required<Options>,
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration'
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration' | 'excludeNames'
> & {
resolvers: ComponentResolverObject[]
extensions: string[]
Expand All @@ -189,6 +194,7 @@ export type ResolvedOptions = Omit<
globs: string[]
dts: string | false
root: string
excludeNames: (id: unknown) => boolean
}

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

0 comments on commit d56cff2

Please sign in to comment.