| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | import { RouterContextProvider, convertRoutesToDataRoutes } from "../../router/utils.js";
|
| 12 | import { Outlet, RouterProvider, createMemoryRouter, withComponentProps, withErrorBoundaryProps, withHydrateFallbackProps } from "../../components.js";
|
| 13 | import { FrameworkContext } from "./components.js";
|
| 14 | import * as React$1 from "react";
|
| 15 |
|
| 16 | |
| 17 | |
| 18 |
|
| 19 | function createRoutesStub(routes, _context) {
|
| 20 | return function RoutesTestStub({ initialEntries, initialIndex, hydrationData, future }) {
|
| 21 | let routerRef = React$1.useRef(null);
|
| 22 | let frameworkContextRef = React$1.useRef(null);
|
| 23 | if (routerRef.current == null || frameworkContextRef.current == null) {
|
| 24 | frameworkContextRef.current = {
|
| 25 | future: {},
|
| 26 | manifest: {
|
| 27 | routes: {},
|
| 28 | entry: {
|
| 29 | imports: [],
|
| 30 | module: ""
|
| 31 | },
|
| 32 | url: "",
|
| 33 | version: ""
|
| 34 | },
|
| 35 | routeModules: {},
|
| 36 | ssr: false,
|
| 37 | isSpaMode: false,
|
| 38 | routeDiscovery: {
|
| 39 | mode: "lazy",
|
| 40 | manifestPath: "/__manifest"
|
| 41 | }
|
| 42 | };
|
| 43 | routerRef.current = createMemoryRouter(processRoutes(convertRoutesToDataRoutes(routes, (r) => r), _context ?? new RouterContextProvider(), frameworkContextRef.current.manifest, frameworkContextRef.current.routeModules), {
|
| 44 | initialEntries,
|
| 45 | initialIndex,
|
| 46 | hydrationData
|
| 47 | });
|
| 48 | }
|
| 49 | return React$1.createElement(FrameworkContext.Provider, { value: frameworkContextRef.current }, React$1.createElement(RouterProvider, { router: routerRef.current }));
|
| 50 | };
|
| 51 | }
|
| 52 | function processRoutes(routes, context, manifest, routeModules, parentId) {
|
| 53 | return routes.map((route) => {
|
| 54 | if (!route.id) throw new Error("Expected a route.id in react-router processRoutes() function");
|
| 55 | let newRoute = {
|
| 56 | id: route.id,
|
| 57 | path: route.path,
|
| 58 | index: route.index,
|
| 59 | Component: route.Component ? withComponentProps(route.Component) : void 0,
|
| 60 | HydrateFallback: route.HydrateFallback ? withHydrateFallbackProps(route.HydrateFallback) : void 0,
|
| 61 | ErrorBoundary: route.ErrorBoundary ? withErrorBoundaryProps(route.ErrorBoundary) : void 0,
|
| 62 | action: route.action ? (args) => route.action({
|
| 63 | ...args,
|
| 64 | context
|
| 65 | }) : void 0,
|
| 66 | loader: route.loader ? (args) => route.loader({
|
| 67 | ...args,
|
| 68 | context
|
| 69 | }) : void 0,
|
| 70 | middleware: route.middleware ? route.middleware.map((mw) => (...args) => mw({
|
| 71 | ...args[0],
|
| 72 | context
|
| 73 | }, args[1])) : void 0,
|
| 74 | handle: route.handle,
|
| 75 | shouldRevalidate: route.shouldRevalidate
|
| 76 | };
|
| 77 | let entryRoute = {
|
| 78 | id: route.id,
|
| 79 | path: route.path,
|
| 80 | index: route.index,
|
| 81 | parentId,
|
| 82 | hasAction: route.action != null,
|
| 83 | hasLoader: route.loader != null,
|
| 84 | hasClientAction: false,
|
| 85 | hasClientLoader: false,
|
| 86 | hasClientMiddleware: false,
|
| 87 | hasErrorBoundary: route.ErrorBoundary != null,
|
| 88 | module: "build/stub-path-to-module.js",
|
| 89 | clientActionModule: void 0,
|
| 90 | clientLoaderModule: void 0,
|
| 91 | clientMiddlewareModule: void 0,
|
| 92 | hydrateFallbackModule: void 0
|
| 93 | };
|
| 94 | manifest.routes[newRoute.id] = entryRoute;
|
| 95 | routeModules[route.id] = {
|
| 96 | default: newRoute.Component || Outlet,
|
| 97 | ErrorBoundary: newRoute.ErrorBoundary || void 0,
|
| 98 | handle: route.handle,
|
| 99 | links: route.links,
|
| 100 | meta: route.meta,
|
| 101 | shouldRevalidate: route.shouldRevalidate
|
| 102 | };
|
| 103 | if (route.children) newRoute.children = processRoutes(route.children, context, manifest, routeModules, newRoute.id);
|
| 104 | return newRoute;
|
| 105 | });
|
| 106 | }
|
| 107 |
|
| 108 | export { createRoutesStub };
|