@bleedingdev/modern-js-plugin-tanstack 3.5.0-ultramodern.9 → 3.5.0-ultramodern.90

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.
Files changed (270) hide show
  1. package/dist/cjs/cli/artifacts.js +157 -0
  2. package/dist/cjs/cli/index.js +8 -79
  3. package/dist/cjs/cli/tanstackTypes/canonicalRoutes.js +113 -0
  4. package/dist/cjs/cli/tanstackTypes/shared.js +123 -0
  5. package/dist/cjs/cli/tanstackTypes/source.js +255 -0
  6. package/dist/cjs/cli/tanstackTypes.js +4 -350
  7. package/dist/cjs/runtime/blockingSubscribe.js +57 -0
  8. package/dist/cjs/runtime/{pluginCore.js → clientBasepath.js} +12 -19
  9. package/dist/cjs/runtime/clientHydration.js +118 -0
  10. package/dist/cjs/runtime/dataMutation.js +11 -176
  11. package/dist/cjs/runtime/formData.js +116 -0
  12. package/dist/cjs/runtime/loaderBridge.js +7 -11
  13. package/dist/cjs/runtime/plugin.js +21 -125
  14. package/dist/cjs/runtime/plugin.node.js +45 -125
  15. package/dist/cjs/runtime/pluginShared.js +87 -0
  16. package/dist/cjs/runtime/prefetchLink.js +15 -2
  17. package/dist/cjs/runtime/register.js +3 -16
  18. package/dist/cjs/runtime/routeTree/components.js +102 -0
  19. package/dist/cjs/runtime/routeTree/construction.js +152 -0
  20. package/dist/cjs/runtime/routeTree/index.js +47 -0
  21. package/dist/cjs/runtime/routeTree/loaders.js +158 -0
  22. package/dist/cjs/runtime/routeTree/paths.js +57 -0
  23. package/dist/cjs/runtime/routeTree/staticData.js +63 -0
  24. package/dist/cjs/runtime/routeTree/tanstackRoutes.js +57 -0
  25. package/dist/cjs/runtime/routeTree/types.js +18 -0
  26. package/dist/cjs/runtime/routeTree.js +7 -320
  27. package/dist/cjs/runtime/router.js +15 -2
  28. package/dist/cjs/runtime/rsc/CompositeComponent.js +18 -13
  29. package/dist/cjs/runtime/rsc/ReplayableStream.js +0 -4
  30. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +5 -16
  31. package/dist/cjs/runtime/rsc/client.js +8 -25
  32. package/dist/cjs/runtime/rsc/flightSerialization.js +132 -0
  33. package/dist/cjs/runtime/rsc/payloadErrors.js +99 -0
  34. package/dist/cjs/runtime/rsc/payloadFetch.js +105 -0
  35. package/dist/cjs/runtime/rsc/payloadRouter.js +21 -142
  36. package/dist/cjs/runtime/rsc/payloadRoutes.js +94 -0
  37. package/dist/cjs/runtime/rsc/payloadStorage.js +58 -0
  38. package/dist/cjs/runtime/rsc/server.js +14 -18
  39. package/dist/cjs/runtime/rsc/shared.js +68 -0
  40. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +1 -0
  41. package/dist/cjs/runtime/rsc/treeGetter.js +72 -0
  42. package/dist/cjs/runtime/shared/isAbsoluteUrl.js +47 -0
  43. package/dist/cjs/runtime/ssrManagedTags.js +103 -0
  44. package/dist/cjs/runtime/ssrPreload.js +89 -0
  45. package/dist/cjs/runtime/ssrTypes.js +18 -0
  46. package/dist/cjs/runtime/state.js +1 -1
  47. package/dist/cjs/runtime/submitAction.js +161 -0
  48. package/dist/cjs/runtime/utils.js +49 -86
  49. package/dist/esm/cli/artifacts.mjs +103 -0
  50. package/dist/esm/cli/index.mjs +8 -82
  51. package/dist/esm/cli/tanstackTypes/canonicalRoutes.mjs +75 -0
  52. package/dist/esm/cli/tanstackTypes/shared.mjs +61 -0
  53. package/dist/esm/cli/tanstackTypes/source.mjs +207 -0
  54. package/dist/esm/cli/tanstackTypes.mjs +2 -339
  55. package/dist/esm/runtime/blockingSubscribe.mjs +19 -0
  56. package/dist/esm/runtime/clientBasepath.mjs +10 -0
  57. package/dist/esm/runtime/clientHydration.mjs +71 -0
  58. package/dist/esm/runtime/dataMutation.mjs +2 -167
  59. package/dist/esm/runtime/formData.mjs +63 -0
  60. package/dist/esm/runtime/loaderBridge.mjs +5 -9
  61. package/dist/esm/runtime/plugin.mjs +14 -118
  62. package/dist/esm/runtime/plugin.node.mjs +41 -121
  63. package/dist/esm/runtime/pluginShared.mjs +40 -0
  64. package/dist/esm/runtime/prefetchLink.mjs +18 -5
  65. package/dist/esm/runtime/register.mjs +1 -14
  66. package/dist/esm/runtime/routeTree/components.mjs +55 -0
  67. package/dist/esm/runtime/routeTree/construction.mjs +111 -0
  68. package/dist/esm/runtime/routeTree/index.mjs +2 -0
  69. package/dist/esm/runtime/routeTree/loaders.mjs +117 -0
  70. package/dist/esm/runtime/routeTree/paths.mjs +16 -0
  71. package/dist/esm/runtime/routeTree/staticData.mjs +22 -0
  72. package/dist/esm/runtime/routeTree/tanstackRoutes.mjs +13 -0
  73. package/dist/esm/runtime/routeTree/types.mjs +0 -0
  74. package/dist/esm/runtime/routeTree.mjs +1 -318
  75. package/dist/esm/runtime/router.mjs +16 -2
  76. package/dist/esm/runtime/rsc/CompositeComponent.mjs +18 -13
  77. package/dist/esm/runtime/rsc/ReplayableStream.mjs +1 -1
  78. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +4 -15
  79. package/dist/esm/runtime/rsc/client.mjs +6 -26
  80. package/dist/esm/runtime/rsc/flightSerialization.mjs +88 -0
  81. package/dist/esm/runtime/rsc/payloadErrors.mjs +49 -0
  82. package/dist/esm/runtime/rsc/payloadFetch.mjs +55 -0
  83. package/dist/esm/runtime/rsc/payloadRouter.mjs +12 -133
  84. package/dist/esm/runtime/rsc/payloadRoutes.mjs +50 -0
  85. package/dist/esm/runtime/rsc/payloadStorage.mjs +14 -0
  86. package/dist/esm/runtime/rsc/server.mjs +17 -21
  87. package/dist/esm/runtime/rsc/shared.mjs +21 -0
  88. package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +1 -0
  89. package/dist/esm/runtime/rsc/treeGetter.mjs +34 -0
  90. package/dist/esm/runtime/shared/isAbsoluteUrl.mjs +9 -0
  91. package/dist/esm/runtime/ssrManagedTags.mjs +53 -0
  92. package/dist/esm/runtime/ssrPreload.mjs +51 -0
  93. package/dist/esm/runtime/ssrTypes.mjs +0 -0
  94. package/dist/esm/runtime/state.mjs +1 -1
  95. package/dist/esm/runtime/submitAction.mjs +120 -0
  96. package/dist/esm/runtime/utils.mjs +47 -75
  97. package/dist/esm-node/cli/artifacts.mjs +104 -0
  98. package/dist/esm-node/cli/index.mjs +8 -82
  99. package/dist/esm-node/cli/tanstackTypes/canonicalRoutes.mjs +76 -0
  100. package/dist/esm-node/cli/tanstackTypes/shared.mjs +62 -0
  101. package/dist/esm-node/cli/tanstackTypes/source.mjs +208 -0
  102. package/dist/esm-node/cli/tanstackTypes.mjs +2 -339
  103. package/dist/esm-node/runtime/blockingSubscribe.mjs +20 -0
  104. package/dist/esm-node/runtime/clientBasepath.mjs +11 -0
  105. package/dist/esm-node/runtime/clientHydration.mjs +72 -0
  106. package/dist/esm-node/runtime/dataMutation.mjs +2 -167
  107. package/dist/esm-node/runtime/formData.mjs +64 -0
  108. package/dist/esm-node/runtime/loaderBridge.mjs +5 -9
  109. package/dist/esm-node/runtime/plugin.mjs +14 -118
  110. package/dist/esm-node/runtime/plugin.node.mjs +41 -121
  111. package/dist/esm-node/runtime/pluginShared.mjs +41 -0
  112. package/dist/esm-node/runtime/prefetchLink.mjs +18 -5
  113. package/dist/esm-node/runtime/register.mjs +1 -14
  114. package/dist/esm-node/runtime/routeTree/components.mjs +56 -0
  115. package/dist/esm-node/runtime/routeTree/construction.mjs +112 -0
  116. package/dist/esm-node/runtime/routeTree/index.mjs +3 -0
  117. package/dist/esm-node/runtime/routeTree/loaders.mjs +118 -0
  118. package/dist/esm-node/runtime/routeTree/paths.mjs +17 -0
  119. package/dist/esm-node/runtime/routeTree/staticData.mjs +23 -0
  120. package/dist/esm-node/runtime/routeTree/tanstackRoutes.mjs +14 -0
  121. package/dist/esm-node/runtime/routeTree/types.mjs +1 -0
  122. package/dist/esm-node/runtime/routeTree.mjs +1 -318
  123. package/dist/esm-node/runtime/router.mjs +16 -2
  124. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +18 -13
  125. package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +1 -1
  126. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +4 -15
  127. package/dist/esm-node/runtime/rsc/client.mjs +6 -26
  128. package/dist/esm-node/runtime/rsc/flightSerialization.mjs +89 -0
  129. package/dist/esm-node/runtime/rsc/payloadErrors.mjs +50 -0
  130. package/dist/esm-node/runtime/rsc/payloadFetch.mjs +56 -0
  131. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +12 -133
  132. package/dist/esm-node/runtime/rsc/payloadRoutes.mjs +51 -0
  133. package/dist/esm-node/runtime/rsc/payloadStorage.mjs +15 -0
  134. package/dist/esm-node/runtime/rsc/server.mjs +17 -21
  135. package/dist/esm-node/runtime/rsc/shared.mjs +22 -0
  136. package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +1 -0
  137. package/dist/esm-node/runtime/rsc/treeGetter.mjs +35 -0
  138. package/dist/esm-node/runtime/shared/isAbsoluteUrl.mjs +10 -0
  139. package/dist/esm-node/runtime/ssrManagedTags.mjs +54 -0
  140. package/dist/esm-node/runtime/ssrPreload.mjs +52 -0
  141. package/dist/esm-node/runtime/ssrTypes.mjs +1 -0
  142. package/dist/esm-node/runtime/state.mjs +1 -1
  143. package/dist/esm-node/runtime/submitAction.mjs +121 -0
  144. package/dist/esm-node/runtime/utils.mjs +47 -75
  145. package/dist/types/cli/artifacts.d.ts +33 -0
  146. package/dist/types/cli/index.d.ts +2 -22
  147. package/dist/types/cli/tanstackTypes/canonicalRoutes.d.ts +22 -0
  148. package/dist/types/cli/tanstackTypes/shared.d.ts +20 -0
  149. package/dist/types/cli/tanstackTypes/source.d.ts +12 -0
  150. package/dist/types/cli/tanstackTypes.d.ts +3 -31
  151. package/dist/types/runtime/blockingSubscribe.d.ts +1 -0
  152. package/dist/types/runtime/clientBasepath.d.ts +1 -0
  153. package/dist/types/runtime/clientHydration.d.ts +10 -0
  154. package/dist/types/runtime/dataMutation.d.ts +3 -12
  155. package/dist/types/runtime/formData.d.ts +24 -0
  156. package/dist/types/runtime/loaderBridge.d.ts +2 -1
  157. package/dist/types/runtime/plugin.d.ts +1 -1
  158. package/dist/types/runtime/plugin.node.d.ts +1 -1
  159. package/dist/types/runtime/pluginShared.d.ts +38 -0
  160. package/dist/types/runtime/register.d.ts +10 -9
  161. package/dist/types/runtime/routeTree/components.d.ts +7 -0
  162. package/dist/types/runtime/routeTree/construction.d.ts +5 -0
  163. package/dist/types/runtime/routeTree/index.d.ts +2 -0
  164. package/dist/types/runtime/routeTree/loaders.d.ts +4 -0
  165. package/dist/types/runtime/routeTree/paths.d.ts +3 -0
  166. package/dist/types/runtime/routeTree/staticData.d.ts +19 -0
  167. package/dist/types/runtime/routeTree/tanstackRoutes.d.ts +4 -0
  168. package/dist/types/runtime/routeTree/types.d.ts +76 -0
  169. package/dist/types/runtime/routeTree.d.ts +1 -9
  170. package/dist/types/runtime/router.d.ts +6 -7
  171. package/dist/types/runtime/rsc/CompositeComponent.d.ts +1 -2
  172. package/dist/types/runtime/rsc/ReplayableStream.d.ts +3 -2
  173. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +2 -3
  174. package/dist/types/runtime/rsc/SlotContext.d.ts +2 -1
  175. package/dist/types/runtime/rsc/client.d.ts +2 -0
  176. package/dist/types/runtime/rsc/flightSerialization.d.ts +3 -0
  177. package/dist/types/runtime/rsc/payloadErrors.d.ts +6 -0
  178. package/dist/types/runtime/rsc/payloadFetch.d.ts +8 -0
  179. package/dist/types/runtime/rsc/payloadRouter.d.ts +2 -10
  180. package/dist/types/runtime/rsc/payloadRoutes.d.ts +40 -0
  181. package/dist/types/runtime/rsc/payloadStorage.d.ts +5 -0
  182. package/dist/types/runtime/rsc/shared.d.ts +18 -0
  183. package/dist/types/runtime/rsc/treeGetter.d.ts +1 -0
  184. package/dist/types/runtime/shared/isAbsoluteUrl.d.ts +1 -0
  185. package/dist/types/runtime/ssrManagedTags.d.ts +7 -0
  186. package/dist/types/runtime/ssrPreload.d.ts +2 -0
  187. package/dist/types/runtime/ssrTypes.d.ts +44 -0
  188. package/dist/types/runtime/state.d.ts +1 -1
  189. package/dist/types/runtime/submitAction.d.ts +14 -0
  190. package/dist/types/runtime/types.d.ts +4 -10
  191. package/dist/types/runtime/utils.d.ts +4 -24
  192. package/package.json +37 -17
  193. package/rstest.config.mts +23 -0
  194. package/src/cli/artifacts.ts +255 -0
  195. package/src/cli/index.ts +7 -185
  196. package/src/cli/tanstackTypes/canonicalRoutes.ts +157 -0
  197. package/src/cli/tanstackTypes/shared.ts +113 -0
  198. package/src/cli/tanstackTypes/source.ts +414 -0
  199. package/src/cli/tanstackTypes.ts +4 -647
  200. package/src/runtime/blockingSubscribe.ts +48 -0
  201. package/src/runtime/clientBasepath.ts +12 -0
  202. package/src/runtime/clientHydration.ts +149 -0
  203. package/src/runtime/dataMutation.tsx +9 -306
  204. package/src/runtime/formData.ts +153 -0
  205. package/src/runtime/loaderBridge.ts +11 -12
  206. package/src/runtime/plugin.node.tsx +76 -291
  207. package/src/runtime/plugin.tsx +21 -257
  208. package/src/runtime/pluginShared.ts +112 -0
  209. package/src/runtime/prefetchLink.tsx +78 -15
  210. package/src/runtime/register.ts +10 -42
  211. package/src/runtime/routeTree/components.ts +130 -0
  212. package/src/runtime/routeTree/construction.ts +238 -0
  213. package/src/runtime/routeTree/index.ts +7 -0
  214. package/src/runtime/routeTree/loaders.ts +232 -0
  215. package/src/runtime/routeTree/paths.ts +36 -0
  216. package/src/runtime/routeTree/staticData.ts +66 -0
  217. package/src/runtime/routeTree/tanstackRoutes.ts +36 -0
  218. package/src/runtime/routeTree/types.ts +87 -0
  219. package/src/runtime/routeTree.ts +5 -750
  220. package/src/runtime/router.ts +35 -7
  221. package/src/runtime/rsc/CompositeComponent.tsx +29 -20
  222. package/src/runtime/rsc/ReplayableStream.ts +2 -2
  223. package/src/runtime/rsc/RscNodeRenderer.tsx +9 -27
  224. package/src/runtime/rsc/SlotContext.tsx +1 -1
  225. package/src/runtime/rsc/client.tsx +12 -40
  226. package/src/runtime/rsc/flightSerialization.ts +158 -0
  227. package/src/runtime/rsc/payloadErrors.ts +73 -0
  228. package/src/runtime/rsc/payloadFetch.ts +92 -0
  229. package/src/runtime/rsc/payloadRouter.ts +29 -234
  230. package/src/runtime/rsc/payloadRoutes.ts +122 -0
  231. package/src/runtime/rsc/payloadStorage.ts +35 -0
  232. package/src/runtime/rsc/server.tsx +20 -29
  233. package/src/runtime/rsc/shared.ts +70 -0
  234. package/src/runtime/rsc/slotUsageSanitizer.ts +4 -0
  235. package/src/runtime/rsc/treeGetter.tsx +52 -0
  236. package/src/runtime/shared/isAbsoluteUrl.ts +8 -0
  237. package/src/runtime/ssrManagedTags.ts +107 -0
  238. package/src/runtime/ssrPreload.ts +125 -0
  239. package/src/runtime/ssrTypes.ts +51 -0
  240. package/src/runtime/state.ts +9 -3
  241. package/src/runtime/submitAction.ts +201 -0
  242. package/src/runtime/types.ts +4 -18
  243. package/src/runtime/utils.tsx +76 -110
  244. package/src/types/react-server-dom-rspack.d.ts +56 -0
  245. package/tests/castRatchet.test.ts +85 -0
  246. package/tests/realm-isolation.test.ts +114 -0
  247. package/tests/router/cli.test.ts +7 -0
  248. package/tests/router/clientHydration.test.tsx +61 -0
  249. package/tests/router/dataMutation.test.tsx +59 -0
  250. package/tests/router/fixtures/legacyRuntimeRouter.ts +10 -0
  251. package/tests/router/fixtures/router-gen.golden.txt +71 -0
  252. package/tests/router/fixtures/router-gen.oxlint.json +5 -0
  253. package/tests/router/flightSerialization.roundtrip.test.tsx +137 -0
  254. package/tests/router/generateRouteArtifacts.test.ts +106 -0
  255. package/tests/router/hooks.test.ts +14 -0
  256. package/tests/router/loaderBridge.test.ts +21 -0
  257. package/tests/router/prefetchLink.test.tsx +106 -59
  258. package/tests/router/prefetchLinkPreload.test.tsx +85 -0
  259. package/tests/router/register.test.ts +1 -39
  260. package/tests/router/routeTree.test.ts +51 -11
  261. package/tests/router/routerCompatibility.test.ts +245 -0
  262. package/tests/router/rsc.test.tsx +189 -0
  263. package/tests/router/rscPayloadRouterMatrix.test.ts +197 -0
  264. package/tests/router/slotUsageSanitizer.test.ts +23 -0
  265. package/tests/router/ssrPreload.test.ts +30 -0
  266. package/tests/router/tanstackTypes.test.ts +298 -1
  267. package/dist/esm/runtime/pluginCore.mjs +0 -14
  268. package/dist/esm-node/runtime/pluginCore.mjs +0 -15
  269. package/dist/types/runtime/pluginCore.d.ts +0 -21
  270. package/src/runtime/pluginCore.ts +0 -48
@@ -0,0 +1,130 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ import type { RouteObject } from '@modern-js/runtime-utils/router';
4
+ import { createElement, type ElementType } from 'react';
5
+
6
+ import type { ModernRouteObject, RouteTreeOptions } from './types';
7
+
8
+ type ModernRouteModule = {
9
+ Component?: unknown;
10
+ default?: unknown;
11
+ };
12
+ type PreloadableComponent = {
13
+ (props: Record<string, unknown>): ReturnType<typeof createElement>;
14
+ load?: () => Promise<unknown>;
15
+ preload?: () => Promise<unknown>;
16
+ };
17
+
18
+ export function pickRouteModuleComponent(
19
+ routeModule: unknown,
20
+ seen: Set<unknown> = new Set(),
21
+ ): ElementType<Record<string, unknown>> | undefined {
22
+ if (
23
+ typeof routeModule === 'function' ||
24
+ (routeModule &&
25
+ typeof routeModule === 'object' &&
26
+ '$$typeof' in routeModule)
27
+ ) {
28
+ return routeModule as ElementType<Record<string, unknown>>;
29
+ }
30
+
31
+ if (!routeModule || typeof routeModule !== 'object') {
32
+ return undefined;
33
+ }
34
+ if (seen.has(routeModule)) {
35
+ return undefined;
36
+ }
37
+ seen.add(routeModule);
38
+
39
+ const module = routeModule as ModernRouteModule;
40
+ for (const candidate of [module.default, module.Component]) {
41
+ const component = pickRouteModuleComponent(candidate, seen);
42
+ if (component) {
43
+ return component;
44
+ }
45
+ }
46
+
47
+ return undefined;
48
+ }
49
+
50
+ function createServerLazyImportComponent(
51
+ lazyImport: () => unknown,
52
+ fallbackComponent?: unknown,
53
+ ): PreloadableComponent | unknown {
54
+ if (typeof document !== 'undefined') {
55
+ return fallbackComponent;
56
+ }
57
+
58
+ let resolvedComponent: ElementType<Record<string, unknown>> | undefined;
59
+ let pendingLoad: Promise<unknown> | undefined;
60
+
61
+ const load = async () => {
62
+ if (resolvedComponent) {
63
+ return resolvedComponent;
64
+ }
65
+
66
+ const routeModule = await lazyImport();
67
+ const component = pickRouteModuleComponent(routeModule);
68
+ if (component) {
69
+ resolvedComponent = component;
70
+ }
71
+ return resolvedComponent;
72
+ };
73
+
74
+ const Component: PreloadableComponent = props => {
75
+ if (resolvedComponent) {
76
+ return createElement(resolvedComponent, props);
77
+ }
78
+
79
+ pendingLoad ||= load();
80
+ throw pendingLoad;
81
+ };
82
+ Component.load = load;
83
+ Component.preload = load;
84
+
85
+ return Component;
86
+ }
87
+
88
+ export function toRouteComponent(
89
+ routeObject: RouteObject,
90
+ options: RouteTreeOptions = {},
91
+ ): unknown {
92
+ const route = routeObject as ModernRouteObject;
93
+ const lazyImport =
94
+ typeof route.lazyImport === 'function' ? route.lazyImport : undefined;
95
+ const routeComponent = route.Component || route.component;
96
+ const fallbackComponent = routeComponent
97
+ ? routeComponent
98
+ : route.element
99
+ ? () => route.element
100
+ : undefined;
101
+
102
+ if (lazyImport && fallbackComponent) {
103
+ return createServerLazyImportComponent(lazyImport, fallbackComponent);
104
+ }
105
+
106
+ if (routeComponent) {
107
+ return routeComponent;
108
+ }
109
+ const element = route.element;
110
+ if (element) {
111
+ return () => element;
112
+ }
113
+ return undefined;
114
+ }
115
+
116
+ export function toErrorComponent(routeObject: RouteObject): unknown {
117
+ const route = routeObject as ModernRouteObject;
118
+ if (route.ErrorBoundary) {
119
+ return route.ErrorBoundary;
120
+ }
121
+ if (route.errorElement) {
122
+ return () => route.errorElement;
123
+ }
124
+ return undefined;
125
+ }
126
+
127
+ export function toPendingComponent(routeObject: RouteObject): unknown {
128
+ const route = routeObject as ModernRouteObject;
129
+ return route.HydrateFallback || route.pendingComponent || undefined;
130
+ }
@@ -0,0 +1,238 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ import { DefaultNotFound } from '@modern-js/runtime/context';
4
+ import type { RouteObject } from '@modern-js/runtime-utils/router';
5
+ import type { AnyRoute, AnyRouter } from '@tanstack/react-router';
6
+ import { rootRouteId } from '@tanstack/react-router';
7
+
8
+ import { withModernRouteMatchContext } from '../outlet';
9
+ import {
10
+ toErrorComponent,
11
+ toPendingComponent,
12
+ toRouteComponent,
13
+ } from './components';
14
+ import { createModernShouldReload, wrapRouteObjectLoader } from './loaders';
15
+ import { isRouteObjectPathlessLayout, toTanstackPath } from './paths';
16
+ import { createRouteStaticData, mergeModernRouteHandle } from './staticData';
17
+ import {
18
+ createTanstackRootRoute,
19
+ createTanstackRoute,
20
+ wrapRouteComponentWithModernContext,
21
+ } from './tanstackRoutes';
22
+ import type {
23
+ ModernRouteObject,
24
+ ModernTanstackRootRoute,
25
+ MutableTanstackRoute,
26
+ RouteRevalidationState,
27
+ RouteTreeOptions,
28
+ TanstackRootRouteOptions,
29
+ TanstackRouteOptions,
30
+ } from './types';
31
+
32
+ function createRouteFromRouteObject(opts: {
33
+ options?: RouteTreeOptions;
34
+ parent: AnyRoute;
35
+ pathlessFallbackId?: string;
36
+ routeObject: RouteObject;
37
+ }): AnyRoute {
38
+ const {
39
+ options = {},
40
+ parent,
41
+ pathlessFallbackId = 'pathless',
42
+ routeObject,
43
+ } = opts;
44
+ const modernRouteObject = routeObject as ModernRouteObject;
45
+ const revalidationState: RouteRevalidationState = {};
46
+ const shouldRevalidate = modernRouteObject.shouldRevalidate;
47
+ const shouldReload = createModernShouldReload(
48
+ shouldRevalidate,
49
+ revalidationState,
50
+ );
51
+
52
+ const stableFallbackId =
53
+ routeObject.id || modernRouteObject.file || pathlessFallbackId;
54
+
55
+ const component = toRouteComponent(routeObject, options);
56
+ const base: TanstackRouteOptions = {
57
+ getParentRoute: () => parent,
58
+ component,
59
+ pendingComponent: toPendingComponent(routeObject),
60
+ errorComponent: toErrorComponent(routeObject),
61
+ validateSearch: modernRouteObject.validateSearch,
62
+ loaderDeps: modernRouteObject.loaderDeps,
63
+ staticData: createRouteStaticData({
64
+ modernRouteId: routeObject.id,
65
+ modernRouteAction: modernRouteObject.action,
66
+ modernRouteHandle: mergeModernRouteHandle(modernRouteObject),
67
+ modernRouteHasAction:
68
+ modernRouteObject.hasAction || Boolean(modernRouteObject.action),
69
+ modernRouteHasClientLoader:
70
+ modernRouteObject.hasClientLoader ||
71
+ typeof modernRouteObject.clientData !== 'undefined',
72
+ modernRouteHasLoader:
73
+ modernRouteObject.hasLoader ||
74
+ typeof modernRouteObject.loader === 'function',
75
+ modernRouteIsClientComponent: modernRouteObject.isClientComponent,
76
+ modernRouteLoader: modernRouteObject.loader,
77
+ modernRouteShouldRevalidate: shouldRevalidate,
78
+ }),
79
+ loader: wrapRouteObjectLoader(routeObject, revalidationState, options),
80
+ };
81
+ if (modernRouteObject.inValidSSRRoute) {
82
+ base.ssr = false;
83
+ }
84
+ if (shouldReload) {
85
+ base.shouldReload = shouldReload;
86
+ }
87
+
88
+ if (isRouteObjectPathlessLayout(routeObject)) {
89
+ base.id = stableFallbackId;
90
+ } else {
91
+ base.path = routeObject.index
92
+ ? '/'
93
+ : toTanstackPath((routeObject.path as string) || '');
94
+ }
95
+
96
+ const route = createTanstackRoute(base);
97
+ wrapRouteComponentWithModernContext(route, component, routeObject.id);
98
+
99
+ const children = routeObject.children;
100
+ if (children && children.length > 0) {
101
+ const childRoutes = children.map((child: RouteObject, index) =>
102
+ createRouteFromRouteObject({
103
+ options,
104
+ parent: route,
105
+ pathlessFallbackId: `pathless-${index}`,
106
+ routeObject: child,
107
+ }),
108
+ );
109
+ route.addChildren(childRoutes);
110
+ }
111
+
112
+ return route;
113
+ }
114
+
115
+ function getRootLikeRouteObject(routes: RouteObject[]) {
116
+ return routes.find(route => route.path === '/' && !route.index);
117
+ }
118
+
119
+ export function createRouteTreeFromRouteObjects(
120
+ routes: RouteObject[],
121
+ options: RouteTreeOptions = {},
122
+ ): ModernTanstackRootRoute {
123
+ const rootLikeRoute = getRootLikeRouteObject(routes) as
124
+ | ModernRouteObject
125
+ | undefined;
126
+ const rootRevalidationState: RouteRevalidationState = {};
127
+ const rootShouldRevalidate = rootLikeRoute?.shouldRevalidate;
128
+ const rootShouldReload = createModernShouldReload(
129
+ rootShouldRevalidate,
130
+ rootRevalidationState,
131
+ );
132
+
133
+ const rootComponent = rootLikeRoute
134
+ ? toRouteComponent(rootLikeRoute)
135
+ : undefined;
136
+ const rootRouteOptions: TanstackRootRouteOptions = {
137
+ component: rootComponent,
138
+ pendingComponent: rootLikeRoute
139
+ ? toPendingComponent(rootLikeRoute)
140
+ : undefined,
141
+ errorComponent: rootLikeRoute ? toErrorComponent(rootLikeRoute) : undefined,
142
+ validateSearch: rootLikeRoute?.validateSearch,
143
+ loaderDeps: rootLikeRoute?.loaderDeps,
144
+ notFoundComponent: DefaultNotFound,
145
+ staticData: createRouteStaticData({
146
+ modernRouteId: rootLikeRoute?.id,
147
+ modernRouteAction: rootLikeRoute?.action,
148
+ modernRouteHandle: rootLikeRoute
149
+ ? mergeModernRouteHandle(rootLikeRoute)
150
+ : undefined,
151
+ modernRouteHasAction:
152
+ rootLikeRoute?.hasAction || Boolean(rootLikeRoute?.action),
153
+ modernRouteHasClientLoader:
154
+ rootLikeRoute?.hasClientLoader ||
155
+ typeof rootLikeRoute?.clientData !== 'undefined',
156
+ modernRouteHasLoader:
157
+ rootLikeRoute?.hasLoader || typeof rootLikeRoute?.loader === 'function',
158
+ modernRouteIsClientComponent: rootLikeRoute?.isClientComponent,
159
+ modernRouteLoader: rootLikeRoute?.loader,
160
+ modernRouteShouldRevalidate: rootShouldRevalidate,
161
+ }),
162
+ loader: rootLikeRoute
163
+ ? wrapRouteObjectLoader(rootLikeRoute, rootRevalidationState, options)
164
+ : undefined,
165
+ };
166
+ if (rootShouldReload) {
167
+ rootRouteOptions.shouldReload = rootShouldReload;
168
+ }
169
+ if (rootLikeRoute?.inValidSSRRoute) {
170
+ rootRouteOptions.ssr = false;
171
+ }
172
+
173
+ const rootRoute = createTanstackRootRoute(rootRouteOptions);
174
+ if (rootComponent) {
175
+ rootRoute.options.component = withModernRouteMatchContext(
176
+ rootComponent,
177
+ rootRouteId,
178
+ ) as typeof rootRoute.options.component;
179
+ }
180
+
181
+ const topLevel = rootLikeRoute
182
+ ? [
183
+ ...((rootLikeRoute.children as RouteObject[] | undefined) || []),
184
+ ...routes.filter(route => route !== rootLikeRoute),
185
+ ]
186
+ : routes;
187
+
188
+ const childRoutes = topLevel.map((routeObject, index) =>
189
+ createRouteFromRouteObject({
190
+ options,
191
+ parent: rootRoute,
192
+ pathlessFallbackId: `pathless-${index}`,
193
+ routeObject,
194
+ }),
195
+ );
196
+
197
+ rootRoute.addChildren(childRoutes);
198
+ return rootRoute as unknown as ModernTanstackRootRoute;
199
+ }
200
+
201
+ export function getModernRouteIdsFromMatches(router: AnyRouter): string[] {
202
+ const matches = router.state.matches || [];
203
+ const routesById = (
204
+ router as AnyRouter & {
205
+ routesById?: Record<
206
+ string,
207
+ {
208
+ options?: {
209
+ staticData?: { modernRouteId?: unknown };
210
+ };
211
+ }
212
+ >;
213
+ }
214
+ ).routesById;
215
+ const ids = matches
216
+ .map(match => {
217
+ const normalizedMatch = match as {
218
+ route?: {
219
+ options?: {
220
+ staticData?: { modernRouteId?: unknown };
221
+ };
222
+ };
223
+ routeId?: unknown;
224
+ };
225
+ const routeId =
226
+ typeof normalizedMatch.routeId === 'string'
227
+ ? normalizedMatch.routeId
228
+ : undefined;
229
+ return (
230
+ normalizedMatch.route?.options?.staticData?.modernRouteId ??
231
+ (routeId
232
+ ? routesById?.[routeId]?.options?.staticData?.modernRouteId
233
+ : undefined)
234
+ );
235
+ })
236
+ .filter((id): id is string => typeof id === 'string');
237
+ return Array.from(new Set(ids));
238
+ }
@@ -0,0 +1,7 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ export { pickRouteModuleComponent } from './components';
4
+ export {
5
+ createRouteTreeFromRouteObjects,
6
+ getModernRouteIdsFromMatches,
7
+ } from './construction';
@@ -0,0 +1,232 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ import type { RouteObject } from '@modern-js/runtime-utils/router';
4
+ import { notFound } from '@tanstack/react-router';
5
+
6
+ import {
7
+ isRedirectResponse,
8
+ isResponse,
9
+ isTanstackRedirect,
10
+ throwTanstackRedirect,
11
+ } from '../loaderBridge';
12
+ import {
13
+ isTanstackRscPayloadNavigationEnabled,
14
+ loadTanstackRscRouteData,
15
+ } from '../rsc/payloadRouter';
16
+ import type {
17
+ ModernRouteObject,
18
+ ModernShouldRevalidate,
19
+ RouteParams,
20
+ RouteRevalidationState,
21
+ RouteTreeOptions,
22
+ TanstackLoaderContext,
23
+ } from './types';
24
+
25
+ type ModernDeferredDataLike = {
26
+ __modern_deferred?: unknown;
27
+ data?: unknown;
28
+ };
29
+
30
+ function isModernDeferredData(
31
+ value: unknown,
32
+ ): value is ModernDeferredDataLike & { data: Record<string, unknown> } {
33
+ if (!value || typeof value !== 'object') {
34
+ return false;
35
+ }
36
+
37
+ const deferred = value as ModernDeferredDataLike;
38
+ return (
39
+ deferred.__modern_deferred === true &&
40
+ Boolean(deferred.data) &&
41
+ typeof deferred.data === 'object' &&
42
+ !Array.isArray(deferred.data)
43
+ );
44
+ }
45
+
46
+ function normalizeModernLoaderResult(result: unknown): unknown {
47
+ return isModernDeferredData(result) ? result.data : result;
48
+ }
49
+
50
+ function normalizeModernLoaderResponse(result: unknown): unknown {
51
+ if (isResponse(result)) {
52
+ if (isRedirectResponse(result)) {
53
+ const location = result.headers.get('Location') || '/';
54
+ throwTanstackRedirect(location);
55
+ }
56
+ if (result.status === 404) {
57
+ throw notFound();
58
+ }
59
+ }
60
+
61
+ return normalizeModernLoaderResult(result);
62
+ }
63
+
64
+ function createModernRequest(input: string, signal: AbortSignal) {
65
+ return new Request(input, { signal });
66
+ }
67
+
68
+ function getModernUrlFromLoaderContext(ctx: TanstackLoaderContext): URL {
69
+ const href =
70
+ typeof ctx?.location === 'string'
71
+ ? ctx.location
72
+ : ctx?.location?.publicHref ||
73
+ ctx?.location?.href ||
74
+ ctx?.location?.url?.href ||
75
+ '/';
76
+ const baseRequest: Request | undefined =
77
+ ctx?.context?.request instanceof Request ? ctx.context.request : undefined;
78
+ const baseUrl =
79
+ baseRequest?.url ||
80
+ (typeof window !== 'undefined' ? window.origin : 'http://localhost');
81
+
82
+ return new URL(href || '/', baseUrl);
83
+ }
84
+
85
+ function rememberRouteLocation(
86
+ state: RouteRevalidationState,
87
+ ctx: TanstackLoaderContext,
88
+ ) {
89
+ state.currentUrl = getModernUrlFromLoaderContext(ctx);
90
+ state.currentParams = ctx?.params || {};
91
+ }
92
+
93
+ export function createModernShouldReload(
94
+ shouldRevalidate: unknown,
95
+ state: RouteRevalidationState,
96
+ ) {
97
+ if (typeof shouldRevalidate !== 'function') {
98
+ return undefined;
99
+ }
100
+ const revalidate = shouldRevalidate as ModernShouldRevalidate;
101
+
102
+ return (ctx: TanstackLoaderContext) => {
103
+ const nextUrl = getModernUrlFromLoaderContext(ctx);
104
+ const nextParams = ctx?.params || {};
105
+ const result = revalidate({
106
+ currentUrl: state.currentUrl || nextUrl,
107
+ currentParams: state.currentParams || nextParams,
108
+ nextUrl,
109
+ nextParams,
110
+ // Returning undefined keeps TanStack's native stale-reload default.
111
+ defaultShouldRevalidate: undefined,
112
+ });
113
+
114
+ state.currentUrl = nextUrl;
115
+ state.currentParams = nextParams;
116
+
117
+ return typeof result === 'boolean' ? result : undefined;
118
+ };
119
+ }
120
+
121
+ function isRouteObjectSplatRoute(route: RouteObject) {
122
+ return typeof route.path === 'string' && route.path.includes('*');
123
+ }
124
+
125
+ function mapParamsForRouteObjectLoader({
126
+ route,
127
+ params,
128
+ }: {
129
+ route: RouteObject;
130
+ params: RouteParams;
131
+ }) {
132
+ if (isRouteObjectSplatRoute(route)) {
133
+ const { _splat, ...rest } = params as RouteParams & {
134
+ _splat?: string;
135
+ };
136
+ if (typeof _splat !== 'undefined') {
137
+ return { ...rest, '*': _splat };
138
+ }
139
+ return rest;
140
+ }
141
+ return params;
142
+ }
143
+
144
+ export function wrapRouteObjectLoader(
145
+ route: RouteObject,
146
+ revalidationState?: RouteRevalidationState,
147
+ options: RouteTreeOptions = {},
148
+ ) {
149
+ const modernRoute = route as ModernRouteObject;
150
+ const routeLoader = modernRoute.loader;
151
+ if (typeof routeLoader !== 'function') {
152
+ return undefined;
153
+ }
154
+
155
+ return async (ctx: TanstackLoaderContext) => {
156
+ try {
157
+ if (revalidationState) {
158
+ rememberRouteLocation(revalidationState, ctx);
159
+ }
160
+
161
+ if (typeof modernRoute.lazyImport === 'function') {
162
+ try {
163
+ await modernRoute.lazyImport();
164
+ } catch {}
165
+ }
166
+
167
+ const signal: AbortSignal =
168
+ ctx?.abortController?.signal ||
169
+ ctx?.signal ||
170
+ new AbortController().signal;
171
+ const baseRequest: Request | undefined =
172
+ ctx?.context?.request instanceof Request
173
+ ? ctx.context.request
174
+ : undefined;
175
+
176
+ const href =
177
+ typeof ctx?.location === 'string'
178
+ ? ctx.location
179
+ : ctx?.location?.publicHref ||
180
+ ctx?.location?.href ||
181
+ ctx?.location?.url?.href ||
182
+ '';
183
+
184
+ const request =
185
+ baseRequest !== undefined
186
+ ? new Request(baseRequest, { signal })
187
+ : createModernRequest(href, signal);
188
+
189
+ const params = mapParamsForRouteObjectLoader({
190
+ route,
191
+ params: ctx.params || {},
192
+ });
193
+
194
+ const loadModernData = async () => {
195
+ const result = await routeLoader({
196
+ request,
197
+ params,
198
+ context: ctx?.context?.requestContext,
199
+ });
200
+
201
+ return normalizeModernLoaderResponse(result);
202
+ };
203
+
204
+ if (options.rscPayloadRouter && isTanstackRscPayloadNavigationEnabled()) {
205
+ return loadTanstackRscRouteData({
206
+ hasClientLoader:
207
+ modernRoute.hasClientLoader ||
208
+ typeof modernRoute.clientData !== 'undefined',
209
+ loadClientData: loadModernData,
210
+ request,
211
+ routeId: ctx.route?.id,
212
+ });
213
+ }
214
+
215
+ return loadModernData();
216
+ } catch (err) {
217
+ if (isResponse(err)) {
218
+ if (isTanstackRedirect(err)) {
219
+ throw err;
220
+ }
221
+ if (isRedirectResponse(err)) {
222
+ const location = err.headers.get('Location') || '/';
223
+ throwTanstackRedirect(location);
224
+ }
225
+ if (err.status === 404) {
226
+ throw notFound();
227
+ }
228
+ }
229
+ throw err;
230
+ }
231
+ };
232
+ }
@@ -0,0 +1,36 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ import type { RouteObject } from '@modern-js/runtime-utils/router';
4
+
5
+ export function toTanstackPath(pathname: string): string {
6
+ // TanStack Router uses `$param` and `$` (splat) style params.
7
+ // Modern's conventional routing currently generates React Router style params (e.g. `:id`, `*`).
8
+ //
9
+ // We only convert the subset Modern generates today:
10
+ // - `:id` -> `$id`
11
+ // - `:id?` -> `{-$id}` (optional param)
12
+ // - `*` -> `$`
13
+ return pathname
14
+ .split('/')
15
+ .map(segment => {
16
+ if (!segment) {
17
+ return segment;
18
+ }
19
+ if (segment === '*') {
20
+ return '$';
21
+ }
22
+ if (segment.startsWith(':')) {
23
+ const name = segment.slice(1);
24
+ if (name.endsWith('?')) {
25
+ return `{-$${name.slice(0, -1)}}`;
26
+ }
27
+ return `$${name}`;
28
+ }
29
+ return segment;
30
+ })
31
+ .join('/');
32
+ }
33
+
34
+ export function isRouteObjectPathlessLayout(route: RouteObject) {
35
+ return !route.path && !route.index;
36
+ }
@@ -0,0 +1,66 @@
1
+ // @effect-diagnostics asyncFunction:off strictBooleanExpressions:off
2
+
3
+ export function mergeModernRouteHandle(route: {
4
+ config?: { handle?: Record<string, unknown> } | unknown;
5
+ handle?: Record<string, unknown>;
6
+ }) {
7
+ const config = route.config as { handle?: Record<string, unknown> } | null;
8
+ const handle = {
9
+ ...route.handle,
10
+ ...(config && typeof config === 'object' ? config.handle : {}),
11
+ };
12
+
13
+ return Object.keys(handle).length > 0 ? handle : undefined;
14
+ }
15
+
16
+ export function createRouteStaticData(opts: {
17
+ modernRouteId?: string;
18
+ modernRouteAction?: unknown;
19
+ modernRouteHandle?: unknown;
20
+ modernRouteHasAction?: boolean;
21
+ modernRouteHasClientLoader?: boolean;
22
+ modernRouteHasLoader?: boolean;
23
+ modernRouteIsClientComponent?: boolean;
24
+ modernRouteLoader?: unknown;
25
+ modernRouteShouldRevalidate?: unknown;
26
+ }) {
27
+ const staticData: Record<string, unknown> = {};
28
+
29
+ if (opts.modernRouteId) {
30
+ staticData.modernRouteId = opts.modernRouteId;
31
+ }
32
+
33
+ if (opts.modernRouteAction) {
34
+ staticData.modernRouteAction = opts.modernRouteAction;
35
+ }
36
+
37
+ if (opts.modernRouteHandle) {
38
+ staticData.modernRouteHandle = opts.modernRouteHandle;
39
+ }
40
+
41
+ if (opts.modernRouteHasAction) {
42
+ staticData.modernRouteHasAction = true;
43
+ }
44
+
45
+ if (opts.modernRouteHasClientLoader) {
46
+ staticData.modernRouteHasClientLoader = true;
47
+ }
48
+
49
+ if (opts.modernRouteHasLoader) {
50
+ staticData.modernRouteHasLoader = true;
51
+ }
52
+
53
+ if (opts.modernRouteIsClientComponent) {
54
+ staticData.modernRouteIsClientComponent = true;
55
+ }
56
+
57
+ if (opts.modernRouteLoader) {
58
+ staticData.modernRouteLoader = opts.modernRouteLoader;
59
+ }
60
+
61
+ if (opts.modernRouteShouldRevalidate) {
62
+ staticData.modernRouteShouldRevalidate = opts.modernRouteShouldRevalidate;
63
+ }
64
+
65
+ return Object.keys(staticData).length > 0 ? staticData : undefined;
66
+ }