@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.12 → 3.2.0-ultramodern.121

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 (136) hide show
  1. package/dist/cjs/cli/index.js +89 -31
  2. package/dist/cjs/cli/routeSplitting.js +55 -0
  3. package/dist/cjs/cli/tanstackTypes.js +172 -170
  4. package/dist/cjs/cli.js +12 -8
  5. package/dist/cjs/runtime/basepathRewrite.js +12 -8
  6. package/dist/cjs/runtime/dataMutation.js +9 -5
  7. package/dist/cjs/runtime/hooks.js +20 -19
  8. package/dist/cjs/runtime/hydrationBoundary.js +48 -0
  9. package/dist/cjs/runtime/index.js +79 -35
  10. package/dist/cjs/runtime/lifecycle.js +21 -91
  11. package/dist/cjs/runtime/loaderBridge.js +173 -0
  12. package/dist/cjs/runtime/outlet.js +58 -0
  13. package/dist/cjs/runtime/plugin.js +195 -114
  14. package/dist/cjs/runtime/plugin.node.js +45 -45
  15. package/dist/cjs/runtime/plugin.worker.js +53 -0
  16. package/dist/cjs/runtime/pluginCore.js +55 -0
  17. package/dist/cjs/runtime/prefetchLink.js +10 -6
  18. package/dist/cjs/runtime/register.js +56 -0
  19. package/dist/cjs/runtime/routeTree.js +74 -207
  20. package/dist/cjs/runtime/router.js +41 -0
  21. package/dist/cjs/runtime/rsc/ClientSlot.js +9 -5
  22. package/dist/cjs/runtime/rsc/CompositeComponent.js +9 -5
  23. package/dist/cjs/runtime/rsc/ReplayableStream.js +14 -9
  24. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +9 -5
  25. package/dist/cjs/runtime/rsc/SlotContext.js +9 -5
  26. package/dist/cjs/runtime/rsc/client.js +9 -5
  27. package/dist/cjs/runtime/rsc/createRscProxy.js +9 -5
  28. package/dist/cjs/runtime/rsc/index.js +9 -5
  29. package/dist/cjs/runtime/rsc/payloadRouter.js +44 -6
  30. package/dist/cjs/runtime/rsc/server.js +9 -5
  31. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +9 -5
  32. package/dist/cjs/runtime/rsc/symbols.js +20 -15
  33. package/dist/cjs/runtime/state.js +45 -0
  34. package/dist/cjs/runtime/types.js +31 -1
  35. package/dist/cjs/runtime/utils.js +9 -10
  36. package/dist/cjs/runtime.js +9 -5
  37. package/dist/esm/cli/index.mjs +75 -27
  38. package/dist/esm/cli/routeSplitting.mjs +14 -0
  39. package/dist/esm/cli/tanstackTypes.mjs +158 -160
  40. package/dist/esm/runtime/hooks.mjs +1 -8
  41. package/dist/esm/runtime/hydrationBoundary.mjs +10 -0
  42. package/dist/esm/runtime/index.mjs +5 -2
  43. package/dist/esm/runtime/lifecycle.mjs +1 -82
  44. package/dist/esm/runtime/loaderBridge.mjs +114 -0
  45. package/dist/esm/runtime/outlet.mjs +17 -0
  46. package/dist/esm/runtime/plugin.mjs +191 -114
  47. package/dist/esm/runtime/plugin.node.mjs +40 -44
  48. package/dist/esm/runtime/plugin.worker.mjs +1 -0
  49. package/dist/esm/runtime/pluginCore.mjs +14 -0
  50. package/dist/esm/runtime/prefetchLink.mjs +1 -1
  51. package/dist/esm/runtime/register.mjs +18 -0
  52. package/dist/esm/runtime/routeTree.mjs +59 -193
  53. package/dist/esm/runtime/router.mjs +2 -0
  54. package/dist/esm/runtime/rsc/payloadRouter.mjs +35 -1
  55. package/dist/esm/runtime/state.mjs +7 -0
  56. package/dist/esm/runtime/types.mjs +7 -0
  57. package/dist/esm/runtime/utils.mjs +0 -5
  58. package/dist/esm-node/cli/index.mjs +75 -27
  59. package/dist/esm-node/cli/routeSplitting.mjs +15 -0
  60. package/dist/esm-node/cli/tanstackTypes.mjs +158 -160
  61. package/dist/esm-node/runtime/hooks.mjs +1 -8
  62. package/dist/esm-node/runtime/hydrationBoundary.mjs +11 -0
  63. package/dist/esm-node/runtime/index.mjs +5 -2
  64. package/dist/esm-node/runtime/lifecycle.mjs +1 -82
  65. package/dist/esm-node/runtime/loaderBridge.mjs +115 -0
  66. package/dist/esm-node/runtime/outlet.mjs +18 -0
  67. package/dist/esm-node/runtime/plugin.mjs +191 -114
  68. package/dist/esm-node/runtime/plugin.node.mjs +40 -44
  69. package/dist/esm-node/runtime/plugin.worker.mjs +2 -0
  70. package/dist/esm-node/runtime/pluginCore.mjs +15 -0
  71. package/dist/esm-node/runtime/prefetchLink.mjs +1 -1
  72. package/dist/esm-node/runtime/register.mjs +19 -0
  73. package/dist/esm-node/runtime/routeTree.mjs +59 -193
  74. package/dist/esm-node/runtime/router.mjs +3 -0
  75. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +35 -1
  76. package/dist/esm-node/runtime/state.mjs +8 -0
  77. package/dist/esm-node/runtime/types.mjs +7 -0
  78. package/dist/esm-node/runtime/utils.mjs +0 -5
  79. package/dist/types/cli/index.d.ts +14 -1
  80. package/dist/types/cli/routeSplitting.d.ts +20 -0
  81. package/dist/types/cli/tanstackTypes.d.ts +21 -1
  82. package/dist/types/runtime/hooks.d.ts +8 -33
  83. package/dist/types/runtime/hydrationBoundary.d.ts +2 -0
  84. package/dist/types/runtime/index.d.ts +8 -3
  85. package/dist/types/runtime/lifecycle.d.ts +7 -22
  86. package/dist/types/runtime/loaderBridge.d.ts +48 -0
  87. package/dist/types/runtime/outlet.d.ts +2 -0
  88. package/dist/types/runtime/plugin.d.ts +2 -15
  89. package/dist/types/runtime/plugin.node.d.ts +2 -15
  90. package/dist/types/runtime/plugin.worker.d.ts +1 -0
  91. package/dist/types/runtime/pluginCore.d.ts +21 -0
  92. package/dist/types/runtime/register.d.ts +9 -0
  93. package/dist/types/runtime/routeTree.d.ts +0 -2
  94. package/dist/types/runtime/router.d.ts +14 -0
  95. package/dist/types/runtime/state.d.ts +16 -0
  96. package/dist/types/runtime/types.d.ts +14 -53
  97. package/package.json +42 -40
  98. package/rstest.config.mts +6 -0
  99. package/src/cli/index.ts +162 -23
  100. package/src/cli/routeSplitting.ts +43 -0
  101. package/src/cli/tanstackTypes.ts +331 -187
  102. package/src/runtime/hooks.ts +10 -27
  103. package/src/runtime/hydrationBoundary.tsx +12 -0
  104. package/src/runtime/index.tsx +17 -7
  105. package/src/runtime/lifecycle.ts +16 -151
  106. package/src/runtime/loaderBridge.ts +257 -0
  107. package/src/runtime/outlet.tsx +48 -0
  108. package/src/runtime/plugin.node.tsx +72 -85
  109. package/src/runtime/plugin.tsx +361 -206
  110. package/src/runtime/plugin.worker.tsx +4 -0
  111. package/src/runtime/pluginCore.ts +48 -0
  112. package/src/runtime/prefetchLink.tsx +1 -1
  113. package/src/runtime/register.ts +58 -0
  114. package/src/runtime/routeTree.ts +163 -354
  115. package/src/runtime/router.ts +15 -0
  116. package/src/runtime/rsc/payloadRouter.ts +45 -2
  117. package/src/runtime/ssr-shim.d.ts +1 -3
  118. package/src/runtime/state.ts +29 -0
  119. package/src/runtime/types.ts +32 -66
  120. package/src/runtime/utils.tsx +3 -6
  121. package/tests/router/cli.test.ts +586 -5
  122. package/tests/router/fastDefaults.test.ts +25 -0
  123. package/tests/router/hooks.test.ts +26 -0
  124. package/tests/router/hydrationBoundary.test.tsx +23 -0
  125. package/tests/router/loaderBridge.test.ts +211 -0
  126. package/tests/router/packageSurface.test.ts +24 -0
  127. package/tests/router/prefetchLink.test.tsx +43 -7
  128. package/tests/router/register.test.ts +46 -0
  129. package/tests/router/routeTree.test.ts +381 -81
  130. package/tests/router/rsc.test.tsx +70 -0
  131. package/tests/router/tanstackTypes.test.ts +573 -1
  132. package/dist/cjs/runtime/DefaultNotFound.js +0 -47
  133. package/dist/esm/runtime/DefaultNotFound.mjs +0 -13
  134. package/dist/esm-node/runtime/DefaultNotFound.mjs +0 -14
  135. package/dist/types/runtime/DefaultNotFound.d.ts +0 -2
  136. package/src/runtime/DefaultNotFound.tsx +0 -15
@@ -1,5 +1,6 @@
1
- // @effect-diagnostics asyncFunction:off globalFetch:off strictBooleanExpressions:off
1
+ // @effect-diagnostics asyncFunction:off globalFetch:off processEnv:off strictBooleanExpressions:off
2
2
  import type { PayloadRoute, ServerPayload } from '@modern-js/runtime/context';
3
+ import { isRouteErrorResponse } from '@modern-js/runtime-utils/router';
3
4
  import { notFound, redirect } from '@tanstack/react-router';
4
5
 
5
6
  type PayloadDecoder = (stream: ReadableStream<Uint8Array>) => Promise<unknown>;
@@ -103,6 +104,48 @@ function toPayloadRoute(match: RouterMatchLike): PayloadRoute | undefined {
103
104
  };
104
105
  }
105
106
 
107
+ function shouldRedactServerError(status = 500) {
108
+ return (
109
+ status >= 500 &&
110
+ process.env.NODE_ENV !== 'development' &&
111
+ process.env.NODE_ENV !== 'test'
112
+ );
113
+ }
114
+
115
+ function serializePayloadError(error: unknown): unknown {
116
+ if (isRouteErrorResponse(error)) {
117
+ if (shouldRedactServerError(error.status)) {
118
+ return {
119
+ status: error.status,
120
+ statusText: 'Internal Server Error',
121
+ data: 'Unexpected Server Error',
122
+ __type: 'RouteErrorResponse',
123
+ };
124
+ }
125
+
126
+ return { ...error, __type: 'RouteErrorResponse' };
127
+ }
128
+
129
+ if (error instanceof Error) {
130
+ if (shouldRedactServerError()) {
131
+ return {
132
+ message: 'Unexpected Server Error',
133
+ stack: undefined,
134
+ __type: 'Error',
135
+ };
136
+ }
137
+
138
+ return {
139
+ message: error.message,
140
+ stack: error.stack,
141
+ __type: 'Error',
142
+ ...(error.name !== 'Error' ? { __subType: error.name } : {}),
143
+ };
144
+ }
145
+
146
+ return error;
147
+ }
148
+
106
149
  export function createTanstackRscServerPayload(
107
150
  router: TanstackPayloadRouterLike,
108
151
  options: {
@@ -133,7 +176,7 @@ export function createTanstackRscServerPayload(
133
176
  }
134
177
 
135
178
  if (typeof match.error !== 'undefined') {
136
- errors[payloadRoute.id] = match.error;
179
+ errors[payloadRoute.id] = serializePayloadError(match.error);
137
180
  }
138
181
  }
139
182
 
@@ -1,7 +1,5 @@
1
1
  declare module '@tanstack/react-router/ssr/client' {
2
- export function RouterClient(props: {
3
- router: unknown;
4
- }): import('react').JSX.Element;
2
+ export function hydrate(router: unknown): Promise<unknown>;
5
3
  }
6
4
 
7
5
  declare module '@tanstack/react-router/ssr/server' {
@@ -0,0 +1,29 @@
1
+ import { getRouterRuntimeState } from '@modern-js/runtime/context';
2
+ import type { AnyRouter } from '@tanstack/react-router';
3
+ import type { InternalRouterRuntimeState } from './types';
4
+
5
+ /**
6
+ * Router runtime state as published by the TanStack router provider into the
7
+ * runtime-context extension slot.
8
+ */
9
+ export interface TanstackRouterState
10
+ extends Omit<InternalRouterRuntimeState, 'framework' | 'instance'> {
11
+ framework: 'tanstack';
12
+ instance?: AnyRouter;
13
+ }
14
+
15
+ /**
16
+ * Typed accessor for the TanStack router state stored on a Modern.js runtime
17
+ * context. Returns `undefined` when the active router provider is not
18
+ * TanStack (e.g. react-router) or no router has been created yet.
19
+ */
20
+ export function getTanstackRouterState(
21
+ context: object,
22
+ ): TanstackRouterState | undefined {
23
+ const state = getRouterRuntimeState(context);
24
+ if (state === undefined || state.framework !== 'tanstack') {
25
+ return undefined;
26
+ }
27
+
28
+ return state as TanstackRouterState;
29
+ }
@@ -1,83 +1,49 @@
1
- import type { RequestContext } from '@modern-js/runtime-utils/node';
1
+ import type {
2
+ RouterFramework,
3
+ // The router runtime state types are owned by @modern-js/runtime; they are
4
+ // imported through the `/context` seam instead of being copied here so
5
+ // upstream fixes propagate to this package automatically.
6
+ } from '@modern-js/runtime/context';
2
7
  import type { RouteObject } from '@modern-js/runtime-utils/router';
3
8
  import type { NestedRoute, PageRoute } from '@modern-js/types';
4
9
  import type React from 'react';
5
10
 
6
- export type BuiltInRouterFramework = 'react-router' | 'tanstack';
7
- export type RouterFramework = BuiltInRouterFramework | (string & {});
8
-
11
+ export type {
12
+ BuiltInRouterFramework,
13
+ InternalRouterRuntimeState,
14
+ InternalRouterServerSnapshot,
15
+ RouterFramework,
16
+ RouterRouteMatchSnapshot,
17
+ RouterServerPrepareResult,
18
+ } from '@modern-js/runtime/context';
19
+
20
+ /**
21
+ * TanStack-specific router config. Unlike the react-router provider config,
22
+ * this intentionally has no `oldVersion`/`future` fields — those are
23
+ * react-router-only knobs with no meaning here.
24
+ */
9
25
  export type RouterConfig = {
10
26
  framework?: RouterFramework;
11
27
  routesConfig: {
12
28
  globalApp?: React.ComponentType<any>;
13
29
  routes?: (NestedRoute | PageRoute)[];
14
30
  };
15
- oldVersion?: boolean;
16
31
  serverBase?: string[];
17
32
  supportHtml5History?: boolean;
18
33
  basename?: string;
19
34
  createRoutes?: () => RouteObject[];
20
- future?: Partial<{
21
- v7_startTransition: boolean;
22
- }>;
35
+ defaultStructuralSharing?: boolean;
23
36
  unstable_reloadOnURLMismatch?: boolean;
24
37
  };
25
38
 
26
- export interface RouterRouteMatchSnapshot {
27
- routeId: string;
28
- assetRouteId?: string;
29
- pathname?: string;
30
- params?: Record<string, string>;
31
- }
32
-
33
- export interface InternalRouterServerSnapshot {
34
- framework?: RouterFramework;
35
- basename?: string;
36
- statusCode?: number;
37
- errors?: Record<string, unknown>;
38
- routerData?: {
39
- loaderData?: Record<string, unknown>;
40
- errors?: Record<string, unknown>;
41
- };
42
- hydrationScript?: string;
43
- hydrationScripts?: string[];
44
- matchedRouteIds?: string[];
45
- matches?: RouterRouteMatchSnapshot[];
46
- }
47
-
48
- export interface InternalRouterRuntimeState {
49
- framework: RouterFramework;
50
- basename?: string;
51
- instance?: unknown;
52
- hydrationScript?: string;
53
- hydrationScripts?: string[];
54
- matchedRouteIds?: string[];
55
- matches?: RouterRouteMatchSnapshot[];
56
- serverSnapshot?: InternalRouterServerSnapshot;
57
- cleanup?: () => void | Promise<void>;
58
- }
59
-
60
- export interface RouterServerPrepareResult {
61
- state: InternalRouterRuntimeState;
62
- snapshot?: InternalRouterServerSnapshot;
63
- redirect?: Response;
64
- cleanup?: () => void | Promise<void>;
65
- }
66
-
67
- interface DataFunctionArgs<D = any> {
68
- request: Request;
69
- params: Record<string, string>;
70
- context?: D;
71
- }
72
-
73
- export type LoaderFunctionArgs<
74
- P extends Record<string, unknown> = Record<string, unknown>,
75
- > = DataFunctionArgs<RequestContext<P>>;
76
-
77
- type DataFunctionValue = Response | NonNullable<unknown> | null;
78
-
79
- export type LoaderFunction = <
80
- P extends Record<string, unknown> = Record<string, unknown>,
81
- >(
82
- args: LoaderFunctionArgs<P>,
83
- ) => Promise<DataFunctionValue> | DataFunctionValue;
39
+ export const modernTanstackRouterFastDefaults = {
40
+ defaultStructuralSharing: true,
41
+ } as const;
42
+
43
+ export const getModernTanstackRouterFastDefaults = (
44
+ config: Partial<Pick<RouterConfig, 'defaultStructuralSharing'>> = {},
45
+ ) => ({
46
+ defaultStructuralSharing:
47
+ config.defaultStructuralSharing ??
48
+ modernTanstackRouterFastDefaults.defaultStructuralSharing,
49
+ });
@@ -2,7 +2,6 @@
2
2
  import type { RouteObject } from '@modern-js/runtime-utils/router';
3
3
  import type { NestedRoute, PageRoute, SSRMode } from '@modern-js/types';
4
4
  import React from 'react';
5
- import { DefaultNotFound } from './DefaultNotFound';
6
5
 
7
6
  type RouterConfig = {
8
7
  routesConfig: {
@@ -106,11 +105,9 @@ export function getRouteObjects(
106
105
  }
107
106
  }
108
107
 
109
- routeObjects.push({
110
- path: '*',
111
- element: <DefaultNotFound />,
112
- });
113
-
108
+ // No synthetic `{ path: '*' }` 404 route here: TanStack Router handles
109
+ // not-found matches through the root route's `notFoundComponent`
110
+ // (see routeTree.ts), so the react-router style catch-all is unnecessary.
114
111
  return routeObjects;
115
112
  }
116
113