Skip to content

Commit

Permalink
use existing isExclude function
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD committed Aug 5, 2024
1 parent 87f38d0 commit 467c356
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { UpdatePayload, ViteDevServer } from 'vite'
import { slash, throttle, toArray } from '@antfu/utils'
import type { ComponentInfo, Options, ResolvedOptions, Transformer } from '../types'
import { DIRECTIVE_IMPORT_PREFIX } from './constants'
import { getNameFromFilePath, matchGlobs, normalizeComponentInfo, parseId, pascalCase, resolveAlias } from './utils'
import { getNameFromFilePath, isExclude, matchGlobs, normalizeComponentInfo, parseId, pascalCase, resolveAlias } from './utils'
import { resolveOptions } from './options'
import { searchComponents } from './fs/glob'
import { writeDeclaration } from './declaration'
Expand Down Expand Up @@ -203,7 +203,7 @@ export class Context {
.from(this._componentPaths)
.forEach((path) => {
const name = pascalCase(getNameFromFilePath(path, this.options))
if (this.options.excludeNames(name)) {
if (isExclude(name, this.options.excludeNames)) {
debug.components('exclude', name)
return
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { join, resolve } from 'node:path'
import { slash, toArray } from '@antfu/utils'
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
import { createFilter } from '@rollup/pluginutils'
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
import { detectTypeImports } from './type-imports/detect'

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

if (resolved.globs) {
resolved.globs = toArray(resolved.globs).map((glob: string) => slash(resolveGlobsExclude(root, glob)))
Expand Down
18 changes: 0 additions & 18 deletions src/core/resolvers/_utils.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/core/resolvers/arco.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Debug from 'debug'
import type { ComponentInfo, ComponentResolver } from '../../types'
import { kebabCase, pascalCase } from '../utils'
import { isExclude } from './_utils'
import { isExclude, kebabCase, pascalCase } from '../utils'

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

Expand Down
14 changes: 4 additions & 10 deletions src/core/resolvers/layui-vue.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { FilterPattern } from '@rollup/pluginutils'
import { isExclude } from '../utils'
import type { ComponentInfo, ComponentResolver, SideEffectsInfo } from '../../types'

const matchComponents = [
Expand Down Expand Up @@ -95,7 +97,7 @@ export interface LayuiVueResolverOptions {
* exclude components that do not require automatic import
*
*/
exclude?: Array<string | RegExp>
exclude?: FilterPattern
}

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

if (options.exclude && isExclude(importName, options.exclude))
if (isExclude(importName, options.exclude))
return undefined

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

function isExclude(name: string, exclude: Array<string | RegExp>): boolean {
for (const item of exclude) {
if (name === item || name.match(item))
return true
}
return false
}

/**
* Resolver for layui-vue
*
Expand Down
22 changes: 4 additions & 18 deletions src/core/resolvers/tdesign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { FilterPattern } from '@rollup/pluginutils'
import type { ComponentResolver } from '../../types'
import { isExclude } from '../utils'

export interface TDesignResolverOptions {
/**
Expand All @@ -23,7 +25,7 @@ export interface TDesignResolverOptions {
* exclude component name, if match do not resolve the name
*
*/
exclude?: string | RegExp | (string | RegExp)[]
exclude?: FilterPattern
}

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

if (options.exclude && isExclude(name, exclude))
if (isExclude(name, exclude))
return

if (options.resolveIcons && name.match(/[a-z]Icon$/)) {
Expand All @@ -55,19 +57,3 @@ export function TDesignResolver(options: TDesignResolverOptions = {}): Component
},
}
}

function isExclude(name: string, exclude: string | RegExp | (string | RegExp)[] | undefined): boolean {
if (typeof exclude === 'string')
return name === exclude

if (exclude instanceof RegExp)
return !!name.match(exclude)

if (Array.isArray(exclude)) {
for (const item of exclude) {
if (name === item || name.match(item))
return true
}
}
return false
}
20 changes: 20 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getPackageInfo,
isPackageExists,
} from 'local-pkg'
import type { FilterPattern } from '@rollup/pluginutils'
import type { ComponentInfo, ImportInfo, ImportInfoLegacy, Options, ResolvedOptions } from '../types'
import type { Context } from './context'
import { DISABLE_COMMENT } from './constants'
Expand Down Expand Up @@ -222,3 +223,22 @@ export function shouldTransform(code: string) {
return false
return true
}

export function isExclude(name: string, exclude?: FilterPattern): boolean {
if (!exclude)
return false

if (typeof exclude === 'string')
return name === exclude

if (exclude instanceof RegExp)
return !!name.match(exclude)

if (Array.isArray(exclude)) {
for (const item of exclude) {
if (name === item || name.match(item))
return true
}
}
return false
}
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface Options {
exclude?: FilterPattern

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

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

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

export type ComponentsImportMap = Record<string, string[] | undefined>
Expand Down
11 changes: 11 additions & 0 deletions test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ describe('search', () => {

expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
})

it('should excludeNames', () => {
const ctx = new Context({
dirs: ['src/components'],
excludeNames: ['Book'],
})
ctx.setRoot(root)
ctx.searchGlob()

expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
})
})

0 comments on commit 467c356

Please sign in to comment.