Skip to content

Commit 467c356

Browse files
committed
use existing isExclude function
1 parent 87f38d0 commit 467c356

File tree

9 files changed

+44
-55
lines changed

9 files changed

+44
-55
lines changed

src/core/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { UpdatePayload, ViteDevServer } from 'vite'
66
import { slash, throttle, toArray } from '@antfu/utils'
77
import type { ComponentInfo, Options, ResolvedOptions, Transformer } from '../types'
88
import { DIRECTIVE_IMPORT_PREFIX } from './constants'
9-
import { getNameFromFilePath, matchGlobs, normalizeComponentInfo, parseId, pascalCase, resolveAlias } from './utils'
9+
import { getNameFromFilePath, isExclude, matchGlobs, normalizeComponentInfo, parseId, pascalCase, resolveAlias } from './utils'
1010
import { resolveOptions } from './options'
1111
import { searchComponents } from './fs/glob'
1212
import { writeDeclaration } from './declaration'
@@ -203,7 +203,7 @@ export class Context {
203203
.from(this._componentPaths)
204204
.forEach((path) => {
205205
const name = pascalCase(getNameFromFilePath(path, this.options))
206-
if (this.options.excludeNames(name)) {
206+
if (isExclude(name, this.options.excludeNames)) {
207207
debug.components('exclude', name)
208208
return
209209
}

src/core/options.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { join, resolve } from 'node:path'
22
import { slash, toArray } from '@antfu/utils'
33
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
4-
import { createFilter } from '@rollup/pluginutils'
54
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
65
import { detectTypeImports } from './type-imports/detect'
76

@@ -35,7 +34,6 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
3534
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
3635
resolved.resolvers = normalizeResolvers(resolved.resolvers)
3736
resolved.extensions = toArray(resolved.extensions)
38-
resolved.excludeNames = createFilter(undefined, resolved.excludeNames, { resolve: false })
3937

4038
if (resolved.globs) {
4139
resolved.globs = toArray(resolved.globs).map((glob: string) => slash(resolveGlobsExclude(root, glob)))

src/core/resolvers/_utils.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/core/resolvers/arco.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Debug from 'debug'
22
import type { ComponentInfo, ComponentResolver } from '../../types'
3-
import { kebabCase, pascalCase } from '../utils'
4-
import { isExclude } from './_utils'
3+
import { isExclude, kebabCase, pascalCase } from '../utils'
54

65
const debug = Debug('unplugin-vue-components:resolvers:arco')
76

src/core/resolvers/layui-vue.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { FilterPattern } from '@rollup/pluginutils'
2+
import { isExclude } from '../utils'
13
import type { ComponentInfo, ComponentResolver, SideEffectsInfo } from '../../types'
24

35
const matchComponents = [
@@ -95,7 +97,7 @@ export interface LayuiVueResolverOptions {
9597
* exclude components that do not require automatic import
9698
*
9799
*/
98-
exclude?: Array<string | RegExp>
100+
exclude?: FilterPattern
99101
}
100102

101103
const layuiRE = /^Lay[A-Z]/
@@ -132,7 +134,7 @@ function getSideEffects(importName: string, options: LayuiVueResolverOptions): S
132134
function resolveComponent(importName: string, options: LayuiVueResolverOptions): ComponentInfo | undefined {
133135
let name: string | undefined
134136

135-
if (options.exclude && isExclude(importName, options.exclude))
137+
if (isExclude(importName, options.exclude))
136138
return undefined
137139

138140
if (options.resolveIcons && importName.match(iconsRE)) {
@@ -152,14 +154,6 @@ function resolveComponent(importName: string, options: LayuiVueResolverOptions):
152154
: undefined
153155
}
154156

155-
function isExclude(name: string, exclude: Array<string | RegExp>): boolean {
156-
for (const item of exclude) {
157-
if (name === item || name.match(item))
158-
return true
159-
}
160-
return false
161-
}
162-
163157
/**
164158
* Resolver for layui-vue
165159
*

src/core/resolvers/tdesign.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { FilterPattern } from '@rollup/pluginutils'
12
import type { ComponentResolver } from '../../types'
3+
import { isExclude } from '../utils'
24

35
export interface TDesignResolverOptions {
46
/**
@@ -23,7 +25,7 @@ export interface TDesignResolverOptions {
2325
* exclude component name, if match do not resolve the name
2426
*
2527
*/
26-
exclude?: string | RegExp | (string | RegExp)[]
28+
exclude?: FilterPattern
2729
}
2830

2931
export function TDesignResolver(options: TDesignResolverOptions = {}): ComponentResolver {
@@ -34,7 +36,7 @@ export function TDesignResolver(options: TDesignResolverOptions = {}): Component
3436
const { library = 'vue', exclude } = options
3537
const importFrom = options.esm ? '/esm' : ''
3638

37-
if (options.exclude && isExclude(name, exclude))
39+
if (isExclude(name, exclude))
3840
return
3941

4042
if (options.resolveIcons && name.match(/[a-z]Icon$/)) {
@@ -55,19 +57,3 @@ export function TDesignResolver(options: TDesignResolverOptions = {}): Component
5557
},
5658
}
5759
}
58-
59-
function isExclude(name: string, exclude: string | RegExp | (string | RegExp)[] | undefined): boolean {
60-
if (typeof exclude === 'string')
61-
return name === exclude
62-
63-
if (exclude instanceof RegExp)
64-
return !!name.match(exclude)
65-
66-
if (Array.isArray(exclude)) {
67-
for (const item of exclude) {
68-
if (name === item || name.match(item))
69-
return true
70-
}
71-
}
72-
return false
73-
}

src/core/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getPackageInfo,
77
isPackageExists,
88
} from 'local-pkg'
9+
import type { FilterPattern } from '@rollup/pluginutils'
910
import type { ComponentInfo, ImportInfo, ImportInfoLegacy, Options, ResolvedOptions } from '../types'
1011
import type { Context } from './context'
1112
import { DISABLE_COMMENT } from './constants'
@@ -222,3 +223,22 @@ export function shouldTransform(code: string) {
222223
return false
223224
return true
224225
}
226+
227+
export function isExclude(name: string, exclude?: FilterPattern): boolean {
228+
if (!exclude)
229+
return false
230+
231+
if (typeof exclude === 'string')
232+
return name === exclude
233+
234+
if (exclude instanceof RegExp)
235+
return !!name.match(exclude)
236+
237+
if (Array.isArray(exclude)) {
238+
for (const item of exclude) {
239+
if (name === item || name.match(item))
240+
return true
241+
}
242+
}
243+
return false
244+
}

src/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface Options {
7272
exclude?: FilterPattern
7373

7474
/**
75-
* RegExp or glob to match component names that will NOT be imported
75+
* RegExp or string to match component names that will NOT be imported
7676
*/
7777
excludeNames?: FilterPattern
7878

@@ -185,7 +185,7 @@ export interface Options {
185185

186186
export type ResolvedOptions = Omit<
187187
Required<Options>,
188-
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration' | 'excludeNames'
188+
'resolvers' | 'extensions' | 'dirs' | 'globalComponentsDeclaration'
189189
> & {
190190
resolvers: ComponentResolverObject[]
191191
extensions: string[]
@@ -194,7 +194,6 @@ export type ResolvedOptions = Omit<
194194
globs: string[]
195195
dts: string | false
196196
root: string
197-
excludeNames: (id: unknown) => boolean
198197
}
199198

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

test/search.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ describe('search', () => {
6969

7070
expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
7171
})
72+
73+
it('should excludeNames', () => {
74+
const ctx = new Context({
75+
dirs: ['src/components'],
76+
excludeNames: ['Book'],
77+
})
78+
ctx.setRoot(root)
79+
ctx.searchGlob()
80+
81+
expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
82+
})
7283
})

0 commit comments

Comments
 (0)