Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tdesign): add exclude option #554

Merged
merged 1 commit into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})