index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import mainInitializeRouter from '#shared/router/index.ts'
  3. import type { InitializeAppRouter, RoutesModule } from '#shared/types/router.ts'
  4. import systemSetupInfo from './guards/before/systemSetupInfo.ts'
  5. import type { App } from 'vue'
  6. import type { RouteRecordRaw } from 'vue-router'
  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: 'LayoutPage',
  46. component: () => import('#desktop/components/layout/LayoutPage.vue'),
  47. children: childRoutes,
  48. },
  49. ]
  50. const initializeRouter: InitializeAppRouter = (app: App) => {
  51. return mainInitializeRouter(app, routes, [systemSetupInfo], [], 'desktop')
  52. }
  53. export default initializeRouter