|
| 1 | +import { ComponentResolver } from "../types" |
| 2 | +import { kebabCase } from "../utils" |
| 3 | + |
| 4 | +export interface IduxResolverOptions { |
| 5 | + /** |
| 6 | + * import style along with components |
| 7 | + */ |
| 8 | + importStyle?: 'css' | 'less' |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Resolver for `@idux/cdk` and `@idux/components` |
| 13 | + * |
| 14 | + * @link https://idux.site |
| 15 | + */ |
| 16 | +export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver { |
| 17 | + return (name: string) => { |
| 18 | + if (name.match(/^Ix[A-Z]/)) { |
| 19 | + const { importStyle } = options |
| 20 | + const compName = name.slice(2) |
| 21 | + const kebabCaseName = kebabCase(compName) |
| 22 | + const isCdk = cdkNames.includes(kebabCaseName) |
| 23 | + const packageName = isCdk ? 'cdk' : 'components' |
| 24 | + const dirname = getDirname(kebabCaseName) |
| 25 | + const path = `@idux/${packageName}/${dirname}` |
| 26 | + const sideEffects = isCdk || !importStyle ? undefined : `${path}/style/${importStyle === 'css' ? 'css' : 'index'}` |
| 27 | + |
| 28 | + return { importName: name, path, sideEffects } |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const cdkNames = ['portal', 'resizable', 'virtual-list'] |
| 34 | +const kebabCaseDirnames = [ |
| 35 | + 'virtual-list', |
| 36 | + 'auto-complete', |
| 37 | + 'back-top', |
| 38 | + 'date-picker', |
| 39 | + 'input-number', |
| 40 | + 'time-picker', |
| 41 | + 'tree-select', |
| 42 | +] |
| 43 | + |
| 44 | +function getDirname(compName: string): string { |
| 45 | + const dirname = kebabCaseDirnames.find(name => compName.startsWith(name)) |
| 46 | + if (dirname) { |
| 47 | + return dirname |
| 48 | + } |
| 49 | + |
| 50 | + const [first] = compName.split('-') |
| 51 | + if (first === 'row' || first === 'col') { |
| 52 | + return 'grid' |
| 53 | + } |
| 54 | + return first |
| 55 | +} |
0 commit comments