index.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import type { App } from 'vue'
  3. import type { RouteRecordRaw } from 'vue-router'
  4. import mainInitializeRouter from '@shared/router'
  5. import type { InitializeAppRouter, RoutesModule } from '@shared/types/router'
  6. import LayoutMain from '@mobile/components/layout/LayoutMain.vue'
  7. import transitionViewGuard from './guards/before/viewTransition'
  8. import { errorAfterGuard } from './error'
  9. const routeModules: Record<string, RoutesModule> = import.meta.glob(
  10. ['../pages/*/routes.ts', '../pages/*/routes/*.ts'],
  11. { eager: true },
  12. )
  13. const mainRoutes: Array<RouteRecordRaw> = []
  14. const childRoutes: Array<RouteRecordRaw> = []
  15. const names = new Set<string | symbol>()
  16. const handleRoutes = (routes: Array<RouteRecordRaw>, isMainRoute = false) => {
  17. if (isMainRoute) {
  18. mainRoutes.push(...routes)
  19. } else {
  20. childRoutes.push(...routes)
  21. }
  22. if (import.meta.env.PROD) return
  23. // for debugging routes, vue-router doesn't do this automatically
  24. routes.forEach((route) => {
  25. if (!route.name) return
  26. if (names.has(route.name)) {
  27. console.error(
  28. `Duplicate route name: ${String(route.name)} for ${route.path}`,
  29. )
  30. } else {
  31. names.add(route.name)
  32. }
  33. })
  34. }
  35. Object.values(routeModules).forEach((module: RoutesModule) => {
  36. const defaultExport = module.default
  37. const { isMainRoute } = module
  38. handleRoutes(
  39. Array.isArray(defaultExport) ? defaultExport : [defaultExport],
  40. isMainRoute,
  41. )
  42. })
  43. export const routes: Array<RouteRecordRaw> = [
  44. ...mainRoutes,
  45. {
  46. path: '/',
  47. name: 'Main',
  48. props: true,
  49. component: LayoutMain,
  50. children: childRoutes,
  51. },
  52. ]
  53. const initializeRouter: InitializeAppRouter = (app: App) => {
  54. return mainInitializeRouter(
  55. app,
  56. routes,
  57. [transitionViewGuard],
  58. [errorAfterGuard],
  59. 'mobile',
  60. )
  61. }
  62. export default initializeRouter