From 22ca3b27f66783ffbb97d17207d1fb870d17a90b Mon Sep 17 00:00:00 2001 From: caohuilin Date: Wed, 8 Jan 2025 22:16:33 +0800 Subject: [PATCH] fix: router v5 unit test --- .../runtime/plugin-router-v5/src/runtime/hooks.ts | 3 ++- .../plugin-router-v5/src/runtime/plugin.tsx | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/runtime/plugin-router-v5/src/runtime/hooks.ts b/packages/runtime/plugin-router-v5/src/runtime/hooks.ts index 8063b11162e..11861d4af6f 100644 --- a/packages/runtime/plugin-router-v5/src/runtime/hooks.ts +++ b/packages/runtime/plugin-router-v5/src/runtime/hooks.ts @@ -1,8 +1,9 @@ import { createSyncHook } from '@modern-js/plugin-v2'; import type { RouteProps } from 'react-router-dom'; +import type { SingleRouteConfig } from './plugin'; // only for inhouse use const modifyRoutesHook = - createSyncHook<(routes: RouteProps[]) => RouteProps[]>(); + createSyncHook<(routes: RouteProps[]) => SingleRouteConfig[]>(); export { modifyRoutesHook }; diff --git a/packages/runtime/plugin-router-v5/src/runtime/plugin.tsx b/packages/runtime/plugin-router-v5/src/runtime/plugin.tsx index 9b1914faaaa..b7e16d40065 100644 --- a/packages/runtime/plugin-router-v5/src/runtime/plugin.tsx +++ b/packages/runtime/plugin-router-v5/src/runtime/plugin.tsx @@ -63,7 +63,11 @@ let routes: SingleRouteConfig[] = []; export const routerPlugin = ( userConfig: RouterConfig = {}, -): RuntimePluginFuture => { +): RuntimePluginFuture<{ + extendHooks: { + modifyRoutes: typeof modifyRoutesHook; + }; +}> => { return { name: '@modern-js/plugin-router', registryHooks: { @@ -124,8 +128,8 @@ export const routerPlugin = ( (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions)); - const runner = (api as any).useHookRunners(); - routes = runner.modifyRoutes(originRoutes); + const hooks = api.getHooks(); + routes = hooks.modifyRoutes.call(originRoutes); finalRouteConfig && (finalRouteConfig.routes = routes); /** * when exist createRoutes function, App.tsx must be exist, and support Component props @@ -160,8 +164,8 @@ export const routerPlugin = ( (historyOptions.basename as string), ) : baseUrl; - const runner = (api as any).useHookRunners(); - const routes = runner.modifyRoutes(originRoutes); + const hooks = api.getHooks(); + const routes = hooks.modifyRoutes.call(originRoutes); finalRouteConfig && (finalRouteConfig.routes = routes); return (