UNPKG

3.65 kBJavaScriptView Raw
1/**
2 * react-router v8.0.0
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import { RouterContextProvider, convertRoutesToDataRoutes } from "../../router/utils.js";
12import { Outlet, RouterProvider, createMemoryRouter, withComponentProps, withErrorBoundaryProps, withHydrateFallbackProps } from "../../components.js";
13import { FrameworkContext } from "./components.js";
14import * as React$1 from "react";
15//#region lib/dom/ssr/routes-test-stub.tsx
16/**
17* @category Utils
18*/
19function 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 /* @__PURE__ */ React$1.createElement(FrameworkContext.Provider, { value: frameworkContextRef.current }, /* @__PURE__ */ React$1.createElement(RouterProvider, { router: routerRef.current }));
50 };
51}
52function 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//#endregion
108export { createRoutesStub };