@vlian/framework 1.2.18 → 1.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics.umd.js +1 -1
- package/dist/core/router/RouterManager.cjs +28 -9
- package/dist/core/router/RouterManager.cjs.map +1 -1
- package/dist/core/router/RouterManager.d.ts +2 -2
- package/dist/core/router/RouterManager.js +28 -9
- package/dist/core/router/RouterManager.js.map +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.cjs +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.cjs.map +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.js +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.js.map +1 -1
- package/dist/core/router/types.d.ts +9 -0
- package/dist/core/router/types.js.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.cjs +11 -6
- package/dist/core/router/utils/adapters/react-router/transform.cjs.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.d.ts +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.js +11 -6
- package/dist/core/router/utils/adapters/react-router/transform.js.map +1 -1
- package/dist/core/router/validation/RouterConfigValidator.d.ts +2 -0
- package/dist/core/router/validation/schema.cjs +2 -1
- package/dist/core/router/validation/schema.cjs.map +1 -1
- package/dist/core/router/validation/schema.d.ts +3 -0
- package/dist/core/router/validation/schema.js +2 -1
- package/dist/core/router/validation/schema.js.map +1 -1
- package/dist/index.umd.js +43 -18
- package/dist/index.umd.js.map +1 -1
- package/dist/request.umd.js +1 -1
- package/dist/state.umd.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../src/core/router/utils/adapters/react-router/transform.tsx"],"sourcesContent":["import type { RouteConfig } from '../../../types';\nimport {\n type RouteObject,\n Navigate\n} from 'react-router-dom';\nimport type { TransformRoutesResult } from '../../transform';\nimport { RouteErrorBoundary } from './RouteErrorBoundary';\nimport type { ComponentImport } from '../../../types';\nimport { logger } from '../../../../../utils';\n\nfunction DefaultRouteHydrateFallback() {\n return (\n <div style={{ padding: 16, textAlign: 'center', color: '#5f6c7b' }}>\n Loading...\n </div>\n );\n}\n\nexport const transformRoutesToReactRoutes = async (\n routes: RouteConfig[], \n transformResult: TransformRoutesResult,\n defaultRouteErrorComponent?: ComponentImport,\n defaultRouteLoadingComponent?: ComponentImport,\n): Promise<RouteObject[]> => {\n\n /**\n * 批量处理路由\n * @param routes 路由组\n */\n function transformRouteToReactRoutes(routes: RouteConfig[]) {\n return routes.flatMap((route: RouteConfig) => transformRouteToReactRoute(route))\n }\n\n /**\n * 处理单个路由\n * @param route 路由\n */\n function transformRouteToReactRoute(route: RouteConfig): RouteObject {\n const {\n isGroup = false,\n enableRedirection = false,\n name,\n path,\n handle,\n children,\n page,\n layout,\n error,\n errors,\n loading,\n } = route;\n const routeError = error ?? errors;\n\n // 获取错误组件\n async function getErrorComponent() {\n // 如果 error/errors 是函数,直接使用\n if (typeof routeError === 'function') {\n return routeError();\n }\n\n const errorsMap = transformResult.errors;\n\n // 判断 error/errors 是否为 string 且不是空字符串\n if (typeof routeError === 'string' && routeError !== '') {\n // 使用 routeError 作为 key 获取 transformResult.errors 的值\n const errorResolver = errorsMap.get(routeError);\n\n // 如果获取不到,返回 null\n if (!errorResolver) {\n return null;\n }\n\n // 如果获取到,返回对应的值(调用动态导入函数)\n if (typeof errorResolver === 'function') {\n return errorResolver();\n }\n\n return null;\n }\n\n // 如果 errors 为空,使用配置的默认错误组件或内置的 RouteErrorBoundary 组件\n if (defaultRouteErrorComponent) {\n // 如果配置了默认错误组件,使用配置的组件\n return defaultRouteErrorComponent();\n }\n\n // 如果没有配置默认错误组件,使用内置的 RouteErrorBoundary 组件\n // 直接导入而非懒加载,因为:\n // 1. RouteErrorBoundary 是框架核心组件,体积小\n // 2. 错误边界需要快速响应,直接导入速度更快\n // 3. 作为基础设施组件,应该保证可用性\n return { default: RouteErrorBoundary };\n }\n\n // 获取转换配置\n function convertConfig(m: any) {\n if (!m) {\n return null;\n }\n const { action, loader, shouldRevalidate, default: Component } = m;\n\n // 如果 Component 不存在,记录警告\n if (!Component) {\n logger.warn(`路由组件未找到 default 导出: ${name}`, m);\n }\n\n return {\n action, // always use action\n loader, // always use loader\n shouldRevalidate,\n Component\n };\n }\n\n // 获取加载组件\n async function getLoadingComponent() {\n if (typeof loading === 'function') {\n return loading();\n }\n\n if (typeof loading === 'string' && loading !== '') {\n const loadingResolver = transformResult.loadings.get(loading);\n if (typeof loadingResolver === 'function') {\n return loadingResolver();\n }\n }\n\n if (defaultRouteLoadingComponent) {\n return defaultRouteLoadingComponent();\n }\n\n return null;\n }\n\n // 获取配置\n async function getConfig(index: boolean = false) {\n // 如果有layout和不是index,返回布局配置\n if (layout && !index) {\n // 如果 layout 是函数,直接使用\n if (typeof layout === 'function') {\n const config = await layout();\n return convertConfig(config);\n }\n\n // 如果 layout 是字符串,从 Map 中获取\n if (typeof layout === 'string') {\n const layoutResolver = transformResult.layouts.get(layout);\n if (!layoutResolver) {\n throw new Error(`未找到名为 ${layout} 的布局组件`);\n }\n const config = await layoutResolver();\n return convertConfig(config);\n }\n }\n\n let pageName = name;\n\n // 如果是notFound则转成404\n if (pageName === 'notFound') {\n pageName = '404'\n }\n\n if (page && (!children?.length || index)) {\n // 如果 page 是函数,直接使用\n if (typeof page === 'function') {\n const config = await page();\n return convertConfig(config);\n }\n\n // 如果 page 是字符串,从 Map 中获取\n if (typeof page === 'string') {\n const viewResolver = transformResult.pages.get(page);\n if (!viewResolver) {\n throw new Error(`未找到名为 ${page} 的页面组件`);\n }\n const config = await viewResolver();\n return convertConfig(config);\n }\n }\n\n return null;\n }\n\n // 获取处理信息,即额外的配置信息\n function getHandle(index: boolean = false) {\n if ((layout || isGroup) && !index) {\n return null\n }\n return {\n ...handle,\n name,\n path\n }\n }\n\n\n const reactRoute: RouteObject = {\n children: [],\n hydrateFallbackElement: <DefaultRouteHydrateFallback />,\n id: name,\n handle: getHandle(),\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const config = await getConfig();\n const LoadingComponent = await getLoadingComponent();\n\n // 如果配置为空,确保至少返回一个空对象,避免展开 undefined\n if (!config) {\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n };\n }\n\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...config\n };\n },\n path\n };\n\n // 处理子路由\n if (children?.length) {\n reactRoute.children = children.flatMap(child => transformRouteToReactRoute(child)).sort((a, b) => {\n const orderA = a.handle?.order ?? 99;\n const orderB = b.handle?.order ?? 99;\n return orderA - orderB;\n });\n\n if (page && !isGroup) {\n reactRoute.children.unshift({\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...(await getConfig(true))\n };\n }\n });\n }\n\n if (enableRedirection && isGroup) {\n const [firstChild] = reactRoute.children;\n if (firstChild?.path) {\n reactRoute.children.unshift({\n index: true,\n handle: getHandle(true),\n element: <Navigate to={firstChild.path as string} replace />\n });\n }\n }\n\n }\n // 若存在 layout 且没有子路由,但同时声明了 page,则自动生成 index 子路由以渲染页面\n // 场景:/test 使用自定义 layout,且没有 children 时仍应渲染 page\n if ((!children || children.length === 0) && layout && page && !isGroup) {\n reactRoute.children = [{\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...(await getConfig(true))\n };\n }\n }];\n }\n\n return reactRoute;\n }\n\n return transformRouteToReactRoutes(routes);\n}\n"],"names":["transformRoutesToReactRoutes","DefaultRouteHydrateFallback","div","style","padding","textAlign","color","routes","transformResult","defaultRouteErrorComponent","defaultRouteLoadingComponent","transformRouteToReactRoutes","flatMap","route","transformRouteToReactRoute","isGroup","enableRedirection","name","path","handle","children","page","layout","error","errors","loading","routeError","getErrorComponent","errorsMap","errorResolver","get","default","RouteErrorBoundary","convertConfig","m","action","loader","shouldRevalidate","Component","logger","warn","getLoadingComponent","loadingResolver","loadings","getConfig","index","config","layoutResolver","layouts","Error","pageName","length","viewResolver","pages","getHandle","reactRoute","hydrateFallbackElement","id","lazy","ErrorBoundary","LoadingComponent","HydrateFallback","child","sort","a","b","orderA","order","orderB","unshift","firstChild","element","Navigate","to","replace"],"mappings":";;;;+BAkBaA;;;eAAAA;;;;gCAdN;oCAE4B;uBAEZ;AAEvB,SAASC;IACL,qBACI,qBAACC;QAAIC,OAAO;YAAEC,SAAS;YAAIC,WAAW;YAAUC,OAAO;QAAU;kBAAG;;AAI5E;AAEO,MAAMN,+BAA+B,OACxCO,QACAC,iBACAC,4BACAC;IAGA;;;KAGC,GACD,SAASC,4BAA4BJ,MAAqB;QACtD,OAAOA,OAAOK,OAAO,CAAC,CAACC,QAAuBC,2BAA2BD;IAC7E;IAEA;;;KAGC,GACD,SAASC,2BAA2BD,KAAkB;QAClD,MAAM,EACFE,UAAU,KAAK,EACfC,oBAAoB,KAAK,EACzBC,IAAI,EACJC,IAAI,EACJC,MAAM,EACNC,QAAQ,EACRC,IAAI,EACJC,MAAM,EACNC,KAAK,EACLC,MAAM,EACNC,OAAO,EACV,GAAGZ;QACJ,MAAMa,aAAaH,SAASC;QAE5B,SAAS;QACT,eAAeG;YACX,2BAA2B;YAC3B,IAAI,OAAOD,eAAe,YAAY;gBAClC,OAAOA;YACX;YAEA,MAAME,YAAYpB,gBAAgBgB,MAAM;YAExC,qCAAqC;YACrC,IAAI,OAAOE,eAAe,YAAYA,eAAe,IAAI;gBACrD,oDAAoD;gBACpD,MAAMG,gBAAgBD,UAAUE,GAAG,CAACJ;gBAEpC,iBAAiB;gBACjB,IAAI,CAACG,eAAe;oBAChB,OAAO;gBACX;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C,gBAAgB,MAAMhC;wBAC5B,MAAMiC,mBAAmB,MAAMnB;wBAC/B,OAAO;4BACHkB,eAAeA,eAAe5B;4BAC9B8B,iBAAiBD,kBAAkB7B;4BACnC,GAAI,MAAMa,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;aAAE;QACN;QAEA,OAAOW;IACX;IAEA,OAAO5C,4BAA4BJ;AACvC"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../src/core/router/utils/adapters/react-router/transform.tsx"],"sourcesContent":["import type { RouteConfig } from '../../../types';\nimport {\n type RouteObject,\n Navigate\n} from 'react-router-dom';\nimport type { TransformRoutesResult } from '../../transform';\nimport { RouteErrorBoundary } from './RouteErrorBoundary';\nimport type { ComponentImport } from '../../../types';\nimport { logger } from '../../../../../utils';\n\nfunction DefaultRouteHydrateFallback() {\n return (\n <div style={{ padding: 16, textAlign: 'center', color: '#5f6c7b' }}>\n Loading...\n </div>\n );\n}\n\nexport const transformRoutesToReactRoutes = async (\n routes: RouteConfig[], \n transformResult: TransformRoutesResult,\n defaultRouteErrorComponent?: ComponentImport,\n defaultRouteLoadingComponent?: ComponentImport,\n enableHydrateFallback: boolean = false,\n): Promise<RouteObject[]> => {\n\n /**\n * 批量处理路由\n * @param routes 路由组\n */\n function transformRouteToReactRoutes(routes: RouteConfig[]) {\n return routes.flatMap((route: RouteConfig) => transformRouteToReactRoute(route))\n }\n\n /**\n * 处理单个路由\n * @param route 路由\n */\n function transformRouteToReactRoute(route: RouteConfig): RouteObject {\n const {\n isGroup = false,\n enableRedirection = false,\n name,\n path,\n handle,\n children,\n page,\n layout,\n error,\n errors,\n loading,\n } = route;\n const routeError = error ?? errors;\n\n // 获取错误组件\n async function getErrorComponent() {\n // 如果 error/errors 是函数,直接使用\n if (typeof routeError === 'function') {\n return routeError();\n }\n\n const errorsMap = transformResult.errors;\n\n // 判断 error/errors 是否为 string 且不是空字符串\n if (typeof routeError === 'string' && routeError !== '') {\n // 使用 routeError 作为 key 获取 transformResult.errors 的值\n const errorResolver = errorsMap.get(routeError);\n\n // 如果获取不到,返回 null\n if (!errorResolver) {\n return null;\n }\n\n // 如果获取到,返回对应的值(调用动态导入函数)\n if (typeof errorResolver === 'function') {\n return errorResolver();\n }\n\n return null;\n }\n\n // 如果 errors 为空,使用配置的默认错误组件或内置的 RouteErrorBoundary 组件\n if (defaultRouteErrorComponent) {\n // 如果配置了默认错误组件,使用配置的组件\n return defaultRouteErrorComponent();\n }\n\n // 如果没有配置默认错误组件,使用内置的 RouteErrorBoundary 组件\n // 直接导入而非懒加载,因为:\n // 1. RouteErrorBoundary 是框架核心组件,体积小\n // 2. 错误边界需要快速响应,直接导入速度更快\n // 3. 作为基础设施组件,应该保证可用性\n return { default: RouteErrorBoundary };\n }\n\n // 获取转换配置\n function convertConfig(m: any) {\n if (!m) {\n return null;\n }\n const { action, loader, shouldRevalidate, default: Component } = m;\n\n // 如果 Component 不存在,记录警告\n if (!Component) {\n logger.warn(`路由组件未找到 default 导出: ${name}`, m);\n }\n\n return {\n action, // always use action\n loader, // always use loader\n shouldRevalidate,\n Component\n };\n }\n\n // 获取加载组件\n async function getLoadingComponent() {\n if (typeof loading === 'function') {\n return loading();\n }\n\n if (typeof loading === 'string' && loading !== '') {\n const loadingResolver = transformResult.loadings.get(loading);\n if (typeof loadingResolver === 'function') {\n return loadingResolver();\n }\n }\n\n if (defaultRouteLoadingComponent) {\n return defaultRouteLoadingComponent();\n }\n\n return null;\n }\n\n // 获取配置\n async function getConfig(index: boolean = false) {\n // 如果有layout和不是index,返回布局配置\n if (layout && !index) {\n // 如果 layout 是函数,直接使用\n if (typeof layout === 'function') {\n const config = await layout();\n return convertConfig(config);\n }\n\n // 如果 layout 是字符串,从 Map 中获取\n if (typeof layout === 'string') {\n const layoutResolver = transformResult.layouts.get(layout);\n if (!layoutResolver) {\n throw new Error(`未找到名为 ${layout} 的布局组件`);\n }\n const config = await layoutResolver();\n return convertConfig(config);\n }\n }\n\n let pageName = name;\n\n // 如果是notFound则转成404\n if (pageName === 'notFound') {\n pageName = '404'\n }\n\n if (page && (!children?.length || index)) {\n // 如果 page 是函数,直接使用\n if (typeof page === 'function') {\n const config = await page();\n return convertConfig(config);\n }\n\n // 如果 page 是字符串,从 Map 中获取\n if (typeof page === 'string') {\n const viewResolver = transformResult.pages.get(page);\n if (!viewResolver) {\n throw new Error(`未找到名为 ${page} 的页面组件`);\n }\n const config = await viewResolver();\n return convertConfig(config);\n }\n }\n\n return null;\n }\n\n // 获取处理信息,即额外的配置信息\n function getHandle(index: boolean = false) {\n if ((layout || isGroup) && !index) {\n return null\n }\n return {\n ...handle,\n name,\n path\n }\n }\n\n\n const reactRoute: RouteObject = {\n children: [],\n id: name,\n handle: getHandle(),\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const config = await getConfig();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n\n // 如果配置为空,确保至少返回一个空对象,避免展开 undefined\n if (!config) {\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n };\n }\n\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...config\n };\n },\n path\n };\n\n if (enableHydrateFallback) {\n reactRoute.hydrateFallbackElement = <DefaultRouteHydrateFallback />;\n }\n\n // 处理子路由\n if (children?.length) {\n reactRoute.children = children.flatMap(child => transformRouteToReactRoute(child)).sort((a, b) => {\n const orderA = a.handle?.order ?? 99;\n const orderB = b.handle?.order ?? 99;\n return orderA - orderB;\n });\n\n if (page && !isGroup) {\n reactRoute.children.unshift({\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...(await getConfig(true))\n };\n }\n });\n }\n\n if (enableRedirection && isGroup) {\n const [firstChild] = reactRoute.children;\n if (firstChild?.path) {\n reactRoute.children.unshift({\n index: true,\n handle: getHandle(true),\n element: <Navigate to={firstChild.path as string} replace />\n });\n }\n }\n\n }\n // 若存在 layout 且没有子路由,但同时声明了 page,则自动生成 index 子路由以渲染页面\n // 场景:/test 使用自定义 layout,且没有 children 时仍应渲染 page\n if ((!children || children.length === 0) && layout && page && !isGroup) {\n reactRoute.children = [{\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...(await getConfig(true))\n };\n }\n }];\n }\n\n return reactRoute;\n }\n\n return transformRouteToReactRoutes(routes);\n}\n"],"names":["transformRoutesToReactRoutes","DefaultRouteHydrateFallback","div","style","padding","textAlign","color","routes","transformResult","defaultRouteErrorComponent","defaultRouteLoadingComponent","enableHydrateFallback","transformRouteToReactRoutes","flatMap","route","transformRouteToReactRoute","isGroup","enableRedirection","name","path","handle","children","page","layout","error","errors","loading","routeError","getErrorComponent","errorsMap","errorResolver","get","default","RouteErrorBoundary","convertConfig","m","action","loader","shouldRevalidate","Component","logger","warn","getLoadingComponent","loadingResolver","loadings","getConfig","index","config","layoutResolver","layouts","Error","pageName","length","viewResolver","pages","getHandle","reactRoute","id","lazy","ErrorBoundary","LoadingComponent","HydrateFallback","hydrateFallbackElement","child","sort","a","b","orderA","order","orderB","unshift","firstChild","element","Navigate","to","replace"],"mappings":";;;;+BAkBaA;;;eAAAA;;;;gCAdN;oCAE4B;uBAEZ;AAEvB,SAASC;IACL,qBACI,qBAACC;QAAIC,OAAO;YAAEC,SAAS;YAAIC,WAAW;YAAUC,OAAO;QAAU;kBAAG;;AAI5E;AAEO,MAAMN,+BAA+B,OACxCO,QACAC,iBACAC,4BACAC,8BACAC,wBAAiC,KAAK;IAGtC;;;KAGC,GACD,SAASC,4BAA4BL,MAAqB;Q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;wBAC/B,MAAMmB,kBAAkBD,kBAAkB5B,WAAW/B;wBACrD,OAAO;4BACH0D,eAAeA,eAAe3B;4BAC9B6B;4BACA,GAAI,MAAMhB,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;YACJ;YAEA,IAAI5B,qBAAqBD,SAAS;gBAC9B,MAAM,CAACuD,WAAW,GAAGf,WAAWnC,QAAQ;gBACxC,IAAIkD,YAAYpD,MAAM;oBAClBqC,WAAWnC,QAAQ,CAACiD,OAAO,CAAC;wBACxBxB,OAAO;wBACP1B,QAAQmC,UAAU;wBAClBiB,uBAAS,qBAACC,wBAAQ;4BAACC,IAAIH,WAAWpD,IAAI;4BAAYwD,OAAO;;oBAC7D;gBACJ;YACJ;QAEJ;QACA,qDAAqD;QACrD,gDAAgD;QAChD,IAAI,AAAC,CAAA,CAACtD,YAAYA,SAAS+B,MAAM,KAAK,CAAA,KAAM7B,UAAUD,QAAQ,CAACN,SAAS;YACpEwC,WAAWnC,QAAQ,GAAG;gBAAC;oBACnBD,QAAQmC,UAAU;oBAClBT,OAAO;oBACPY,MAAM;wBACF,MAAMC,gBAAgB,MAAM/B;wBAC5B,MAAMgC,mBAAmB,MAAMlB;wBAC/B,MAAMmB,kBAAkBD,kBAAkB5B,WAAW/B;wBACrD,OAAO;4BACH0D,eAAeA,eAAe3B;4BAC9B6B;4BACA,GAAI,MAAMhB,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;aAAE;QACN;QAEA,OAAOW;IACX;IAEA,OAAO5C,4BAA4BL;AACvC"}
|
|
@@ -2,4 +2,4 @@ import type { RouteConfig } from '../../../types';
|
|
|
2
2
|
import { type RouteObject } from 'react-router-dom';
|
|
3
3
|
import type { TransformRoutesResult } from '../../transform';
|
|
4
4
|
import type { ComponentImport } from '../../../types';
|
|
5
|
-
export declare const transformRoutesToReactRoutes: (routes: RouteConfig[], transformResult: TransformRoutesResult, defaultRouteErrorComponent?: ComponentImport, defaultRouteLoadingComponent?: ComponentImport) => Promise<RouteObject[]>;
|
|
5
|
+
export declare const transformRoutesToReactRoutes: (routes: RouteConfig[], transformResult: TransformRoutesResult, defaultRouteErrorComponent?: ComponentImport, defaultRouteLoadingComponent?: ComponentImport, enableHydrateFallback?: boolean) => Promise<RouteObject[]>;
|
|
@@ -12,7 +12,7 @@ function DefaultRouteHydrateFallback() {
|
|
|
12
12
|
children: "Loading..."
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
-
export const transformRoutesToReactRoutes = async (routes, transformResult, defaultRouteErrorComponent, defaultRouteLoadingComponent)=>{
|
|
15
|
+
export const transformRoutesToReactRoutes = async (routes, transformResult, defaultRouteErrorComponent, defaultRouteLoadingComponent, enableHydrateFallback = false)=>{
|
|
16
16
|
/**
|
|
17
17
|
* 批量处理路由
|
|
18
18
|
* @param routes 路由组
|
|
@@ -148,28 +148,31 @@ export const transformRoutesToReactRoutes = async (routes, transformResult, defa
|
|
|
148
148
|
}
|
|
149
149
|
const reactRoute = {
|
|
150
150
|
children: [],
|
|
151
|
-
hydrateFallbackElement: /*#__PURE__*/ _jsx(DefaultRouteHydrateFallback, {}),
|
|
152
151
|
id: name,
|
|
153
152
|
handle: getHandle(),
|
|
154
153
|
lazy: async ()=>{
|
|
155
154
|
const ErrorBoundary = await getErrorComponent();
|
|
156
155
|
const config = await getConfig();
|
|
157
156
|
const LoadingComponent = await getLoadingComponent();
|
|
157
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
158
158
|
// 如果配置为空,确保至少返回一个空对象,避免展开 undefined
|
|
159
159
|
if (!config) {
|
|
160
160
|
return {
|
|
161
161
|
ErrorBoundary: ErrorBoundary?.default,
|
|
162
|
-
HydrateFallback
|
|
162
|
+
HydrateFallback
|
|
163
163
|
};
|
|
164
164
|
}
|
|
165
165
|
return {
|
|
166
166
|
ErrorBoundary: ErrorBoundary?.default,
|
|
167
|
-
HydrateFallback
|
|
167
|
+
HydrateFallback,
|
|
168
168
|
...config
|
|
169
169
|
};
|
|
170
170
|
},
|
|
171
171
|
path
|
|
172
172
|
};
|
|
173
|
+
if (enableHydrateFallback) {
|
|
174
|
+
reactRoute.hydrateFallbackElement = /*#__PURE__*/ _jsx(DefaultRouteHydrateFallback, {});
|
|
175
|
+
}
|
|
173
176
|
// 处理子路由
|
|
174
177
|
if (children?.length) {
|
|
175
178
|
reactRoute.children = children.flatMap((child)=>transformRouteToReactRoute(child)).sort((a, b)=>{
|
|
@@ -184,9 +187,10 @@ export const transformRoutesToReactRoutes = async (routes, transformResult, defa
|
|
|
184
187
|
lazy: async ()=>{
|
|
185
188
|
const ErrorBoundary = await getErrorComponent();
|
|
186
189
|
const LoadingComponent = await getLoadingComponent();
|
|
190
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
187
191
|
return {
|
|
188
192
|
ErrorBoundary: ErrorBoundary?.default,
|
|
189
|
-
HydrateFallback
|
|
193
|
+
HydrateFallback,
|
|
190
194
|
...await getConfig(true)
|
|
191
195
|
};
|
|
192
196
|
}
|
|
@@ -216,9 +220,10 @@ export const transformRoutesToReactRoutes = async (routes, transformResult, defa
|
|
|
216
220
|
lazy: async ()=>{
|
|
217
221
|
const ErrorBoundary = await getErrorComponent();
|
|
218
222
|
const LoadingComponent = await getLoadingComponent();
|
|
223
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
219
224
|
return {
|
|
220
225
|
ErrorBoundary: ErrorBoundary?.default,
|
|
221
|
-
HydrateFallback
|
|
226
|
+
HydrateFallback,
|
|
222
227
|
...await getConfig(true)
|
|
223
228
|
};
|
|
224
229
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../src/core/router/utils/adapters/react-router/transform.tsx"],"sourcesContent":["import type { RouteConfig } from '../../../types';\nimport {\n type RouteObject,\n Navigate\n} from 'react-router-dom';\nimport type { TransformRoutesResult } from '../../transform';\nimport { RouteErrorBoundary } from './RouteErrorBoundary';\nimport type { ComponentImport } from '../../../types';\nimport { logger } from '../../../../../utils';\n\nfunction DefaultRouteHydrateFallback() {\n return (\n <div style={{ padding: 16, textAlign: 'center', color: '#5f6c7b' }}>\n Loading...\n </div>\n );\n}\n\nexport const transformRoutesToReactRoutes = async (\n routes: RouteConfig[], \n transformResult: TransformRoutesResult,\n defaultRouteErrorComponent?: ComponentImport,\n defaultRouteLoadingComponent?: ComponentImport,\n): Promise<RouteObject[]> => {\n\n /**\n * 批量处理路由\n * @param routes 路由组\n */\n function transformRouteToReactRoutes(routes: RouteConfig[]) {\n return routes.flatMap((route: RouteConfig) => transformRouteToReactRoute(route))\n }\n\n /**\n * 处理单个路由\n * @param route 路由\n */\n function transformRouteToReactRoute(route: RouteConfig): RouteObject {\n const {\n isGroup = false,\n enableRedirection = false,\n name,\n path,\n handle,\n children,\n page,\n layout,\n error,\n errors,\n loading,\n } = route;\n const routeError = error ?? errors;\n\n // 获取错误组件\n async function getErrorComponent() {\n // 如果 error/errors 是函数,直接使用\n if (typeof routeError === 'function') {\n return routeError();\n }\n\n const errorsMap = transformResult.errors;\n\n // 判断 error/errors 是否为 string 且不是空字符串\n if (typeof routeError === 'string' && routeError !== '') {\n // 使用 routeError 作为 key 获取 transformResult.errors 的值\n const errorResolver = errorsMap.get(routeError);\n\n // 如果获取不到,返回 null\n if (!errorResolver) {\n return null;\n }\n\n // 如果获取到,返回对应的值(调用动态导入函数)\n if (typeof errorResolver === 'function') {\n return errorResolver();\n }\n\n return null;\n }\n\n // 如果 errors 为空,使用配置的默认错误组件或内置的 RouteErrorBoundary 组件\n if (defaultRouteErrorComponent) {\n // 如果配置了默认错误组件,使用配置的组件\n return defaultRouteErrorComponent();\n }\n\n // 如果没有配置默认错误组件,使用内置的 RouteErrorBoundary 组件\n // 直接导入而非懒加载,因为:\n // 1. RouteErrorBoundary 是框架核心组件,体积小\n // 2. 错误边界需要快速响应,直接导入速度更快\n // 3. 作为基础设施组件,应该保证可用性\n return { default: RouteErrorBoundary };\n }\n\n // 获取转换配置\n function convertConfig(m: any) {\n if (!m) {\n return null;\n }\n const { action, loader, shouldRevalidate, default: Component } = m;\n\n // 如果 Component 不存在,记录警告\n if (!Component) {\n logger.warn(`路由组件未找到 default 导出: ${name}`, m);\n }\n\n return {\n action, // always use action\n loader, // always use loader\n shouldRevalidate,\n Component\n };\n }\n\n // 获取加载组件\n async function getLoadingComponent() {\n if (typeof loading === 'function') {\n return loading();\n }\n\n if (typeof loading === 'string' && loading !== '') {\n const loadingResolver = transformResult.loadings.get(loading);\n if (typeof loadingResolver === 'function') {\n return loadingResolver();\n }\n }\n\n if (defaultRouteLoadingComponent) {\n return defaultRouteLoadingComponent();\n }\n\n return null;\n }\n\n // 获取配置\n async function getConfig(index: boolean = false) {\n // 如果有layout和不是index,返回布局配置\n if (layout && !index) {\n // 如果 layout 是函数,直接使用\n if (typeof layout === 'function') {\n const config = await layout();\n return convertConfig(config);\n }\n\n // 如果 layout 是字符串,从 Map 中获取\n if (typeof layout === 'string') {\n const layoutResolver = transformResult.layouts.get(layout);\n if (!layoutResolver) {\n throw new Error(`未找到名为 ${layout} 的布局组件`);\n }\n const config = await layoutResolver();\n return convertConfig(config);\n }\n }\n\n let pageName = name;\n\n // 如果是notFound则转成404\n if (pageName === 'notFound') {\n pageName = '404'\n }\n\n if (page && (!children?.length || index)) {\n // 如果 page 是函数,直接使用\n if (typeof page === 'function') {\n const config = await page();\n return convertConfig(config);\n }\n\n // 如果 page 是字符串,从 Map 中获取\n if (typeof page === 'string') {\n const viewResolver = transformResult.pages.get(page);\n if (!viewResolver) {\n throw new Error(`未找到名为 ${page} 的页面组件`);\n }\n const config = await viewResolver();\n return convertConfig(config);\n }\n }\n\n return null;\n }\n\n // 获取处理信息,即额外的配置信息\n function getHandle(index: boolean = false) {\n if ((layout || isGroup) && !index) {\n return null\n }\n return {\n ...handle,\n name,\n path\n }\n }\n\n\n const reactRoute: RouteObject = {\n children: [],\n hydrateFallbackElement: <DefaultRouteHydrateFallback />,\n id: name,\n handle: getHandle(),\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const config = await getConfig();\n const LoadingComponent = await getLoadingComponent();\n\n // 如果配置为空,确保至少返回一个空对象,避免展开 undefined\n if (!config) {\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n };\n }\n\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...config\n };\n },\n path\n };\n\n // 处理子路由\n if (children?.length) {\n reactRoute.children = children.flatMap(child => transformRouteToReactRoute(child)).sort((a, b) => {\n const orderA = a.handle?.order ?? 99;\n const orderB = b.handle?.order ?? 99;\n return orderA - orderB;\n });\n\n if (page && !isGroup) {\n reactRoute.children.unshift({\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...(await getConfig(true))\n };\n }\n });\n }\n\n if (enableRedirection && isGroup) {\n const [firstChild] = reactRoute.children;\n if (firstChild?.path) {\n reactRoute.children.unshift({\n index: true,\n handle: getHandle(true),\n element: <Navigate to={firstChild.path as string} replace />\n });\n }\n }\n\n }\n // 若存在 layout 且没有子路由,但同时声明了 page,则自动生成 index 子路由以渲染页面\n // 场景:/test 使用自定义 layout,且没有 children 时仍应渲染 page\n if ((!children || children.length === 0) && layout && page && !isGroup) {\n reactRoute.children = [{\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback: LoadingComponent?.default,\n ...(await getConfig(true))\n };\n }\n }];\n }\n\n return reactRoute;\n }\n\n return transformRouteToReactRoutes(routes);\n}\n"],"names":["Navigate","RouteErrorBoundary","logger","DefaultRouteHydrateFallback","div","style","padding","textAlign","color","transformRoutesToReactRoutes","routes","transformResult","defaultRouteErrorComponent","defaultRouteLoadingComponent","transformRouteToReactRoutes","flatMap","route","transformRouteToReactRoute","isGroup","enableRedirection","name","path","handle","children","page","layout","error","errors","loading","routeError","getErrorComponent","errorsMap","errorResolver","get","default","convertConfig","m","action","loader","shouldRevalidate","Component","warn","getLoadingComponent","loadingResolver","loadings","getConfig","index","config","layoutResolver","layouts","Error","pageName","length","viewResolver","pages","getHandle","reactRoute","hydrateFallbackElement","id","lazy","ErrorBoundary","LoadingComponent","HydrateFallback","child","sort","a","b","orderA","order","orderB","unshift","firstChild","element","to","replace"],"mappings":";AACA,SAEIA,QAAQ,QACL,mBAAmB;AAE1B,SAASC,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,MAAM,QAAQ,uBAAuB;AAE9C,SAASC;IACL,qBACI,KAACC;QAAIC,OAAO;YAAEC,SAAS;YAAIC,WAAW;YAAUC,OAAO;QAAU;kBAAG;;AAI5E;AAEA,OAAO,MAAMC,+BAA+B,OACxCC,QACAC,iBACAC,4BACAC;IAGA;;;KAGC,GACD,SAASC,4BAA4BJ,MAAqB;QACtD,OAAOA,OAAOK,OAAO,CAAC,CAACC,QAAuBC,2BAA2BD;IAC7E;IAEA;;;KAGC,GACD,SAASC,2BAA2BD,KAAkB;QAClD,MAAM,EACFE,UAAU,KAAK,EACfC,oBAAoB,KAAK,EACzBC,IAAI,EACJC,IAAI,EACJC,MAAM,EACNC,QAAQ,EACRC,IAAI,EACJC,MAAM,EACNC,KAAK,EACLC,MAAM,EACNC,OAAO,EACV,GAAGZ;QACJ,MAAMa,aAAaH,SAASC;QAE5B,SAAS;QACT,eAAeG;YACX,2BAA2B;YAC3B,IAAI,OAAOD,eAAe,YAAY;gBAClC,OAAOA;YACX;YAEA,MAAME,YAAYpB,gBAAgBgB,MAAM;YAExC,qCAAqC;YACrC,IAAI,OAAOE,eAAe,YAAYA,eAAe,IAAI;gBACrD,oDAAoD;gBACpD,MAAMG,gBAAgBD,UAAUE,GAAG,CAACJ;gBAEpC,iBA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AClBT,OAAO;oBACPa,MAAM;wBACF,MAAMC,gBAAgB,MAAM9B;wBAC5B,MAAM+B,mBAAmB,MAAMnB;wBAC/B,OAAO;4BACHkB,eAAeA,eAAe1B;4BAC9B4B,iBAAiBD,kBAAkB3B;4BACnC,GAAI,MAAMW,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;aAAE;QACN;QAEA,OAAOW;IACX;IAEA,OAAO1C,4BAA4BJ;AACvC,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../src/core/router/utils/adapters/react-router/transform.tsx"],"sourcesContent":["import type { RouteConfig } from '../../../types';\nimport {\n type RouteObject,\n Navigate\n} from 'react-router-dom';\nimport type { TransformRoutesResult } from '../../transform';\nimport { RouteErrorBoundary } from './RouteErrorBoundary';\nimport type { ComponentImport } from '../../../types';\nimport { logger } from '../../../../../utils';\n\nfunction DefaultRouteHydrateFallback() {\n return (\n <div style={{ padding: 16, textAlign: 'center', color: '#5f6c7b' }}>\n Loading...\n </div>\n );\n}\n\nexport const transformRoutesToReactRoutes = async (\n routes: RouteConfig[], \n transformResult: TransformRoutesResult,\n defaultRouteErrorComponent?: ComponentImport,\n defaultRouteLoadingComponent?: ComponentImport,\n enableHydrateFallback: boolean = false,\n): Promise<RouteObject[]> => {\n\n /**\n * 批量处理路由\n * @param routes 路由组\n */\n function transformRouteToReactRoutes(routes: RouteConfig[]) {\n return routes.flatMap((route: RouteConfig) => transformRouteToReactRoute(route))\n }\n\n /**\n * 处理单个路由\n * @param route 路由\n */\n function transformRouteToReactRoute(route: RouteConfig): RouteObject {\n const {\n isGroup = false,\n enableRedirection = false,\n name,\n path,\n handle,\n children,\n page,\n layout,\n error,\n errors,\n loading,\n } = route;\n const routeError = error ?? errors;\n\n // 获取错误组件\n async function getErrorComponent() {\n // 如果 error/errors 是函数,直接使用\n if (typeof routeError === 'function') {\n return routeError();\n }\n\n const errorsMap = transformResult.errors;\n\n // 判断 error/errors 是否为 string 且不是空字符串\n if (typeof routeError === 'string' && routeError !== '') {\n // 使用 routeError 作为 key 获取 transformResult.errors 的值\n const errorResolver = errorsMap.get(routeError);\n\n // 如果获取不到,返回 null\n if (!errorResolver) {\n return null;\n }\n\n // 如果获取到,返回对应的值(调用动态导入函数)\n if (typeof errorResolver === 'function') {\n return errorResolver();\n }\n\n return null;\n }\n\n // 如果 errors 为空,使用配置的默认错误组件或内置的 RouteErrorBoundary 组件\n if (defaultRouteErrorComponent) {\n // 如果配置了默认错误组件,使用配置的组件\n return defaultRouteErrorComponent();\n }\n\n // 如果没有配置默认错误组件,使用内置的 RouteErrorBoundary 组件\n // 直接导入而非懒加载,因为:\n // 1. RouteErrorBoundary 是框架核心组件,体积小\n // 2. 错误边界需要快速响应,直接导入速度更快\n // 3. 作为基础设施组件,应该保证可用性\n return { default: RouteErrorBoundary };\n }\n\n // 获取转换配置\n function convertConfig(m: any) {\n if (!m) {\n return null;\n }\n const { action, loader, shouldRevalidate, default: Component } = m;\n\n // 如果 Component 不存在,记录警告\n if (!Component) {\n logger.warn(`路由组件未找到 default 导出: ${name}`, m);\n }\n\n return {\n action, // always use action\n loader, // always use loader\n shouldRevalidate,\n Component\n };\n }\n\n // 获取加载组件\n async function getLoadingComponent() {\n if (typeof loading === 'function') {\n return loading();\n }\n\n if (typeof loading === 'string' && loading !== '') {\n const loadingResolver = transformResult.loadings.get(loading);\n if (typeof loadingResolver === 'function') {\n return loadingResolver();\n }\n }\n\n if (defaultRouteLoadingComponent) {\n return defaultRouteLoadingComponent();\n }\n\n return null;\n }\n\n // 获取配置\n async function getConfig(index: boolean = false) {\n // 如果有layout和不是index,返回布局配置\n if (layout && !index) {\n // 如果 layout 是函数,直接使用\n if (typeof layout === 'function') {\n const config = await layout();\n return convertConfig(config);\n }\n\n // 如果 layout 是字符串,从 Map 中获取\n if (typeof layout === 'string') {\n const layoutResolver = transformResult.layouts.get(layout);\n if (!layoutResolver) {\n throw new Error(`未找到名为 ${layout} 的布局组件`);\n }\n const config = await layoutResolver();\n return convertConfig(config);\n }\n }\n\n let pageName = name;\n\n // 如果是notFound则转成404\n if (pageName === 'notFound') {\n pageName = '404'\n }\n\n if (page && (!children?.length || index)) {\n // 如果 page 是函数,直接使用\n if (typeof page === 'function') {\n const config = await page();\n return convertConfig(config);\n }\n\n // 如果 page 是字符串,从 Map 中获取\n if (typeof page === 'string') {\n const viewResolver = transformResult.pages.get(page);\n if (!viewResolver) {\n throw new Error(`未找到名为 ${page} 的页面组件`);\n }\n const config = await viewResolver();\n return convertConfig(config);\n }\n }\n\n return null;\n }\n\n // 获取处理信息,即额外的配置信息\n function getHandle(index: boolean = false) {\n if ((layout || isGroup) && !index) {\n return null\n }\n return {\n ...handle,\n name,\n path\n }\n }\n\n\n const reactRoute: RouteObject = {\n children: [],\n id: name,\n handle: getHandle(),\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const config = await getConfig();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n\n // 如果配置为空,确保至少返回一个空对象,避免展开 undefined\n if (!config) {\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n };\n }\n\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...config\n };\n },\n path\n };\n\n if (enableHydrateFallback) {\n reactRoute.hydrateFallbackElement = <DefaultRouteHydrateFallback />;\n }\n\n // 处理子路由\n if (children?.length) {\n reactRoute.children = children.flatMap(child => transformRouteToReactRoute(child)).sort((a, b) => {\n const orderA = a.handle?.order ?? 99;\n const orderB = b.handle?.order ?? 99;\n return orderA - orderB;\n });\n\n if (page && !isGroup) {\n reactRoute.children.unshift({\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...(await getConfig(true))\n };\n }\n });\n }\n\n if (enableRedirection && isGroup) {\n const [firstChild] = reactRoute.children;\n if (firstChild?.path) {\n reactRoute.children.unshift({\n index: true,\n handle: getHandle(true),\n element: <Navigate to={firstChild.path as string} replace />\n });\n }\n }\n\n }\n // 若存在 layout 且没有子路由,但同时声明了 page,则自动生成 index 子路由以渲染页面\n // 场景:/test 使用自定义 layout,且没有 children 时仍应渲染 page\n if ((!children || children.length === 0) && layout && page && !isGroup) {\n reactRoute.children = [{\n handle: getHandle(true),\n index: true,\n lazy: async () => {\n const ErrorBoundary = await getErrorComponent();\n const LoadingComponent = await getLoadingComponent();\n const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;\n return {\n ErrorBoundary: ErrorBoundary?.default,\n HydrateFallback,\n ...(await getConfig(true))\n };\n }\n }];\n }\n\n return reactRoute;\n }\n\n return transformRouteToReactRoutes(routes);\n}\n"],"names":["Navigate","RouteErrorBoundary","logger","DefaultRouteHydrateFallback","div","style","padding","textAlign","color","transformRoutesToReactRoutes","routes","transformResult","defaultRouteErrorComponent","defaultRouteLoadingComponent","enableHydrateFallback","transformRouteToReactRoutes","flatMap","route","transformRouteToReactRoute","isGroup","enableRedirection","name","path","handle","children","page","layout","error","errors","loading","routeError","getErrorComponent","errorsMap","errorResolver","get","default","convertConfig","m","action","loader","shouldRevalidate","Component","warn","getLoadingComponent","loadingResolver","loadings","getConfig","index","config","layoutResolver","layouts","Error","pageName","length","viewResolver","pages","getHandle","reactRoute","id","lazy","ErrorBoundary","LoadingComponent","HydrateFallback","hydrateFallbackElement","child","sort","a","b","orderA","order","orderB","unshift","firstChild","element","to","replace"],"mappings":";AACA,SAEIA,QAAQ,QACL,mBAAmB;AAE1B,SAASC,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,MAAM,QAAQ,uBAAuB;AAE9C,SAASC;IACL,qBACI,KAACC;QAAIC,OAAO;YAAEC,SAAS;YAAIC,WAAW;YAAUC,OAAO;QAAU;kBAAG;;AAI5E;AAEA,OAAO,MAAMC,+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AAU;oBAClBT,OAAO;oBACPY,MAAM;wBACF,MAAMC,gBAAgB,MAAM7B;wBAC5B,MAAM8B,mBAAmB,MAAMlB;wBAC/B,MAAMmB,kBAAkBD,kBAAkB1B,WAAWhC;wBACrD,OAAO;4BACHyD,eAAeA,eAAezB;4BAC9B2B;4BACA,GAAI,MAAMhB,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;YACJ;YAEA,IAAI1B,qBAAqBD,SAAS;gBAC9B,MAAM,CAACqD,WAAW,GAAGf,WAAWjC,QAAQ;gBACxC,IAAIgD,YAAYlD,MAAM;oBAClBmC,WAAWjC,QAAQ,CAAC+C,OAAO,CAAC;wBACxBxB,OAAO;wBACPxB,QAAQiC,UAAU;wBAClBiB,uBAAS,KAACzE;4BAAS0E,IAAIF,WAAWlD,IAAI;4BAAYqD,OAAO;;oBAC7D;gBACJ;YACJ;QAEJ;QACA,qDAAqD;QACrD,gDAAgD;QAChD,IAAI,AAAC,CAAA,CAACnD,YAAYA,SAAS6B,MAAM,KAAK,CAAA,KAAM3B,UAAUD,QAAQ,CAACN,SAAS;YACpEsC,WAAWjC,QAAQ,GAAG;gBAAC;oBACnBD,QAAQiC,UAAU;oBAClBT,OAAO;oBACPY,MAAM;wBACF,MAAMC,gBAAgB,MAAM7B;wBAC5B,MAAM8B,mBAAmB,MAAMlB;wBAC/B,MAAMmB,kBAAkBD,kBAAkB1B,WAAWhC;wBACrD,OAAO;4BACHyD,eAAeA,eAAezB;4BAC9B2B;4BACA,GAAI,MAAMhB,UAAU,KAAK;wBAC7B;oBACJ;gBACJ;aAAE;QACN;QAEA,OAAOW;IACX;IAEA,OAAO1C,4BAA4BL;AACvC,EAAC"}
|
|
@@ -55,6 +55,7 @@ export declare class RouterConfigValidator {
|
|
|
55
55
|
priorityThreshold?: number | undefined;
|
|
56
56
|
timeout?: number | undefined;
|
|
57
57
|
} | undefined;
|
|
58
|
+
enableHydrateFallback?: boolean | undefined;
|
|
58
59
|
};
|
|
59
60
|
/**
|
|
60
61
|
* 安全验证路由配置(不抛出异常)
|
|
@@ -103,6 +104,7 @@ export declare class RouterConfigValidator {
|
|
|
103
104
|
priorityThreshold?: number | undefined;
|
|
104
105
|
timeout?: number | undefined;
|
|
105
106
|
} | undefined;
|
|
107
|
+
enableHydrateFallback?: boolean | undefined;
|
|
106
108
|
}>;
|
|
107
109
|
/**
|
|
108
110
|
* 验证路由配置并提供修复建议
|
|
@@ -120,7 +120,8 @@ const routerConfigSchema = _zod.z.object({
|
|
|
120
120
|
delay: _zod.z.number().int().positive().optional(),
|
|
121
121
|
priorityThreshold: _zod.z.number().int().nonnegative().optional(),
|
|
122
122
|
timeout: _zod.z.number().int().positive().optional()
|
|
123
|
-
}).optional()
|
|
123
|
+
}).optional(),
|
|
124
|
+
enableHydrateFallback: _zod.z.boolean().optional()
|
|
124
125
|
}).superRefine((config, ctx)=>{
|
|
125
126
|
if (!Array.isArray(config.routes)) {
|
|
126
127
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/core/router/validation/schema.ts"],"sourcesContent":["/**\n * 路由配置验证 Schema(使用 Zod)\n */\n\nimport { z } from 'zod';\n\n/**\n * RouteItemHandle Schema\n */\nexport const routeItemHandleSchema = z.object({\n title: z.string().min(1, '路由标题不能为空'),\n i18nKey: z.string().optional(),\n order: z.number().int('排序值必须是整数'),\n icon: z.string().optional(),\n hideInMenu: z.boolean().optional(),\n hideFooter: z.boolean().optional(),\n keepAlive: z.boolean().optional(),\n needLogin: z.boolean().optional(),\n roles: z.array(z.string()).optional(),\n name: z.string().optional(),\n}).passthrough(); // 允许其他自定义字段\n\n/**\n * ComponentImport Schema\n * 可以是字符串路径或函数\n */\nexport const componentImportSchema = z.union([\n z.string().min(1, '组件路径不能为空'),\n z.function(),\n z.null(),\n]);\n\nconst routerOptionsSchema = z\n .object({\n basename: z.string().optional(),\n future: z.record(z.string(), z.union([z.boolean(), z.string(), z.number()])).optional(),\n hydrationData: z.unknown().optional(),\n window: z.unknown().optional(),\n initialEntries: z.array(z.string()).optional(),\n initialIndex: z.number().int().nonnegative().optional(),\n })\n .passthrough();\n\nconst transformOptionsSchema = z\n .object({\n pathValidation: z\n .object({\n enablePathValidation: z.boolean().optional(),\n allowedPathPrefixes: z.array(z.string().min(1)).optional(),\n maxPathLength: z.number().int().positive().optional(),\n allowAbsolutePaths: z.boolean().optional(),\n })\n .optional(),\n performance: z\n .object({\n enableCache: z.boolean().optional(),\n cacheTTL: z.number().int().positive().optional(),\n maxCacheSize: z.number().int().positive().optional(),\n maxRecursionDepth: z.number().int().positive().optional(),\n enableParallelProcessing: z.boolean().optional(),\n parallelBatchSize: z.number().int().positive().optional(),\n })\n .optional(),\n enableDebugLog: z.boolean().optional(),\n })\n .passthrough();\n\n/**\n * RouteConfig Schema\n */\nexport const routeConfigSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n layout: componentImportSchema.optional().nullable(),\n page: componentImportSchema.optional().nullable(),\n loading: componentImportSchema.optional().nullable(),\n error: componentImportSchema.optional().nullable(),\n errors: componentImportSchema.optional().nullable(),\n name: z.string().min(1, '路由名称不能为空'),\n path: z.string().optional(),\n isGroup: z.boolean().optional(),\n enableRedirection: z.boolean().optional(),\n handle: routeItemHandleSchema,\n children: z.array(routeConfigSchema).optional(),\n })\n);\n\n/**\n * RouterConfig Schema\n */\nexport const routerConfigSchema = z.object({\n enabled: z.union([z.boolean(), z.literal('disabled')]).optional(),\n routes: z\n .union([\n z.array(routeConfigSchema),\n z.function(),\n ])\n .optional(),\n mode: z.enum(['browser', 'hash', 'memory']).optional(),\n options: routerOptionsSchema.optional(),\n pathResolve: z\n .object({\n basePath: z.string().optional(),\n pathAliases: z.record(z.string(), z.string()).optional(),\n })\n .optional(),\n transformOptions: transformOptionsSchema.optional(),\n preload: z\n .object({\n strategy: z.enum(['none', 'next-level', 'all', 'visible']).optional(),\n delay: z.number().int().positive().optional(),\n priorityThreshold: z.number().int().nonnegative().optional(),\n timeout: z.number().int().positive().optional(),\n })\n .optional(),\n}).superRefine((config, ctx) => {\n if (!Array.isArray(config.routes)) {\n return;\n }\n\n const routeNameMap = new Map<string, string>();\n\n const walk = (routes: Array<z.infer<typeof routeConfigSchema>>, parentPath: string): void => {\n routes.forEach((route, index) => {\n const currentPath = `${parentPath}[${index}]`;\n const existingPath = routeNameMap.get(route.name);\n if (existingPath) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['routes', index, 'name'],\n message: `路由名称重复: \"${route.name}\",首次定义位置 ${existingPath}`,\n });\n } else {\n routeNameMap.set(route.name, currentPath);\n }\n\n if (Array.isArray(route.children) && route.children.length > 0) {\n walk(route.children as Array<z.infer<typeof routeConfigSchema>>, `${currentPath}.children`);\n }\n });\n };\n\n walk(config.routes, 'routes');\n});\n\n/**\n * 验证路由配置\n * \n * @param config - 要验证的路由配置\n * @returns 验证后的路由配置\n * @throws z.ZodError 如果配置验证失败\n */\nexport function validateRouterConfig(config: unknown) {\n return routerConfigSchema.parse(config);\n}\n\n/**\n * 安全验证路由配置(不抛出异常)\n * \n * @param config - 要验证的路由配置\n * @returns 验证结果\n */\nexport function safeValidateRouterConfig(config: unknown) {\n return routerConfigSchema.safeParse(config);\n}\n"],"names":["componentImportSchema","routeConfigSchema","routeItemHandleSchema","routerConfigSchema","safeValidateRouterConfig","validateRouterConfig","z","object","title","string","min","i18nKey","optional","order","number","int","icon","hideInMenu","boolean","hideFooter","keepAlive","needLogin","roles","array","name","passthrough","union","function","null","routerOptionsSchema","basename","future","record","hydrationData","unknown","window","initialEntries","initialIndex","nonnegative","transformOptionsSchema","pathValidation","enablePathValidation","allowedPathPrefixes","maxPathLength","positive","allowAbsolutePaths","performance","enableCache","cacheTTL","maxCacheSize","maxRecursionDepth","enableParallelProcessing","parallelBatchSize","enableDebugLog","lazy","layout","nullable","page","loading","error","errors","path","isGroup","enableRedirection","handle","children","enabled","literal","routes","mode","enum","options","pathResolve","basePath","pathAliases","transformOptions","preload","strategy","delay","priorityThreshold","timeout","superRefine","config","ctx","Array","isArray","routeNameMap","Map","walk","parentPath","forEach","route","index","currentPath","existingPath","get","addIssue","code","ZodIssueCode","custom","message","set","length","parse","safeParse"],"mappings":"AAAA;;CAEC;;;;;;;;;;;QAwBYA;eAAAA;;QA4CAC;eAAAA;;QA7DAC;eAAAA;;QAgFAC;eAAAA;;QAwEGC;eAAAA;;QAVAC;eAAAA;;;qBAnJE;AAKX,MAAMH,wBAAwBI,MAAC,CAACC,MAAM,CAAC;IAC5CC,OAAOF,MAAC,CAACG,MAAM,GAAGC,GAAG,CAAC,GAAG;IACzBC,SAASL,MAAC,CAACG,MAAM,GAAGG,QAAQ;IAC5BC,OAAOP,MAAC,CAACQ,MAAM,GAAGC,GAAG,CAAC;IACtBC,MAAMV,MAAC,CAACG,MAAM,GAAGG,QAAQ;IACzBK,YAAYX,MAAC,CAACY,OAAO,GAAGN,QAAQ;IAChCO,YAAYb,MAAC,CAACY,OAAO,GAAGN,QAAQ;IAChCQ,WAAWd,MAAC,CAACY,OAAO,GAAGN,QAAQ;IAC/BS,WAAWf,MAAC,CAACY,OAAO,GAAGN,QAAQ;IAC/BU,OAAOhB,MAAC,CAACiB,KAAK,CAACjB,MAAC,CAACG,MAAM,IAAIG,QAAQ;IACnCY,MAAMlB,MAAC,CAACG,MAAM,GAAGG,QAAQ;AAC3B,GAAGa,WAAW,IAAI,YAAY;AAMvB,MAAMzB,wBAAwBM,MAAC,CAACoB,KAAK,CAAC;IAC3CpB,MAAC,CAACG,MAAM,GAAGC,GAAG,CAAC,GAAG;IAClBJ,MAAC,CAACqB,QAAQ;IACVrB,MAAC,CAACsB,IAAI;CACP;AAED,MAAMC,sBAAsBvB,MAAC,CAC1BC,MAAM,CAAC;IACNuB,UAAUxB,MAAC,CAACG,MAAM,GAAGG,QAAQ;IAC7BmB,QAAQzB,MAAC,CAAC0B,MAAM,CAAC1B,MAAC,CAACG,MAAM,IAAIH,MAAC,CAACoB,KAAK,CAAC;QAACpB,MAAC,CAACY,OAA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GAAG,GAAG;gBAC9Dd,KAAKG,MAAM1B,QAAQ,EAA8C,GAAG4B,YAAY,SAAS,CAAC;YAC5F;QACF;IACF;IAEAL,KAAKN,OAAOd,MAAM,EAAE;AACtB;AASO,SAAS/D,qBAAqB6E,MAAe;IAClD,OAAO/E,mBAAmBoG,KAAK,CAACrB;AAClC;AAQO,SAAS9E,yBAAyB8E,MAAe;IACtD,OAAO/E,mBAAmBqG,SAAS,CAACtB;AACtC"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/router/validation/schema.ts"],"sourcesContent":["/**\n * 路由配置验证 Schema(使用 Zod)\n */\n\nimport { z } from 'zod';\n\n/**\n * RouteItemHandle Schema\n */\nexport const routeItemHandleSchema = z.object({\n title: z.string().min(1, '路由标题不能为空'),\n i18nKey: z.string().optional(),\n order: z.number().int('排序值必须是整数'),\n icon: z.string().optional(),\n hideInMenu: z.boolean().optional(),\n hideFooter: z.boolean().optional(),\n keepAlive: z.boolean().optional(),\n needLogin: z.boolean().optional(),\n roles: z.array(z.string()).optional(),\n name: z.string().optional(),\n}).passthrough(); // 允许其他自定义字段\n\n/**\n * ComponentImport Schema\n * 可以是字符串路径或函数\n */\nexport const componentImportSchema = z.union([\n z.string().min(1, '组件路径不能为空'),\n z.function(),\n z.null(),\n]);\n\nconst routerOptionsSchema = z\n .object({\n basename: z.string().optional(),\n future: z.record(z.string(), z.union([z.boolean(), z.string(), z.number()])).optional(),\n hydrationData: z.unknown().optional(),\n window: z.unknown().optional(),\n initialEntries: z.array(z.string()).optional(),\n initialIndex: z.number().int().nonnegative().optional(),\n })\n .passthrough();\n\nconst transformOptionsSchema = z\n .object({\n pathValidation: z\n .object({\n enablePathValidation: z.boolean().optional(),\n allowedPathPrefixes: z.array(z.string().min(1)).optional(),\n maxPathLength: z.number().int().positive().optional(),\n allowAbsolutePaths: z.boolean().optional(),\n })\n .optional(),\n performance: z\n .object({\n enableCache: z.boolean().optional(),\n cacheTTL: z.number().int().positive().optional(),\n maxCacheSize: z.number().int().positive().optional(),\n maxRecursionDepth: z.number().int().positive().optional(),\n enableParallelProcessing: z.boolean().optional(),\n parallelBatchSize: z.number().int().positive().optional(),\n })\n .optional(),\n enableDebugLog: z.boolean().optional(),\n })\n .passthrough();\n\n/**\n * RouteConfig Schema\n */\nexport const routeConfigSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n layout: componentImportSchema.optional().nullable(),\n page: componentImportSchema.optional().nullable(),\n loading: componentImportSchema.optional().nullable(),\n error: componentImportSchema.optional().nullable(),\n errors: componentImportSchema.optional().nullable(),\n name: z.string().min(1, '路由名称不能为空'),\n path: z.string().optional(),\n isGroup: z.boolean().optional(),\n enableRedirection: z.boolean().optional(),\n handle: routeItemHandleSchema,\n children: z.array(routeConfigSchema).optional(),\n })\n);\n\n/**\n * RouterConfig Schema\n */\nexport const routerConfigSchema = z.object({\n enabled: z.union([z.boolean(), z.literal('disabled')]).optional(),\n routes: z\n .union([\n z.array(routeConfigSchema),\n z.function(),\n ])\n .optional(),\n mode: z.enum(['browser', 'hash', 'memory']).optional(),\n options: routerOptionsSchema.optional(),\n pathResolve: z\n .object({\n basePath: z.string().optional(),\n pathAliases: z.record(z.string(), z.string()).optional(),\n })\n .optional(),\n transformOptions: transformOptionsSchema.optional(),\n preload: z\n .object({\n strategy: z.enum(['none', 'next-level', 'all', 'visible']).optional(),\n delay: z.number().int().positive().optional(),\n priorityThreshold: z.number().int().nonnegative().optional(),\n timeout: z.number().int().positive().optional(),\n })\n .optional(),\n enableHydrateFallback: z.boolean().optional(),\n}).superRefine((config, ctx) => {\n if (!Array.isArray(config.routes)) {\n return;\n }\n\n const routeNameMap = new Map<string, string>();\n\n const walk = (routes: Array<z.infer<typeof routeConfigSchema>>, parentPath: string): void => {\n routes.forEach((route, index) => {\n const currentPath = `${parentPath}[${index}]`;\n const existingPath = routeNameMap.get(route.name);\n if (existingPath) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['routes', index, 'name'],\n message: `路由名称重复: \"${route.name}\",首次定义位置 ${existingPath}`,\n });\n } else {\n routeNameMap.set(route.name, currentPath);\n }\n\n if (Array.isArray(route.children) && route.children.length > 0) {\n walk(route.children as Array<z.infer<typeof routeConfigSchema>>, `${currentPath}.children`);\n }\n });\n };\n\n walk(config.routes, 'routes');\n});\n\n/**\n * 验证路由配置\n * \n * @param config - 要验证的路由配置\n * @returns 验证后的路由配置\n * @throws z.ZodError 如果配置验证失败\n */\nexport function validateRouterConfig(config: unknown) {\n return routerConfigSchema.parse(config);\n}\n\n/**\n * 安全验证路由配置(不抛出异常)\n * \n * @param config - 要验证的路由配置\n * @returns 验证结果\n */\nexport function safeValidateRouterConfig(config: unknown) {\n return routerConfigSchema.safeParse(config);\n}\n"],"names":["componentImportSchema","routeConfigSchema","routeItemHandleSchema","routerConfigSchema","safeValidateRouterConfig","validateRouterConfig","z","object","title","string","min","i18nKey","optional","order","number","int","icon","hideInMenu","boolean","hideFooter","keepAlive","needLogin","roles","array","name","passthrough","union","function","null","routerOptionsSchema","basename","future","record","hydrationData","unknown","window","initialEntries","initialIndex","nonnegative","transformOptionsSchema","pathValidation","enablePathValidation","allowedPathPrefixes","maxPathLength","positive","allowAbsolutePaths","performance","enableCache","cacheTTL","maxCacheSize","maxRecursionDepth","enableParallelProcessing","parallelBatchSize","enableDebugLog","lazy","layout","nullable","page","loading","error","errors","path","isGroup","enableRedirection","handle","children","enabled","literal","routes","mode","enum","options","pathResolve","basePath","pathAliases","transformOptions","preload","strategy","delay","priorityThreshold","timeout","enableHydrateFallback","superRefine","config","ctx","Array","isArray","routeNameMap","Map","walk","parentPath","forEach","route","index","currentPath","existingPath","get","addIssue","code","ZodIssueCode","custom","message","set","length","parse","safeParse"],"mappings":"AAAA;;CAEC;;;;;;;;;;;QAwBYA;eAAAA;;QA4CAC;eAAAA;;QA7DAC;eAAAA;;QAgFAC;eAAAA;;QAyEGC;eAAAA;;QAVAC;eAAAA;;;qBApJE;AAKX,MAAMH,wBAAwBI,MAAC,CAAC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B+E,MAAe;IACtD,OAAOhF,mBAAmBsG,SAAS,CAACtB;AACtC"}
|
|
@@ -77,6 +77,7 @@ export declare const routerConfigSchema: z.ZodObject<{
|
|
|
77
77
|
priorityThreshold: z.ZodOptional<z.ZodNumber>;
|
|
78
78
|
timeout: z.ZodOptional<z.ZodNumber>;
|
|
79
79
|
}, z.core.$strip>>;
|
|
80
|
+
enableHydrateFallback: z.ZodOptional<z.ZodBoolean>;
|
|
80
81
|
}, z.core.$strip>;
|
|
81
82
|
/**
|
|
82
83
|
* 验证路由配置
|
|
@@ -126,6 +127,7 @@ export declare function validateRouterConfig(config: unknown): {
|
|
|
126
127
|
priorityThreshold?: number | undefined;
|
|
127
128
|
timeout?: number | undefined;
|
|
128
129
|
} | undefined;
|
|
130
|
+
enableHydrateFallback?: boolean | undefined;
|
|
129
131
|
};
|
|
130
132
|
/**
|
|
131
133
|
* 安全验证路由配置(不抛出异常)
|
|
@@ -174,4 +176,5 @@ export declare function safeValidateRouterConfig(config: unknown): z.ZodSafePars
|
|
|
174
176
|
priorityThreshold?: number | undefined;
|
|
175
177
|
timeout?: number | undefined;
|
|
176
178
|
} | undefined;
|
|
179
|
+
enableHydrateFallback?: boolean | undefined;
|
|
177
180
|
}>;
|
|
@@ -99,7 +99,8 @@ const transformOptionsSchema = z.object({
|
|
|
99
99
|
delay: z.number().int().positive().optional(),
|
|
100
100
|
priorityThreshold: z.number().int().nonnegative().optional(),
|
|
101
101
|
timeout: z.number().int().positive().optional()
|
|
102
|
-
}).optional()
|
|
102
|
+
}).optional(),
|
|
103
|
+
enableHydrateFallback: z.boolean().optional()
|
|
103
104
|
}).superRefine((config, ctx)=>{
|
|
104
105
|
if (!Array.isArray(config.routes)) {
|
|
105
106
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/core/router/validation/schema.ts"],"sourcesContent":["/**\n * 路由配置验证 Schema(使用 Zod)\n */\n\nimport { z } from 'zod';\n\n/**\n * RouteItemHandle Schema\n */\nexport const routeItemHandleSchema = z.object({\n title: z.string().min(1, '路由标题不能为空'),\n i18nKey: z.string().optional(),\n order: z.number().int('排序值必须是整数'),\n icon: z.string().optional(),\n hideInMenu: z.boolean().optional(),\n hideFooter: z.boolean().optional(),\n keepAlive: z.boolean().optional(),\n needLogin: z.boolean().optional(),\n roles: z.array(z.string()).optional(),\n name: z.string().optional(),\n}).passthrough(); // 允许其他自定义字段\n\n/**\n * ComponentImport Schema\n * 可以是字符串路径或函数\n */\nexport const componentImportSchema = z.union([\n z.string().min(1, '组件路径不能为空'),\n z.function(),\n z.null(),\n]);\n\nconst routerOptionsSchema = z\n .object({\n basename: z.string().optional(),\n future: z.record(z.string(), z.union([z.boolean(), z.string(), z.number()])).optional(),\n hydrationData: z.unknown().optional(),\n window: z.unknown().optional(),\n initialEntries: z.array(z.string()).optional(),\n initialIndex: z.number().int().nonnegative().optional(),\n })\n .passthrough();\n\nconst transformOptionsSchema = z\n .object({\n pathValidation: z\n .object({\n enablePathValidation: z.boolean().optional(),\n allowedPathPrefixes: z.array(z.string().min(1)).optional(),\n maxPathLength: z.number().int().positive().optional(),\n allowAbsolutePaths: z.boolean().optional(),\n })\n .optional(),\n performance: z\n .object({\n enableCache: z.boolean().optional(),\n cacheTTL: z.number().int().positive().optional(),\n maxCacheSize: z.number().int().positive().optional(),\n maxRecursionDepth: z.number().int().positive().optional(),\n enableParallelProcessing: z.boolean().optional(),\n parallelBatchSize: z.number().int().positive().optional(),\n })\n .optional(),\n enableDebugLog: z.boolean().optional(),\n })\n .passthrough();\n\n/**\n * RouteConfig Schema\n */\nexport const routeConfigSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n layout: componentImportSchema.optional().nullable(),\n page: componentImportSchema.optional().nullable(),\n loading: componentImportSchema.optional().nullable(),\n error: componentImportSchema.optional().nullable(),\n errors: componentImportSchema.optional().nullable(),\n name: z.string().min(1, '路由名称不能为空'),\n path: z.string().optional(),\n isGroup: z.boolean().optional(),\n enableRedirection: z.boolean().optional(),\n handle: routeItemHandleSchema,\n children: z.array(routeConfigSchema).optional(),\n })\n);\n\n/**\n * RouterConfig Schema\n */\nexport const routerConfigSchema = z.object({\n enabled: z.union([z.boolean(), z.literal('disabled')]).optional(),\n routes: z\n .union([\n z.array(routeConfigSchema),\n z.function(),\n ])\n .optional(),\n mode: z.enum(['browser', 'hash', 'memory']).optional(),\n options: routerOptionsSchema.optional(),\n pathResolve: z\n .object({\n basePath: z.string().optional(),\n pathAliases: z.record(z.string(), z.string()).optional(),\n })\n .optional(),\n transformOptions: transformOptionsSchema.optional(),\n preload: z\n .object({\n strategy: z.enum(['none', 'next-level', 'all', 'visible']).optional(),\n delay: z.number().int().positive().optional(),\n priorityThreshold: z.number().int().nonnegative().optional(),\n timeout: z.number().int().positive().optional(),\n })\n .optional(),\n}).superRefine((config, ctx) => {\n if (!Array.isArray(config.routes)) {\n return;\n }\n\n const routeNameMap = new Map<string, string>();\n\n const walk = (routes: Array<z.infer<typeof routeConfigSchema>>, parentPath: string): void => {\n routes.forEach((route, index) => {\n const currentPath = `${parentPath}[${index}]`;\n const existingPath = routeNameMap.get(route.name);\n if (existingPath) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['routes', index, 'name'],\n message: `路由名称重复: \"${route.name}\",首次定义位置 ${existingPath}`,\n });\n } else {\n routeNameMap.set(route.name, currentPath);\n }\n\n if (Array.isArray(route.children) && route.children.length > 0) {\n walk(route.children as Array<z.infer<typeof routeConfigSchema>>, `${currentPath}.children`);\n }\n });\n };\n\n walk(config.routes, 'routes');\n});\n\n/**\n * 验证路由配置\n * \n * @param config - 要验证的路由配置\n * @returns 验证后的路由配置\n * @throws z.ZodError 如果配置验证失败\n */\nexport function validateRouterConfig(config: unknown) {\n return routerConfigSchema.parse(config);\n}\n\n/**\n * 安全验证路由配置(不抛出异常)\n * \n * @param config - 要验证的路由配置\n * @returns 验证结果\n */\nexport function safeValidateRouterConfig(config: unknown) {\n return routerConfigSchema.safeParse(config);\n}\n"],"names":["z","routeItemHandleSchema","object","title","string","min","i18nKey","optional","order","number","int","icon","hideInMenu","boolean","hideFooter","keepAlive","needLogin","roles","array","name","passthrough","componentImportSchema","union","function","null","routerOptionsSchema","basename","future","record","hydrationData","unknown","window","initialEntries","initialIndex","nonnegative","transformOptionsSchema","pathValidation","enablePathValidation","allowedPathPrefixes","maxPathLength","positive","allowAbsolutePaths","performance","enableCache","cacheTTL","maxCacheSize","maxRecursionDepth","enableParallelProcessing","parallelBatchSize","enableDebugLog","routeConfigSchema","lazy","layout","nullable","page","loading","error","errors","path","isGroup","enableRedirection","handle","children","routerConfigSchema","enabled","literal","routes","mode","enum","options","pathResolve","basePath","pathAliases","transformOptions","preload","strategy","delay","priorityThreshold","timeout","superRefine","config","ctx","Array","isArray","routeNameMap","Map","walk","parentPath","forEach","route","index","currentPath","existingPath","get","addIssue","code","ZodIssueCode","custom","message","set","length","validateRouterConfig","parse","safeValidateRouterConfig","safeParse"],"mappings":"AAAA;;CAEC,GAED,SAASA,CAAC,QAAQ,MAAM;AAExB;;CAEC,GACD,OAAO,MAAMC,wBAAwBD,EAAEE,MAAM,CAAC;IAC5CC,OAAOH,EAAEI,MAAM,GAAGC,GAAG,CAAC,GAAG;IACzBC,SAASN,EAAEI,MAAM,GAAGG,QAAQ;IAC5BC,OAAOR,EAAES,MAAM,GAAGC,GAAG,CAAC;IACtBC,MAAMX,EAAEI,MAAM,GAAGG,QAAQ;IACzBK,YAAYZ,EAAEa,OAAO,GAAGN,QAAQ;IAChCO,YAAYd,EAAEa,OAAO,GAAGN,QAAQ;IAChCQ,WAAWf,EAAEa,OAAO,GAAGN,QAAQ;IAC/BS,WAAWhB,EAAEa,OAAO,GAAGN,QAAQ;IAC/BU,OAAOjB,EAAEkB,KAAK,CAAClB,EAAEI,MAAM,IAAIG,QAAQ;IACnCY,MAAMnB,EAAEI,MAAM,GAAGG,QAAQ;AAC3B,GAAGa,WAAW,GAAG,CAAC,YAAY;AAE9B;;;CAGC,G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AACV,MAAMtE,IAAI,EAAEwE;YAC/B;YAEA,IAAIT,MAAMC,OAAO,CAACM,MAAM3B,QAAQ,KAAK2B,MAAM3B,QAAQ,CAACsC,MAAM,GAAG,GAAG;gBAC9Dd,KAAKG,MAAM3B,QAAQ,EAA8C,GAAG6B,YAAY,SAAS,CAAC;YAC5F;QACF;IACF;IAEAL,KAAKN,OAAOd,MAAM,EAAE;AACtB,GAAG;AAEH;;;;;;CAMC,GACD,OAAO,SAASmC,qBAAqBrB,MAAe;IAClD,OAAOjB,mBAAmBuC,KAAK,CAACtB;AAClC;AAEA;;;;;CAKC,GACD,OAAO,SAASuB,yBAAyBvB,MAAe;IACtD,OAAOjB,mBAAmByC,SAAS,CAACxB;AACtC"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/router/validation/schema.ts"],"sourcesContent":["/**\n * 路由配置验证 Schema(使用 Zod)\n */\n\nimport { z } from 'zod';\n\n/**\n * RouteItemHandle Schema\n */\nexport const routeItemHandleSchema = z.object({\n title: z.string().min(1, '路由标题不能为空'),\n i18nKey: z.string().optional(),\n order: z.number().int('排序值必须是整数'),\n icon: z.string().optional(),\n hideInMenu: z.boolean().optional(),\n hideFooter: z.boolean().optional(),\n keepAlive: z.boolean().optional(),\n needLogin: z.boolean().optional(),\n roles: z.array(z.string()).optional(),\n name: z.string().optional(),\n}).passthrough(); // 允许其他自定义字段\n\n/**\n * ComponentImport Schema\n * 可以是字符串路径或函数\n */\nexport const componentImportSchema = z.union([\n z.string().min(1, '组件路径不能为空'),\n z.function(),\n z.null(),\n]);\n\nconst routerOptionsSchema = z\n .object({\n basename: z.string().optional(),\n future: z.record(z.string(), z.union([z.boolean(), z.string(), z.number()])).optional(),\n hydrationData: z.unknown().optional(),\n window: z.unknown().optional(),\n initialEntries: z.array(z.string()).optional(),\n initialIndex: z.number().int().nonnegative().optional(),\n })\n .passthrough();\n\nconst transformOptionsSchema = z\n .object({\n pathValidation: z\n .object({\n enablePathValidation: z.boolean().optional(),\n allowedPathPrefixes: z.array(z.string().min(1)).optional(),\n maxPathLength: z.number().int().positive().optional(),\n allowAbsolutePaths: z.boolean().optional(),\n })\n .optional(),\n performance: z\n .object({\n enableCache: z.boolean().optional(),\n cacheTTL: z.number().int().positive().optional(),\n maxCacheSize: z.number().int().positive().optional(),\n maxRecursionDepth: z.number().int().positive().optional(),\n enableParallelProcessing: z.boolean().optional(),\n parallelBatchSize: z.number().int().positive().optional(),\n })\n .optional(),\n enableDebugLog: z.boolean().optional(),\n })\n .passthrough();\n\n/**\n * RouteConfig Schema\n */\nexport const routeConfigSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n layout: componentImportSchema.optional().nullable(),\n page: componentImportSchema.optional().nullable(),\n loading: componentImportSchema.optional().nullable(),\n error: componentImportSchema.optional().nullable(),\n errors: componentImportSchema.optional().nullable(),\n name: z.string().min(1, '路由名称不能为空'),\n path: z.string().optional(),\n isGroup: z.boolean().optional(),\n enableRedirection: z.boolean().optional(),\n handle: routeItemHandleSchema,\n children: z.array(routeConfigSchema).optional(),\n })\n);\n\n/**\n * RouterConfig Schema\n */\nexport const routerConfigSchema = z.object({\n enabled: z.union([z.boolean(), z.literal('disabled')]).optional(),\n routes: z\n .union([\n z.array(routeConfigSchema),\n z.function(),\n ])\n .optional(),\n mode: z.enum(['browser', 'hash', 'memory']).optional(),\n options: routerOptionsSchema.optional(),\n pathResolve: z\n .object({\n basePath: z.string().optional(),\n pathAliases: z.record(z.string(), z.string()).optional(),\n })\n .optional(),\n transformOptions: transformOptionsSchema.optional(),\n preload: z\n .object({\n strategy: z.enum(['none', 'next-level', 'all', 'visible']).optional(),\n delay: z.number().int().positive().optional(),\n priorityThreshold: z.number().int().nonnegative().optional(),\n timeout: z.number().int().positive().optional(),\n })\n .optional(),\n enableHydrateFallback: z.boolean().optional(),\n}).superRefine((config, ctx) => {\n if (!Array.isArray(config.routes)) {\n return;\n }\n\n const routeNameMap = new Map<string, string>();\n\n const walk = (routes: Array<z.infer<typeof routeConfigSchema>>, parentPath: string): void => {\n routes.forEach((route, index) => {\n const currentPath = `${parentPath}[${index}]`;\n const existingPath = routeNameMap.get(route.name);\n if (existingPath) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['routes', index, 'name'],\n message: `路由名称重复: \"${route.name}\",首次定义位置 ${existingPath}`,\n });\n } else {\n routeNameMap.set(route.name, currentPath);\n }\n\n if (Array.isArray(route.children) && route.children.length > 0) {\n walk(route.children as Array<z.infer<typeof routeConfigSchema>>, `${currentPath}.children`);\n }\n });\n };\n\n walk(config.routes, 'routes');\n});\n\n/**\n * 验证路由配置\n * \n * @param config - 要验证的路由配置\n * @returns 验证后的路由配置\n * @throws z.ZodError 如果配置验证失败\n */\nexport function validateRouterConfig(config: unknown) {\n return routerConfigSchema.parse(config);\n}\n\n/**\n * 安全验证路由配置(不抛出异常)\n * \n * @param config - 要验证的路由配置\n * @returns 验证结果\n */\nexport function safeValidateRouterConfig(config: unknown) {\n return routerConfigSchema.safeParse(config);\n}\n"],"names":["z","routeItemHandleSchema","object","title","string","min","i18nKey","optional","order","number","int","icon","hideInMenu","boolean","hideFooter","keepAlive","needLogin","roles","array","name","passthrough","componentImportSchema","union","function","null","routerOptionsSchema","basename","future","record","hydrationData","unknown","window","initialEntries","initialIndex","nonnegative","transformOptionsSchema","pathValidation","enablePathValidation","allowedPathPrefixes","maxPathLength","positive","allowAbsolutePaths","performance","enableCache","cacheTTL","maxCacheSize","maxRecursionDepth","enableParallelProcessing","parallelBatchSize","enableDebugLog","routeConfigSchema","lazy","layout","nullable","page","loading","error","errors","path","isGroup","enableRedirection","handle","children","routerConfigSchema","enabled","literal","routes","mode","enum","options","pathResolve","basePath","pathAliases","transformOptions","preload","strategy","delay","priorityThreshold","timeout","enableHydrateFallback","superRefine","config","ctx","Array","isArray","routeNameMap","Map","walk","parentPath","forEach","route","index","currentPath","existingPath","get","addIssue","code","ZodIssueCode","custom","message","set","length","validateRouterConfig","parse","safeValidateRouterConfig","safeParse"],"mappings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
2
|
+
* @vlian/framework v1.2.19
|
|
3
3
|
* Secra Framework - 一个现代化的低代码框架
|
|
4
4
|
* (c) 2026 Secra Framework Contributors
|
|
5
5
|
* Licensed under Apache-2.0
|
|
@@ -25803,7 +25803,7 @@
|
|
|
25803
25803
|
children: "Loading..."
|
|
25804
25804
|
});
|
|
25805
25805
|
}
|
|
25806
|
-
const transformRoutesToReactRoutes = async (routes, transformResult, defaultRouteErrorComponent, defaultRouteLoadingComponent)=>{
|
|
25806
|
+
const transformRoutesToReactRoutes = async (routes, transformResult, defaultRouteErrorComponent, defaultRouteLoadingComponent, enableHydrateFallback = false)=>{
|
|
25807
25807
|
/**
|
|
25808
25808
|
* 批量处理路由
|
|
25809
25809
|
* @param routes 路由组
|
|
@@ -25934,28 +25934,31 @@
|
|
|
25934
25934
|
}
|
|
25935
25935
|
const reactRoute = {
|
|
25936
25936
|
children: [],
|
|
25937
|
-
hydrateFallbackElement: /*#__PURE__*/ jsxRuntime.jsx(DefaultRouteHydrateFallback, {}),
|
|
25938
25937
|
id: name,
|
|
25939
25938
|
handle: getHandle(),
|
|
25940
25939
|
lazy: async ()=>{
|
|
25941
25940
|
const ErrorBoundary = await getErrorComponent();
|
|
25942
25941
|
const config = await getConfig();
|
|
25943
25942
|
const LoadingComponent = await getLoadingComponent();
|
|
25943
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
25944
25944
|
// 如果配置为空,确保至少返回一个空对象,避免展开 undefined
|
|
25945
25945
|
if (!config) {
|
|
25946
25946
|
return {
|
|
25947
25947
|
ErrorBoundary: ErrorBoundary?.default,
|
|
25948
|
-
HydrateFallback
|
|
25948
|
+
HydrateFallback
|
|
25949
25949
|
};
|
|
25950
25950
|
}
|
|
25951
25951
|
return {
|
|
25952
25952
|
ErrorBoundary: ErrorBoundary?.default,
|
|
25953
|
-
HydrateFallback
|
|
25953
|
+
HydrateFallback,
|
|
25954
25954
|
...config
|
|
25955
25955
|
};
|
|
25956
25956
|
},
|
|
25957
25957
|
path
|
|
25958
25958
|
};
|
|
25959
|
+
if (enableHydrateFallback) {
|
|
25960
|
+
reactRoute.hydrateFallbackElement = /*#__PURE__*/ jsxRuntime.jsx(DefaultRouteHydrateFallback, {});
|
|
25961
|
+
}
|
|
25959
25962
|
// 处理子路由
|
|
25960
25963
|
if (children?.length) {
|
|
25961
25964
|
reactRoute.children = children.flatMap((child)=>transformRouteToReactRoute(child)).sort((a, b)=>{
|
|
@@ -25970,9 +25973,10 @@
|
|
|
25970
25973
|
lazy: async ()=>{
|
|
25971
25974
|
const ErrorBoundary = await getErrorComponent();
|
|
25972
25975
|
const LoadingComponent = await getLoadingComponent();
|
|
25976
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
25973
25977
|
return {
|
|
25974
25978
|
ErrorBoundary: ErrorBoundary?.default,
|
|
25975
|
-
HydrateFallback
|
|
25979
|
+
HydrateFallback,
|
|
25976
25980
|
...await getConfig(true)
|
|
25977
25981
|
};
|
|
25978
25982
|
}
|
|
@@ -26002,9 +26006,10 @@
|
|
|
26002
26006
|
lazy: async ()=>{
|
|
26003
26007
|
const ErrorBoundary = await getErrorComponent();
|
|
26004
26008
|
const LoadingComponent = await getLoadingComponent();
|
|
26009
|
+
const HydrateFallback = LoadingComponent?.default ?? DefaultRouteHydrateFallback;
|
|
26005
26010
|
return {
|
|
26006
26011
|
ErrorBoundary: ErrorBoundary?.default,
|
|
26007
|
-
HydrateFallback
|
|
26012
|
+
HydrateFallback,
|
|
26008
26013
|
...await getConfig(true)
|
|
26009
26014
|
};
|
|
26010
26015
|
}
|
|
@@ -26114,7 +26119,8 @@
|
|
|
26114
26119
|
delay: zod.z.number().int().positive().optional(),
|
|
26115
26120
|
priorityThreshold: zod.z.number().int().nonnegative().optional(),
|
|
26116
26121
|
timeout: zod.z.number().int().positive().optional()
|
|
26117
|
-
}).optional()
|
|
26122
|
+
}).optional(),
|
|
26123
|
+
enableHydrateFallback: zod.z.boolean().optional()
|
|
26118
26124
|
}).superRefine((config, ctx)=>{
|
|
26119
26125
|
if (!Array.isArray(config.routes)) {
|
|
26120
26126
|
return;
|
|
@@ -28014,7 +28020,7 @@
|
|
|
28014
28020
|
*/ async initialize(config) {
|
|
28015
28021
|
// 转换路由配置
|
|
28016
28022
|
const transformResult = await transformRoutes(config.routes);
|
|
28017
|
-
const reactRoutes = await transformRoutesToReactRoutes(transformResult.routes, transformResult, config.defaultRouteErrorComponent, config.defaultRouteLoadingComponent);
|
|
28023
|
+
const reactRoutes = await transformRoutesToReactRoutes(transformResult.routes, transformResult, config.defaultRouteErrorComponent, config.defaultRouteLoadingComponent, Boolean(config.options?.hydrationData));
|
|
28018
28024
|
// 创建路由实例
|
|
28019
28025
|
const mode = config.mode || 'browser';
|
|
28020
28026
|
if (mode === 'browser') {
|
|
@@ -28185,14 +28191,15 @@
|
|
|
28185
28191
|
// 缓存转换结果
|
|
28186
28192
|
this.cache.set(cacheKey, result);
|
|
28187
28193
|
}
|
|
28188
|
-
const
|
|
28194
|
+
const enableHydrateFallback = config.enableHydrateFallback ?? Boolean(config.options?.hydrationData);
|
|
28195
|
+
const reactRoutes = await transformRoutesToReactRoutes(result.routes, result, config.defaultRouteErrorComponent, config.defaultRouteLoadingComponent, enableHydrateFallback);
|
|
28189
28196
|
// 注册路由到预加载器
|
|
28190
28197
|
if (result.routes) {
|
|
28191
28198
|
this.preloader.registerRoutes(result.routes, result);
|
|
28192
28199
|
// 开始预加载
|
|
28193
28200
|
this.preloader.startPreload();
|
|
28194
28201
|
}
|
|
28195
|
-
await this.resolveInitialLazyRoutes(reactRoutes);
|
|
28202
|
+
await this.resolveInitialLazyRoutes(reactRoutes, !enableHydrateFallback);
|
|
28196
28203
|
let routerInstance = null;
|
|
28197
28204
|
if (config.mode === 'browser') {
|
|
28198
28205
|
routerInstance = reactRouterDom.createBrowserRouter(reactRoutes, config.options);
|
|
@@ -28225,17 +28232,35 @@
|
|
|
28225
28232
|
logger.info('路由管理器初始化完成');
|
|
28226
28233
|
}
|
|
28227
28234
|
/**
|
|
28228
|
-
*
|
|
28229
|
-
*
|
|
28230
|
-
*/ async resolveInitialLazyRoutes(routes) {
|
|
28235
|
+
* 预解析 lazy 路由,避免首屏或重定向链路长时间停留在 hydrate fallback。
|
|
28236
|
+
* resolveAll=true 时会在初始化阶段解析全部 lazy 路由。
|
|
28237
|
+
*/ async resolveInitialLazyRoutes(routes, resolveAll = false) {
|
|
28231
28238
|
if (typeof window === 'undefined') {
|
|
28232
28239
|
return;
|
|
28233
28240
|
}
|
|
28234
|
-
|
|
28235
|
-
if (
|
|
28236
|
-
|
|
28241
|
+
let targetRoutes = [];
|
|
28242
|
+
if (resolveAll) {
|
|
28243
|
+
const stack = [
|
|
28244
|
+
...routes
|
|
28245
|
+
];
|
|
28246
|
+
while(stack.length > 0){
|
|
28247
|
+
const current = stack.shift();
|
|
28248
|
+
if (!current) {
|
|
28249
|
+
continue;
|
|
28250
|
+
}
|
|
28251
|
+
targetRoutes.push(current);
|
|
28252
|
+
if (current.children && current.children.length > 0) {
|
|
28253
|
+
stack.push(...current.children);
|
|
28254
|
+
}
|
|
28255
|
+
}
|
|
28256
|
+
} else {
|
|
28257
|
+
const matches = reactRouterDom.matchRoutes(routes, window.location.pathname) ?? [];
|
|
28258
|
+
if (matches.length === 0) {
|
|
28259
|
+
return;
|
|
28260
|
+
}
|
|
28261
|
+
targetRoutes = matches.map((match)=>match.route);
|
|
28237
28262
|
}
|
|
28238
|
-
await Promise.all(
|
|
28263
|
+
await Promise.all(targetRoutes.map(async (route)=>{
|
|
28239
28264
|
const lazy = route.lazy;
|
|
28240
28265
|
if (typeof lazy !== 'function') {
|
|
28241
28266
|
return;
|