override.ts 1.1 KB

import { ConfigType } from "../index.d"
import { matchExternalPrefixReg } from "./useWidgetKey"

const getAllConfigFile = async () => {
  return import.meta.glob('../components/**/config.ts', )
}

const getChartConfigFile = async (path: string) => {
  const fileList = await getAllConfigFile()
  return fileList[path]() as any
} 

export const createComponent = async (configType: ConfigType) => {
  const { key, chartKey, category, package: packageName, redirectComponent } = configType
  // redirectComponent 是给图片组件库和图标组件库使用的
  if (redirectComponent) {
    const [packageName, categoryName, keyName] = redirectComponent.split('/')
    const filePath = `../components/${packageName}/${categoryName}/${keyName}/config.ts`
    const redirectChart = await getChartConfigFile(filePath)
    return new redirectChart.default()
  }

  const hasExternalPrefix = matchExternalPrefixReg.test(chartKey)
  const filePath = `../components${hasExternalPrefix ? '/external' : ''}/${packageName}/${category}/${key}/config.ts`
  const chart = await getChartConfigFile(filePath)
  return new chart.default()
}