|
| 1 | +import { ComponentResolver, SideEffectsInfo } from '../../types' |
| 2 | +import { kebabCase } from '../utils' |
| 3 | + |
| 4 | +export interface TDesignResolverOptions { |
| 5 | + /** |
| 6 | + * import style along with components |
| 7 | + * @default 'css' |
| 8 | + */ |
| 9 | + importStyle?: boolean | 'css' | 'less' |
| 10 | + |
| 11 | + /** |
| 12 | + * select the specified library |
| 13 | + * @default 'vue' |
| 14 | + */ |
| 15 | + library?: 'vue' | 'vue-next' | 'react' | 'mobile-vue' | 'mobile-react' |
| 16 | + |
| 17 | + /** |
| 18 | + * resolve `tdesign-icons' |
| 19 | + * @default false |
| 20 | + */ |
| 21 | + resolveIcons?: boolean |
| 22 | +} |
| 23 | + |
| 24 | +function getSideEffects(importName: string, options: TDesignResolverOptions): SideEffectsInfo | undefined { |
| 25 | + const { library = 'vue', importStyle = 'css' } = options |
| 26 | + const fileName = kebabCase(importName) |
| 27 | + |
| 28 | + if (!importStyle) return |
| 29 | + |
| 30 | + if (importStyle === 'less') return `tdesign-${library}/esm/${fileName}/style` |
| 31 | + |
| 32 | + return `tdesign-${library}/es/${fileName}/style` |
| 33 | +} |
| 34 | + |
| 35 | +export function TDesignResolver(options: TDesignResolverOptions = {}): ComponentResolver { |
| 36 | + return { |
| 37 | + type: 'component', |
| 38 | + resolve: (name: string) => { |
| 39 | + const { library = 'vue' } = options |
| 40 | + |
| 41 | + if (options.resolveIcons && name.match(/[a-z]Icon$/)) { |
| 42 | + return { |
| 43 | + importName: name, |
| 44 | + path: `tdesign-icons-${library}`, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (name.match(/^T[A-Z]/)) { |
| 49 | + const importName = name.slice(1) |
| 50 | + |
| 51 | + return { |
| 52 | + importName, |
| 53 | + path: `tdesign-${library}`, |
| 54 | + sideEffects: getSideEffects(importName, options), |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
0 commit comments