|
| 1 | +import { ComponentResolver } from '../types' |
| 2 | +import { kebabCase } from '../utils' |
| 3 | + |
| 4 | +const getSideEffects: ( |
| 5 | + compName: string, |
| 6 | +) => string[] = (compName) => { |
| 7 | + const sideEffects = [ |
| 8 | + 'view-design/dist/styles/iview.css', |
| 9 | + 'popper.js/dist/umd/popper.js', |
| 10 | + ] |
| 11 | + |
| 12 | + if (/^Table/.test(compName)) |
| 13 | + sideEffects.push('element-resize-detector') |
| 14 | + |
| 15 | + if (/^Date/.test(compName)) |
| 16 | + sideEffects.push('js-calendar') |
| 17 | + |
| 18 | + return sideEffects |
| 19 | +} |
| 20 | + |
| 21 | +const matchComponents = [ |
| 22 | + { |
| 23 | + pattern: /^List/, |
| 24 | + compDir: 'list', |
| 25 | + }, |
| 26 | +] |
| 27 | + |
| 28 | +const getCompDir = (compName: string): string => { |
| 29 | + let compPath: string|undefined |
| 30 | + |
| 31 | + const total = matchComponents.length |
| 32 | + for (let i = 0; i < total; i++) { |
| 33 | + const matcher = matchComponents[i] |
| 34 | + if (compName.match(matcher.pattern)) { |
| 35 | + compPath = `${matcher.compDir}/${kebabCase(compName)}.vue` |
| 36 | + break |
| 37 | + } |
| 38 | + } |
| 39 | + if (!compPath) compPath = kebabCase(compName) |
| 40 | + return compPath |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Resolver for View UI |
| 45 | + * @requires @originjs/vite-plugin-commonjs |
| 46 | + * @author @nabaonan |
| 47 | + * @link https://www.iviewui.com/ |
| 48 | + * @description has known problems list below |
| 49 | + * - select component render error PR: https://github.com/view-design/ViewUI/pull/944, choose can't display value,because click option trigger twice,at second time,select value turn into undefined. |
| 50 | + * - scroll component has a template syntax called lang='html',it is require html-loader,but vite plugin not support yet,remove it can run. |
| 51 | + */ |
| 52 | +export const ViewUiResolver = (): ComponentResolver => (name: string) => { |
| 53 | + if (name.match(/^I[A-Z]/)) { |
| 54 | + const compName = name.slice(1) |
| 55 | + return { |
| 56 | + path: `view-design/src/components/${getCompDir(compName)}`, |
| 57 | + sideEffects: getSideEffects(compName), |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments