index.ts 1.8 KB

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