transform.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* eslint-disable */
  2. /* prettier-ignore */
  3. // Generated by elegant-router
  4. // Read more: https://github.com/soybeanjs/elegant-router
  5. import type { RouteRecordRaw, RouteComponent } from 'vue-router';
  6. import type { ElegantConstRoute } from '@elegant-router/vue';
  7. import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types';
  8. /**
  9. * transform elegant const routes to vue routes
  10. * @param routes elegant const routes
  11. * @param layouts layout components
  12. * @param views view components
  13. */
  14. export function transformElegantRoutesToVueRoutes(
  15. routes: ElegantConstRoute[],
  16. layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  17. views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
  18. ) {
  19. return routes.flatMap(route => transformElegantRouteToVueRoute(route, layouts, views));
  20. }
  21. /**
  22. * transform elegant route to vue route
  23. * @param route elegant const route
  24. * @param layouts layout components
  25. * @param views view components
  26. */
  27. function transformElegantRouteToVueRoute(
  28. route: ElegantConstRoute,
  29. layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  30. views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
  31. ) {
  32. const LAYOUT_PREFIX = 'layout.';
  33. const VIEW_PREFIX = 'view.';
  34. const ROUTE_DEGREE_SPLITTER = '_';
  35. const FIRST_LEVEL_ROUTE_COMPONENT_SPLIT = '$';
  36. function isLayout(component: string) {
  37. return component.startsWith(LAYOUT_PREFIX);
  38. }
  39. function getLayoutName(component: string) {
  40. const layout = component.replace(LAYOUT_PREFIX, '');
  41. if(!layouts[layout]) {
  42. throw new Error(`Layout component "${layout}" not found`);
  43. }
  44. return layout;
  45. }
  46. function isView(component: string) {
  47. return component.startsWith(VIEW_PREFIX);
  48. }
  49. function getViewName(component: string) {
  50. const view = component.replace(VIEW_PREFIX, '');
  51. if(!views[view]) {
  52. throw new Error(`View component "${view}" not found`);
  53. }
  54. return view;
  55. }
  56. function isFirstLevelRoute(item: ElegantConstRoute) {
  57. return !item.name.includes(ROUTE_DEGREE_SPLITTER);
  58. }
  59. function isSingleLevelRoute(item: ElegantConstRoute) {
  60. return isFirstLevelRoute(item) && !item.children?.length;
  61. }
  62. function getSingleLevelRouteComponent(component: string) {
  63. const [layout, view] = component.split(FIRST_LEVEL_ROUTE_COMPONENT_SPLIT);
  64. return {
  65. layout: getLayoutName(layout),
  66. view: getViewName(view)
  67. };
  68. }
  69. const vueRoutes: RouteRecordRaw[] = [];
  70. // add props: true to route
  71. if (route.path.includes(':') && !route.props) {
  72. route.props = true;
  73. }
  74. const { name, path, component, children, ...rest } = route;
  75. const vueRoute = { name, path, ...rest } as RouteRecordRaw;
  76. try {
  77. if (component) {
  78. if (isSingleLevelRoute(route)) {
  79. const { layout, view } = getSingleLevelRouteComponent(component);
  80. const singleLevelRoute: RouteRecordRaw = {
  81. path,
  82. component: layouts[layout],
  83. meta: {
  84. title: route.meta?.title || ''
  85. },
  86. children: [
  87. {
  88. name,
  89. path: '',
  90. component: views[view],
  91. ...rest
  92. } as RouteRecordRaw
  93. ]
  94. };
  95. return [singleLevelRoute];
  96. }
  97. if (isLayout(component)) {
  98. const layoutName = getLayoutName(component);
  99. vueRoute.component = layouts[layoutName];
  100. }
  101. if (isView(component)) {
  102. const viewName = getViewName(component);
  103. vueRoute.component = views[viewName];
  104. }
  105. }
  106. } catch (error: any) {
  107. console.error(`Error transforming route "${route.name}": ${error.toString()}`);
  108. return [];
  109. }
  110. // add redirect to child
  111. if (children?.length && !vueRoute.redirect) {
  112. vueRoute.redirect = {
  113. name: children[0].name
  114. };
  115. }
  116. if (children?.length) {
  117. const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child, layouts, views));
  118. if(isFirstLevelRoute(route)) {
  119. vueRoute.children = childRoutes;
  120. } else {
  121. vueRoutes.push(...childRoutes);
  122. }
  123. }
  124. vueRoutes.unshift(vueRoute);
  125. return vueRoutes;
  126. }
  127. /**
  128. * map of route name and route path
  129. */
  130. const routeMap: RouteMap = {
  131. "root": "/",
  132. "not-found": "/:pathMatch(.*)*",
  133. "exception": "/exception",
  134. "exception_403": "/exception/403",
  135. "exception_404": "/exception/404",
  136. "exception_500": "/exception/500",
  137. "document": "/document",
  138. "document_project": "/document/project",
  139. "document_project-link": "/document/project-link",
  140. "document_video": "/document/video",
  141. "document_vue": "/document/vue",
  142. "document_vite": "/document/vite",
  143. "document_unocss": "/document/unocss",
  144. "document_naive": "/document/naive",
  145. "document_pro-naive": "/document/pro-naive",
  146. "document_antd": "/document/antd",
  147. "document_alova": "/document/alova",
  148. "403": "/403",
  149. "404": "/404",
  150. "500": "/500",
  151. "about": "/about",
  152. "config": "/config",
  153. "config_dict": "/config/dict",
  154. "config_fright-config": "/config/fright-config",
  155. "config_order-split": "/config/order-split",
  156. "delivery": "/delivery",
  157. "delivery_after-sales-order": "/delivery/after-sales-order",
  158. "delivery_normal-order": "/delivery/normal-order",
  159. "delivery_ponits-details": "/delivery/ponits-details",
  160. "delivery_split-order": "/delivery/split-order",
  161. "delivery_split-order-detail": "/delivery/split-order-detail",
  162. "finance": "/finance",
  163. "finance_commodity-freight": "/finance/commodity-freight",
  164. "finance_summary": "/finance/summary",
  165. "goods": "/goods",
  166. "goods_desk-category": "/goods/desk-category",
  167. "goods_store-goods": "/goods/store-goods",
  168. "goods_tag": "/goods/tag",
  169. "government": "/government",
  170. "government_government-list": "/government/government-list",
  171. "government_points": "/government/points",
  172. "government_user-list": "/government/user-list",
  173. "home": "/home",
  174. "iframe-page": "/iframe-page/:url",
  175. "login": "/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?",
  176. "manage": "/manage",
  177. "manage_config": "/manage/config",
  178. "manage_department": "/manage/department",
  179. "manage_log": "/manage/log",
  180. "manage_menu": "/manage/menu",
  181. "manage_role": "/manage/role",
  182. "manage_schedule": "/manage/schedule",
  183. "manage_user": "/manage/user",
  184. "operation": "/operation",
  185. "operation_advertisement": "/operation/advertisement",
  186. "operation_delivery-admin": "/operation/delivery-admin",
  187. "operation_search": "/operation/search",
  188. "plugin": "/plugin",
  189. "plugin_barcode": "/plugin/barcode",
  190. "plugin_charts": "/plugin/charts",
  191. "plugin_charts_antv": "/plugin/charts/antv",
  192. "plugin_charts_echarts": "/plugin/charts/echarts",
  193. "plugin_charts_vchart": "/plugin/charts/vchart",
  194. "plugin_copy": "/plugin/copy",
  195. "plugin_editor": "/plugin/editor",
  196. "plugin_editor_markdown": "/plugin/editor/markdown",
  197. "plugin_editor_quill": "/plugin/editor/quill",
  198. "plugin_excel": "/plugin/excel",
  199. "plugin_gantt": "/plugin/gantt",
  200. "plugin_gantt_dhtmlx": "/plugin/gantt/dhtmlx",
  201. "plugin_gantt_vtable": "/plugin/gantt/vtable",
  202. "plugin_icon": "/plugin/icon",
  203. "plugin_map": "/plugin/map",
  204. "plugin_pdf": "/plugin/pdf",
  205. "plugin_pinyin": "/plugin/pinyin",
  206. "plugin_print": "/plugin/print",
  207. "plugin_swiper": "/plugin/swiper",
  208. "plugin_tables": "/plugin/tables",
  209. "plugin_tables_vtable": "/plugin/tables/vtable",
  210. "plugin_typeit": "/plugin/typeit",
  211. "plugin_video": "/plugin/video",
  212. "pro-naive": "/pro-naive",
  213. "pro-naive_form": "/pro-naive/form",
  214. "pro-naive_form_basic": "/pro-naive/form/basic",
  215. "pro-naive_form_query": "/pro-naive/form/query",
  216. "pro-naive_form_step": "/pro-naive/form/step",
  217. "pro-naive_table": "/pro-naive/table",
  218. "pro-naive_table_remote": "/pro-naive/table/remote",
  219. "pro-naive_table_row-edit": "/pro-naive/table/row-edit",
  220. "store-management": "/store-management",
  221. "store-management_store-info": "/store-management/store-info",
  222. "user-center": "/user-center",
  223. "user-management": "/user-management",
  224. "user-management_user-list": "/user-management/user-list",
  225. "user-management_user-reviews": "/user-management/user-reviews"
  226. };
  227. /**
  228. * get route path by route name
  229. * @param name route name
  230. */
  231. export function getRoutePath<T extends RouteKey>(name: T) {
  232. return routeMap[name];
  233. }
  234. /**
  235. * get route name by route path
  236. * @param path route path
  237. */
  238. export function getRouteName(path: RoutePath) {
  239. const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];
  240. const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;
  241. return routeName;
  242. }