route.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function handleOriginRoute(route, idx, basePath) {
  2. const fullPath = basePath === null ? route.path : basePath + '/' + (route.path || 'index')
  3. const filePath = fullPath.replaceAll(/\/\:\w+/g, '')
  4. // basePath === null ? route.path : basePath + '/' + (route.path.replaceAll(/\/\:\w+/g, '') || 'index')
  5. const children = route?.children?.map((child, idy) => handleOriginRoute(child, idy, fullPath))
  6. const routeName = route.name ?? filePath.replaceAll('/', '-').toUpperCase()
  7. return ({
  8. path: route.path,
  9. name: routeName,
  10. props: /\/\:\w+/.test(basePath + '/' + route.path),
  11. meta: Object.assign({
  12. hidden: false,
  13. // ['tab', 'single'].includes(route?.plop?.type) || route?.children?.length === 1,
  14. breadcrumb: ['tab'].includes(route?.plop?.type),
  15. sort: idx,
  16. }, route.meta ?? null),
  17. plop: {
  18. ...route?.plop,
  19. filePath,
  20. fullPath,
  21. props: fullPath.match(/(?<=\/\:)\w+/g),
  22. routeName,
  23. children: children?.map(child => {
  24. const { name, meta: { title } } = child.redirect ? child.children.find(({ name }) => name === child.redirect.name) : child
  25. // ?.map(({ name, meta: { title } }) => ({ name, title }))
  26. return {
  27. name,
  28. title
  29. }
  30. }),
  31. template: route?.plop?.type ?? (route?.plop?.url ? 'index' : 'multi')
  32. },
  33. component: `___() => import('~/pages${filePath}.vue')___`,
  34. children,
  35. redirect: Array.isArray(children) ? ({ name: children[0].name }) : null
  36. })
  37. }
  38. export function origin2target(originRoutes) {
  39. const res = originRoutes.map((originRoute, idx) => handleOriginRoute(originRoute, idx, null))
  40. return res
  41. //[{ path: '/', redirect: res[0].path, meta: { hidden: true, sort: -1 } }].concat(res)
  42. }