Skip to content

Commit

Permalink
feat(tdesign): add exclude option (#554)
Browse files Browse the repository at this point in the history
close #552
close #505
  • Loading branch information
Sight-wcg committed Dec 1, 2022
1 parent 383b11a commit 48c3374
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/core/resolvers/tdesign.ts
Expand Up @@ -25,6 +25,12 @@ export interface TDesignResolverOptions {
* @default false
*/
esm?: boolean

/**
* exclude component name, if match do not resolve the name
*
*/
exclude?: string | RegExp | (string | RegExp)[]
}

function getSideEffects(importName: string, options: TDesignResolverOptions): SideEffectsInfo | undefined {
Expand Down Expand Up @@ -91,9 +97,12 @@ export function TDesignResolver(options: TDesignResolverOptions = {}): Component
return {
type: 'component',
resolve: (name: string) => {
const { library = 'vue' } = options
const { library = 'vue', exclude } = options
const importFrom = options.esm ? '/esm' : ''

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

if (options.resolveIcons && name.match(/[a-z]Icon$/)) {
return {
name,
Expand All @@ -113,3 +122,19 @@ 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
}
22 changes: 22 additions & 0 deletions test/resolvers/tdesign.test.ts
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { TDesignResolver } from '../../src/resolvers'

import type { ComponentResolveResult, ComponentResolverObject } from '../../src'

describe('TDesignResolver', () => {
it('name matching string rule should not be resolved', async () => {
const resolver = TDesignResolver({ exclude: 'TString' }) as ComponentResolverObject
expect(resolver.resolve('TString')).toBeFalsy()
})

it('name matching RegExp rule should not be resolved', async () => {
const resolver = TDesignResolver({ exclude: /^TDoc[A-Z]/ }) as ComponentResolverObject
expect(resolver.resolve('TDocRegExp')).toBeFalsy()
})

it('name matching Array<string | RegExp> rule should not be resolved', async () => {
const resolver = TDesignResolver({ exclude: ['TString', /^TDoc[A-Z]/] }) as ComponentResolverObject
expect(resolver.resolve('TString')).toBeFalsy()
expect(resolver.resolve('TDocRegExp')).toBeFalsy()
})
})

0 comments on commit 48c3374

Please sign in to comment.