index.ts 1.8 KB

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