@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.0

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/cli/index.js +268 -0
  3. package/dist/cjs/cli/tanstackTypes.js +388 -0
  4. package/dist/cjs/cli.js +65 -0
  5. package/dist/cjs/runtime/DefaultNotFound.js +47 -0
  6. package/dist/cjs/runtime/basepathRewrite.js +62 -0
  7. package/dist/cjs/runtime/dataMutation.js +345 -0
  8. package/dist/cjs/runtime/hooks.js +57 -0
  9. package/dist/cjs/runtime/index.js +114 -0
  10. package/dist/cjs/runtime/lifecycle.js +125 -0
  11. package/dist/cjs/runtime/plugin.js +250 -0
  12. package/dist/cjs/runtime/plugin.node.js +304 -0
  13. package/dist/cjs/runtime/prefetchLink.js +55 -0
  14. package/dist/cjs/runtime/routeTree.js +492 -0
  15. package/dist/cjs/runtime/rsc/ClientSlot.js +53 -0
  16. package/dist/cjs/runtime/rsc/CompositeComponent.js +75 -0
  17. package/dist/cjs/runtime/rsc/ReplayableStream.js +141 -0
  18. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +65 -0
  19. package/dist/cjs/runtime/rsc/SlotContext.js +54 -0
  20. package/dist/cjs/runtime/rsc/client.js +93 -0
  21. package/dist/cjs/runtime/rsc/createRscProxy.js +141 -0
  22. package/dist/cjs/runtime/rsc/index.js +42 -0
  23. package/dist/cjs/runtime/rsc/payloadRouter.js +211 -0
  24. package/dist/cjs/runtime/rsc/server.js +246 -0
  25. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +65 -0
  26. package/dist/cjs/runtime/rsc/symbols.js +72 -0
  27. package/dist/cjs/runtime/types.js +18 -0
  28. package/dist/cjs/runtime/utils.js +142 -0
  29. package/dist/cjs/runtime.js +58 -0
  30. package/dist/esm/cli/index.mjs +201 -0
  31. package/dist/esm/cli/tanstackTypes.mjs +341 -0
  32. package/dist/esm/cli.mjs +2 -0
  33. package/dist/esm/rslib-runtime.mjs +18 -0
  34. package/dist/esm/runtime/DefaultNotFound.mjs +13 -0
  35. package/dist/esm/runtime/basepathRewrite.mjs +28 -0
  36. package/dist/esm/runtime/dataMutation.mjs +305 -0
  37. package/dist/esm/runtime/hooks.mjs +8 -0
  38. package/dist/esm/runtime/index.mjs +6 -0
  39. package/dist/esm/runtime/lifecycle.mjs +82 -0
  40. package/dist/esm/runtime/plugin.mjs +214 -0
  41. package/dist/esm/runtime/plugin.node.mjs +268 -0
  42. package/dist/esm/runtime/prefetchLink.mjs +18 -0
  43. package/dist/esm/runtime/routeTree.mjs +452 -0
  44. package/dist/esm/runtime/rsc/ClientSlot.mjs +19 -0
  45. package/dist/esm/runtime/rsc/CompositeComponent.mjs +41 -0
  46. package/dist/esm/runtime/rsc/ReplayableStream.mjs +104 -0
  47. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +31 -0
  48. package/dist/esm/runtime/rsc/SlotContext.mjs +17 -0
  49. package/dist/esm/runtime/rsc/client.mjs +53 -0
  50. package/dist/esm/runtime/rsc/createRscProxy.mjs +107 -0
  51. package/dist/esm/runtime/rsc/index.mjs +1 -0
  52. package/dist/esm/runtime/rsc/payloadRouter.mjs +162 -0
  53. package/dist/esm/runtime/rsc/server.mjs +200 -0
  54. package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +31 -0
  55. package/dist/esm/runtime/rsc/symbols.mjs +17 -0
  56. package/dist/esm/runtime/types.mjs +0 -0
  57. package/dist/esm/runtime/utils.mjs +89 -0
  58. package/dist/esm/runtime.mjs +1 -0
  59. package/dist/esm-node/cli/index.mjs +205 -0
  60. package/dist/esm-node/cli/tanstackTypes.mjs +342 -0
  61. package/dist/esm-node/cli.mjs +3 -0
  62. package/dist/esm-node/rslib-runtime.mjs +19 -0
  63. package/dist/esm-node/runtime/DefaultNotFound.mjs +14 -0
  64. package/dist/esm-node/runtime/basepathRewrite.mjs +29 -0
  65. package/dist/esm-node/runtime/dataMutation.mjs +306 -0
  66. package/dist/esm-node/runtime/hooks.mjs +9 -0
  67. package/dist/esm-node/runtime/index.mjs +7 -0
  68. package/dist/esm-node/runtime/lifecycle.mjs +83 -0
  69. package/dist/esm-node/runtime/plugin.mjs +215 -0
  70. package/dist/esm-node/runtime/plugin.node.mjs +269 -0
  71. package/dist/esm-node/runtime/prefetchLink.mjs +19 -0
  72. package/dist/esm-node/runtime/routeTree.mjs +453 -0
  73. package/dist/esm-node/runtime/rsc/ClientSlot.mjs +20 -0
  74. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +42 -0
  75. package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +105 -0
  76. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +32 -0
  77. package/dist/esm-node/runtime/rsc/SlotContext.mjs +18 -0
  78. package/dist/esm-node/runtime/rsc/client.mjs +54 -0
  79. package/dist/esm-node/runtime/rsc/createRscProxy.mjs +108 -0
  80. package/dist/esm-node/runtime/rsc/index.mjs +2 -0
  81. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +163 -0
  82. package/dist/esm-node/runtime/rsc/server.mjs +201 -0
  83. package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +32 -0
  84. package/dist/esm-node/runtime/rsc/symbols.mjs +18 -0
  85. package/dist/esm-node/runtime/types.mjs +1 -0
  86. package/dist/esm-node/runtime/utils.mjs +90 -0
  87. package/dist/esm-node/runtime.mjs +2 -0
  88. package/dist/types/cli/index.d.ts +20 -0
  89. package/dist/types/cli/tanstackTypes.d.ts +11 -0
  90. package/dist/types/cli.d.ts +2 -0
  91. package/dist/types/runtime/DefaultNotFound.d.ts +2 -0
  92. package/dist/types/runtime/basepathRewrite.d.ts +8 -0
  93. package/dist/types/runtime/dataMutation.d.ts +29 -0
  94. package/dist/types/runtime/hooks.d.ts +18 -0
  95. package/dist/types/runtime/index.d.ts +9 -0
  96. package/dist/types/runtime/lifecycle.d.ts +22 -0
  97. package/dist/types/runtime/plugin.d.ts +17 -0
  98. package/dist/types/runtime/plugin.node.d.ts +17 -0
  99. package/dist/types/runtime/prefetchLink.d.ts +11 -0
  100. package/dist/types/runtime/routeTree.d.ts +11 -0
  101. package/dist/types/runtime/rsc/ClientSlot.d.ts +5 -0
  102. package/dist/types/runtime/rsc/CompositeComponent.d.ts +3 -0
  103. package/dist/types/runtime/rsc/ReplayableStream.d.ts +24 -0
  104. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  105. package/dist/types/runtime/rsc/SlotContext.d.ts +11 -0
  106. package/dist/types/runtime/rsc/client.d.ts +11 -0
  107. package/dist/types/runtime/rsc/createRscProxy.d.ts +7 -0
  108. package/dist/types/runtime/rsc/index.d.ts +2 -0
  109. package/dist/types/runtime/rsc/payloadRouter.d.ts +24 -0
  110. package/dist/types/runtime/rsc/server.d.ts +14 -0
  111. package/dist/types/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  112. package/dist/types/runtime/rsc/symbols.d.ts +46 -0
  113. package/dist/types/runtime/types.d.ts +68 -0
  114. package/dist/types/runtime/utils.d.ts +36 -0
  115. package/dist/types/runtime.d.ts +1 -0
  116. package/dist/types-direct/cli/index.d.ts +20 -0
  117. package/dist/types-direct/cli/tanstackTypes.d.ts +11 -0
  118. package/dist/types-direct/cli.d.ts +2 -0
  119. package/dist/types-direct/runtime/DefaultNotFound.d.ts +2 -0
  120. package/dist/types-direct/runtime/basepathRewrite.d.ts +8 -0
  121. package/dist/types-direct/runtime/dataMutation.d.ts +29 -0
  122. package/dist/types-direct/runtime/hooks.d.ts +18 -0
  123. package/dist/types-direct/runtime/index.d.ts +9 -0
  124. package/dist/types-direct/runtime/lifecycle.d.ts +22 -0
  125. package/dist/types-direct/runtime/plugin.d.ts +17 -0
  126. package/dist/types-direct/runtime/plugin.node.d.ts +17 -0
  127. package/dist/types-direct/runtime/prefetchLink.d.ts +11 -0
  128. package/dist/types-direct/runtime/routeTree.d.ts +11 -0
  129. package/dist/types-direct/runtime/rsc/ClientSlot.d.ts +5 -0
  130. package/dist/types-direct/runtime/rsc/CompositeComponent.d.ts +3 -0
  131. package/dist/types-direct/runtime/rsc/ReplayableStream.d.ts +24 -0
  132. package/dist/types-direct/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  133. package/dist/types-direct/runtime/rsc/SlotContext.d.ts +11 -0
  134. package/dist/types-direct/runtime/rsc/client.d.ts +11 -0
  135. package/dist/types-direct/runtime/rsc/createRscProxy.d.ts +7 -0
  136. package/dist/types-direct/runtime/rsc/index.d.ts +2 -0
  137. package/dist/types-direct/runtime/rsc/payloadRouter.d.ts +24 -0
  138. package/dist/types-direct/runtime/rsc/server.d.ts +14 -0
  139. package/dist/types-direct/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  140. package/dist/types-direct/runtime/rsc/symbols.d.ts +46 -0
  141. package/dist/types-direct/runtime/types.d.ts +68 -0
  142. package/dist/types-direct/runtime/utils.d.ts +36 -0
  143. package/dist/types-direct/runtime.d.ts +1 -0
  144. package/package.json +126 -0
  145. package/rslib.config.mts +4 -0
  146. package/rstest.config.mts +43 -0
  147. package/src/cli/index.ts +388 -0
  148. package/src/cli/tanstackTypes.ts +503 -0
  149. package/src/cli.ts +2 -0
  150. package/src/runtime/DefaultNotFound.tsx +15 -0
  151. package/src/runtime/basepathRewrite.ts +59 -0
  152. package/src/runtime/dataMutation.tsx +517 -0
  153. package/src/runtime/hooks.ts +34 -0
  154. package/src/runtime/index.tsx +30 -0
  155. package/src/runtime/lifecycle.ts +150 -0
  156. package/src/runtime/plugin.node.tsx +534 -0
  157. package/src/runtime/plugin.tsx +395 -0
  158. package/src/runtime/prefetchLink.tsx +87 -0
  159. package/src/runtime/routeTree.ts +942 -0
  160. package/src/runtime/rsc/ClientSlot.tsx +25 -0
  161. package/src/runtime/rsc/CompositeComponent.tsx +65 -0
  162. package/src/runtime/rsc/ReplayableStream.ts +155 -0
  163. package/src/runtime/rsc/RscNodeRenderer.tsx +45 -0
  164. package/src/runtime/rsc/SlotContext.tsx +31 -0
  165. package/src/runtime/rsc/client.tsx +90 -0
  166. package/src/runtime/rsc/createRscProxy.tsx +189 -0
  167. package/src/runtime/rsc/index.ts +10 -0
  168. package/src/runtime/rsc/payloadRouter.ts +318 -0
  169. package/src/runtime/rsc/server.tsx +303 -0
  170. package/src/runtime/rsc/slotUsageSanitizer.ts +76 -0
  171. package/src/runtime/rsc/symbols.ts +106 -0
  172. package/src/runtime/ssr-shim.d.ts +12 -0
  173. package/src/runtime/types.ts +83 -0
  174. package/src/runtime/utils.tsx +161 -0
  175. package/src/runtime.ts +1 -0
  176. package/tests/router/cli.test.ts +386 -0
  177. package/tests/router/dataMutation.test.tsx +396 -0
  178. package/tests/router/prefetchLink.test.tsx +43 -0
  179. package/tests/router/routeTree.test.ts +502 -0
  180. package/tests/router/rsc.test.tsx +256 -0
  181. package/tests/router/tanstackTypes.test.ts +62 -0
  182. package/tsconfig.json +12 -0
  183. package/tsconfig.tsgo.json +6 -0
@@ -0,0 +1,305 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useRouter } from "@tanstack/react-router";
3
+ import { useCallback, useRef, useState } from "react";
4
+ class RouteActionResponseError extends Error {
5
+ constructor(response, data){
6
+ super(`Route action failed with status ${response.status}`);
7
+ this.name = 'RouteActionResponseError';
8
+ this.response = response;
9
+ this.data = data;
10
+ }
11
+ }
12
+ function formDataToUrlSearchParams(formData) {
13
+ const searchParams = new URLSearchParams();
14
+ formData.forEach((value, key)=>{
15
+ if ('string' == typeof value) searchParams.append(key, value);
16
+ });
17
+ return searchParams;
18
+ }
19
+ function formDataToTextPlain(formData) {
20
+ return Array.from(formData.entries()).map(([key, value])=>`${key}=${String(value)}`).join('\n');
21
+ }
22
+ function toFormData(target) {
23
+ if (target instanceof HTMLFormElement) return new FormData(target);
24
+ if (target instanceof FormData) return target;
25
+ if (target instanceof URLSearchParams) {
26
+ const formData = new FormData();
27
+ target.forEach((value, key)=>{
28
+ formData.append(key, value);
29
+ });
30
+ return formData;
31
+ }
32
+ const formData = new FormData();
33
+ Object.entries(target).forEach(([key, value])=>{
34
+ if (null == value) return;
35
+ formData.append(key, String(value));
36
+ });
37
+ return formData;
38
+ }
39
+ function getSubmitter(event) {
40
+ const nativeEvent = event.nativeEvent;
41
+ const submitter = nativeEvent?.submitter;
42
+ if (submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement) return submitter;
43
+ return null;
44
+ }
45
+ function createFormDataFromSubmit({ form, submitter }) {
46
+ if (submitter) try {
47
+ return new FormData(form, submitter);
48
+ } catch {}
49
+ return new FormData(form);
50
+ }
51
+ function resolveSubmitOptionsFromForm({ form, submitter, action, method, encType }) {
52
+ const resolvedAction = submitter?.getAttribute('formaction') || action || form.getAttribute('action') || '.';
53
+ const resolvedMethod = (submitter?.getAttribute('formmethod') || method || form.getAttribute('method') || 'get').toLowerCase();
54
+ const resolvedEncType = submitter?.getAttribute('formenctype') || encType || form.getAttribute('enctype') || 'application/x-www-form-urlencoded';
55
+ return {
56
+ action: resolvedAction,
57
+ method: resolvedMethod,
58
+ encType: resolvedEncType
59
+ };
60
+ }
61
+ function resolveRouteHandlers(router, actionTo) {
62
+ const builtLocation = router.buildLocation({
63
+ to: actionTo
64
+ });
65
+ const href = router.getParsedLocationHref(builtLocation);
66
+ const matchedRoutes = router.getMatchedRoutes(builtLocation.pathname);
67
+ const routeStaticData = matchedRoutes.foundRoute?.options?.staticData;
68
+ const action = routeStaticData?.modernRouteAction;
69
+ const loader = routeStaticData?.modernRouteLoader;
70
+ return {
71
+ action,
72
+ loader,
73
+ href,
74
+ params: matchedRoutes.routeParams || {}
75
+ };
76
+ }
77
+ function isRedirectResponse(value) {
78
+ if (!(value instanceof Response)) return false;
79
+ return [
80
+ 301,
81
+ 302,
82
+ 303,
83
+ 307,
84
+ 308
85
+ ].includes(value.status);
86
+ }
87
+ async function parseResponseData(response) {
88
+ if (204 === response.status) return null;
89
+ const contentType = response.headers.get('Content-Type') || '';
90
+ if (contentType.includes('application/json')) return response.json();
91
+ return response.text();
92
+ }
93
+ async function parseResponseResultOrThrow(response) {
94
+ const parsed = await parseResponseData(response);
95
+ if (!response.ok) throw new RouteActionResponseError(response, parsed);
96
+ return parsed;
97
+ }
98
+ async function submitRouteAction({ router, target, options = {}, isFetcher = false, onInvalidateStart }) {
99
+ const method = (options.method || 'post').toLowerCase();
100
+ const encType = options.encType || 'application/x-www-form-urlencoded';
101
+ const actionTo = options.action || '.';
102
+ const formData = toFormData(target);
103
+ const resolved = resolveRouteHandlers(router, actionTo);
104
+ if ('get' === method) {
105
+ const search = formDataToUrlSearchParams(formData).toString();
106
+ const requestUrl = new URL(resolved.href, window.location.origin);
107
+ requestUrl.search = search;
108
+ if (isFetcher && resolved.loader) {
109
+ const result = await resolved.loader({
110
+ request: new Request(requestUrl, {
111
+ method: 'GET'
112
+ }),
113
+ params: resolved.params
114
+ });
115
+ if (result instanceof Response) {
116
+ const redirectTo = result.headers.get('X-Modernjs-Redirect') || result.headers.get('Location');
117
+ if (redirectTo || isRedirectResponse(result)) {
118
+ await router.navigate({
119
+ to: redirectTo || '/'
120
+ });
121
+ return parseResponseData(result);
122
+ }
123
+ return parseResponseResultOrThrow(result);
124
+ }
125
+ return result;
126
+ }
127
+ await router.navigate({
128
+ href: search ? `${resolved.href}?${search}` : resolved.href
129
+ });
130
+ return;
131
+ }
132
+ if (!resolved.action) throw new Error(`No route action found for "${actionTo}"`);
133
+ const headers = new Headers();
134
+ let body = null;
135
+ if (encType.includes('application/json')) {
136
+ headers.set('Content-Type', 'application/json');
137
+ body = JSON.stringify(Object.fromEntries(formDataToUrlSearchParams(formData).entries()));
138
+ } else if (encType.includes('text/plain')) {
139
+ headers.set('Content-Type', 'text/plain;charset=UTF-8');
140
+ body = formDataToTextPlain(formData);
141
+ } else if (encType.includes('application/x-www-form-urlencoded')) {
142
+ headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
143
+ body = formDataToUrlSearchParams(formData);
144
+ } else body = formData;
145
+ const request = new Request(new URL(resolved.href, window.location.origin), {
146
+ method: method.toUpperCase(),
147
+ headers,
148
+ body
149
+ });
150
+ const result = await resolved.action({
151
+ request,
152
+ params: resolved.params
153
+ });
154
+ if (result instanceof Response) {
155
+ const redirectTo = result.headers.get('X-Modernjs-Redirect') || result.headers.get('Location');
156
+ if (redirectTo || isRedirectResponse(result)) {
157
+ await router.navigate({
158
+ to: redirectTo || '/'
159
+ });
160
+ return parseResponseData(result);
161
+ }
162
+ const parsed = isFetcher ? await parseResponseResultOrThrow(result) : await parseResponseData(result);
163
+ onInvalidateStart?.();
164
+ await router.invalidate();
165
+ return parsed;
166
+ }
167
+ onInvalidateStart?.();
168
+ await router.invalidate();
169
+ return result;
170
+ }
171
+ function Form({ action, method = 'get', encType, reloadDocument, onSubmit, ...rest }) {
172
+ const router = useRouter();
173
+ const handleSubmit = useCallback(async (event)=>{
174
+ onSubmit?.(event);
175
+ if (event.defaultPrevented || reloadDocument) return;
176
+ event.preventDefault();
177
+ const submitter = getSubmitter(event);
178
+ const formData = createFormDataFromSubmit({
179
+ form: event.currentTarget,
180
+ submitter
181
+ });
182
+ const normalizedOptions = resolveSubmitOptionsFromForm({
183
+ form: event.currentTarget,
184
+ submitter,
185
+ action,
186
+ method,
187
+ encType
188
+ });
189
+ await submitRouteAction({
190
+ router,
191
+ target: formData,
192
+ options: normalizedOptions
193
+ });
194
+ }, [
195
+ action,
196
+ encType,
197
+ method,
198
+ onSubmit,
199
+ reloadDocument,
200
+ router
201
+ ]);
202
+ return /*#__PURE__*/ jsx("form", {
203
+ ...rest,
204
+ action: action,
205
+ method: method,
206
+ encType: encType,
207
+ onSubmit: handleSubmit
208
+ });
209
+ }
210
+ function useFetcher() {
211
+ const router = useRouter();
212
+ const [state, setState] = useState('idle');
213
+ const [data, setData] = useState(void 0);
214
+ const [error, setError] = useState(void 0);
215
+ const requestStatesRef = useRef(new Map());
216
+ const requestIdRef = useRef(0);
217
+ const syncStateFromRequests = useCallback(()=>{
218
+ let hasSubmitting = false;
219
+ let hasLoading = false;
220
+ requestStatesRef.current.forEach((requestState)=>{
221
+ if ('submitting' === requestState) hasSubmitting = true;
222
+ else if ('loading' === requestState) hasLoading = true;
223
+ });
224
+ if (hasSubmitting) return void setState('submitting');
225
+ if (hasLoading) return void setState('loading');
226
+ setState('idle');
227
+ }, []);
228
+ const setRequestState = useCallback((requestId, requestState)=>{
229
+ requestStatesRef.current.set(requestId, requestState);
230
+ syncStateFromRequests();
231
+ }, [
232
+ syncStateFromRequests
233
+ ]);
234
+ const clearRequestState = useCallback((requestId)=>{
235
+ requestStatesRef.current.delete(requestId);
236
+ syncStateFromRequests();
237
+ }, [
238
+ syncStateFromRequests
239
+ ]);
240
+ const submit = useCallback(async (target, options)=>{
241
+ setError(void 0);
242
+ const requestId = ++requestIdRef.current;
243
+ const normalizedMethod = (options?.method || 'post').toLowerCase();
244
+ const isLoaderSubmit = 'get' === normalizedMethod;
245
+ setRequestState(requestId, isLoaderSubmit ? 'loading' : 'submitting');
246
+ try {
247
+ const result = await submitRouteAction({
248
+ router,
249
+ target,
250
+ options,
251
+ isFetcher: true,
252
+ onInvalidateStart: ()=>{
253
+ if (!isLoaderSubmit) setRequestState(requestId, 'loading');
254
+ }
255
+ });
256
+ setData(result);
257
+ } catch (err) {
258
+ setError(err);
259
+ throw err;
260
+ } finally{
261
+ clearRequestState(requestId);
262
+ }
263
+ }, [
264
+ clearRequestState,
265
+ router,
266
+ setRequestState
267
+ ]);
268
+ const FetcherForm = useCallback(({ action, method = 'get', encType, reloadDocument, onSubmit, ...rest })=>{
269
+ const handleSubmit = async (event)=>{
270
+ onSubmit?.(event);
271
+ if (event.defaultPrevented || reloadDocument) return;
272
+ event.preventDefault();
273
+ const submitter = getSubmitter(event);
274
+ const formData = createFormDataFromSubmit({
275
+ form: event.currentTarget,
276
+ submitter
277
+ });
278
+ const normalizedOptions = resolveSubmitOptionsFromForm({
279
+ form: event.currentTarget,
280
+ submitter,
281
+ action,
282
+ method,
283
+ encType
284
+ });
285
+ await submit(formData, normalizedOptions);
286
+ };
287
+ return /*#__PURE__*/ jsx("form", {
288
+ ...rest,
289
+ action: action,
290
+ method: method,
291
+ encType: encType,
292
+ onSubmit: handleSubmit
293
+ });
294
+ }, [
295
+ submit
296
+ ]);
297
+ return {
298
+ state,
299
+ data,
300
+ error,
301
+ Form: FetcherForm,
302
+ submit
303
+ };
304
+ }
305
+ export { Form, RouteActionResponseError, useFetcher };
@@ -0,0 +1,8 @@
1
+ import { createSyncHook } from "@modern-js/plugin";
2
+ const modifyRoutes = createSyncHook();
3
+ const onBeforeCreateRoutes = createSyncHook();
4
+ const onBeforeCreateRouter = createSyncHook();
5
+ const onAfterCreateRouter = createSyncHook();
6
+ const onBeforeHydrateRouter = createSyncHook();
7
+ const onAfterHydrateRouter = createSyncHook();
8
+ export { modifyRoutes, onAfterCreateRouter, onAfterHydrateRouter, onBeforeCreateRouter, onBeforeCreateRoutes, onBeforeHydrateRouter };
@@ -0,0 +1,6 @@
1
+ export * from "@tanstack/react-router";
2
+ export { useMatch } from "@tanstack/react-router";
3
+ export { Form, RouteActionResponseError, useFetcher } from "./dataMutation.mjs";
4
+ export { tanstackRouterPlugin as default, tanstackRouterPlugin } from "./plugin.mjs";
5
+ export { Link, NavLink } from "./prefetchLink.mjs";
6
+ export { CompositeComponent } from "./rsc/client.mjs";
@@ -0,0 +1,82 @@
1
+ function toHydrationScripts(state) {
2
+ if (state.hydrationScripts?.length) return state.hydrationScripts;
3
+ return state.hydrationScript ? [
4
+ state.hydrationScript
5
+ ] : void 0;
6
+ }
7
+ function getMatchedRouteIdsFromMatches(matches) {
8
+ const routeIds = matches?.map((match)=>match.assetRouteId ?? match.routeId).filter((routeId)=>'string' == typeof routeId);
9
+ return routeIds?.length ? routeIds : void 0;
10
+ }
11
+ function createRouterServerSnapshot(state) {
12
+ const hydrationScripts = toHydrationScripts(state);
13
+ const matchedRouteIds = state.matchedRouteIds ?? getMatchedRouteIdsFromMatches(state.matches);
14
+ return {
15
+ ...state,
16
+ ...hydrationScripts?.length ? {
17
+ hydrationScript: state.hydrationScript ?? hydrationScripts[0],
18
+ hydrationScripts
19
+ } : {},
20
+ ...matchedRouteIds ? {
21
+ matchedRouteIds
22
+ } : {}
23
+ };
24
+ }
25
+ function createRouterRuntimeState(state) {
26
+ const hasSnapshotState = Boolean(state.serverSnapshot) || Boolean(state.hydrationScript) || Boolean(state.hydrationScripts?.length) || Boolean(state.matchedRouteIds?.length) || Boolean(state.matches?.length);
27
+ const serverSnapshot = state.serverSnapshot ? createRouterServerSnapshot({
28
+ ...state.serverSnapshot,
29
+ framework: state.serverSnapshot.framework ?? state.framework,
30
+ basename: state.serverSnapshot.basename ?? state.basename,
31
+ hydrationScript: state.serverSnapshot.hydrationScript ?? state.hydrationScript,
32
+ hydrationScripts: state.serverSnapshot.hydrationScripts ?? state.hydrationScripts,
33
+ matchedRouteIds: state.serverSnapshot.matchedRouteIds ?? state.matchedRouteIds,
34
+ matches: state.serverSnapshot.matches ?? state.matches
35
+ }) : hasSnapshotState ? createRouterServerSnapshot({
36
+ framework: state.framework,
37
+ basename: state.basename,
38
+ hydrationScript: state.hydrationScript,
39
+ hydrationScripts: state.hydrationScripts,
40
+ matchedRouteIds: state.matchedRouteIds,
41
+ matches: state.matches
42
+ }) : void 0;
43
+ const hydrationScripts = toHydrationScripts({
44
+ hydrationScript: state.hydrationScript ?? serverSnapshot?.hydrationScript,
45
+ hydrationScripts: state.hydrationScripts ?? serverSnapshot?.hydrationScripts
46
+ });
47
+ const matchedRouteIds = state.matchedRouteIds ?? serverSnapshot?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(state.matches);
48
+ return {
49
+ ...state,
50
+ ...hydrationScripts?.length ? {
51
+ hydrationScript: state.hydrationScript ?? hydrationScripts[0],
52
+ hydrationScripts
53
+ } : {},
54
+ ...matchedRouteIds ? {
55
+ matchedRouteIds
56
+ } : {},
57
+ ...serverSnapshot ? {
58
+ serverSnapshot
59
+ } : {}
60
+ };
61
+ }
62
+ function applyRouterRuntimeState(runtimeContext, state) {
63
+ const normalized = createRouterRuntimeState(state);
64
+ const mutableRuntimeContext = runtimeContext;
65
+ mutableRuntimeContext.routerFramework = normalized.framework;
66
+ mutableRuntimeContext.routerInstance = normalized.instance;
67
+ mutableRuntimeContext.routerHydrationScript = normalized.hydrationScript;
68
+ mutableRuntimeContext.routerMatchedRouteIds = normalized.matchedRouteIds;
69
+ mutableRuntimeContext.routerRuntime = normalized;
70
+ if (normalized.serverSnapshot) mutableRuntimeContext.routerServerSnapshot = normalized.serverSnapshot;
71
+ return runtimeContext;
72
+ }
73
+ function applyRouterServerPrepareResult(runtimeContext, result) {
74
+ const state = createRouterRuntimeState({
75
+ ...result.state,
76
+ cleanup: result.cleanup ?? result.state.cleanup,
77
+ serverSnapshot: result.snapshot ?? result.state.serverSnapshot
78
+ });
79
+ applyRouterRuntimeState(runtimeContext, state);
80
+ return runtimeContext;
81
+ }
82
+ export { applyRouterRuntimeState, applyRouterServerPrepareResult, createRouterRuntimeState, createRouterServerSnapshot };
@@ -0,0 +1,214 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { InternalRuntimeContext, getGlobalEnableRsc, getGlobalLayoutApp, getGlobalRoutes } from "@modern-js/runtime/context";
3
+ import { merge } from "@modern-js/runtime-utils/merge";
4
+ import { normalizePathname } from "@modern-js/runtime-utils/url";
5
+ import { RouterProvider, createBrowserHistory, createHashHistory, createRouter, useLocation, useMatches, useNavigate, useRouter } from "@tanstack/react-router";
6
+ import { RouterClient } from "@tanstack/react-router/ssr/client";
7
+ import { createModernBasepathRewrite } from "./basepathRewrite.mjs";
8
+ import { modifyRoutes, onAfterCreateRouter, onAfterHydrateRouter, onBeforeCreateRouter, onBeforeCreateRoutes, onBeforeHydrateRouter } from "./hooks.mjs";
9
+ import { applyRouterRuntimeState } from "./lifecycle.mjs";
10
+ import { createRouteTreeFromRouteObjects } from "./routeTree.mjs";
11
+ import { getTanstackRscSerializationAdapters } from "./rsc/client.mjs";
12
+ import { createRouteObjectsFromConfig, urlJoin } from "./utils.mjs";
13
+ import * as __rspack_external_react from "react";
14
+ const BLOCKING_SUBSCRIBE_SYMBOL = Symbol.for('@modern-js/plugin-tanstack:blocking-subscribe');
15
+ const BLOCKING_STATE_SYMBOL = Symbol.for('@modern-js/plugin-tanstack:blocking-state');
16
+ function normalizeBase(b) {
17
+ if (b.length > 1 && b.endsWith('/')) return b.slice(0, -1);
18
+ return b || '/';
19
+ }
20
+ function isSegmentPrefix(pathname, base) {
21
+ const b = normalizeBase(base);
22
+ const p = pathname || '/';
23
+ return p === b || p.startsWith(`${b}/`);
24
+ }
25
+ function wrapRouterSubscribeWithBlockState(router, getBlockNavState) {
26
+ if (!router || 'object' != typeof router) return;
27
+ const target = router;
28
+ target[BLOCKING_STATE_SYMBOL] = getBlockNavState;
29
+ if (target[BLOCKING_SUBSCRIBE_SYMBOL] || 'function' != typeof target.subscribe) return;
30
+ const originSubscribe = target.subscribe.bind(target);
31
+ target.subscribe = (eventType, listener)=>{
32
+ const wrappedListener = (...args)=>{
33
+ const blockRoute = target[BLOCKING_STATE_SYMBOL]?.() || false;
34
+ if (blockRoute) return;
35
+ return listener(...args);
36
+ };
37
+ return originSubscribe(eventType, wrappedListener);
38
+ };
39
+ target[BLOCKING_SUBSCRIBE_SYMBOL] = true;
40
+ }
41
+ function stripSyntheticNotFoundRoute(routes) {
42
+ return routes.filter((route)=>!('*' === route.path && !route.id && !route.loader)).map((route)=>{
43
+ if (!route.children?.length) return route;
44
+ return {
45
+ ...route,
46
+ children: stripSyntheticNotFoundRoute(route.children)
47
+ };
48
+ });
49
+ }
50
+ const tanstackRouterPlugin = (userConfig = {})=>{
51
+ const plugin = {
52
+ name: '@modern-js/plugin-router-tanstack',
53
+ registryHooks: {
54
+ modifyRoutes: modifyRoutes,
55
+ onAfterCreateRouter: onAfterCreateRouter,
56
+ onAfterHydrateRouter: onAfterHydrateRouter,
57
+ onBeforeCreateRouter: onBeforeCreateRouter,
58
+ onBeforeCreateRoutes: onBeforeCreateRoutes,
59
+ onBeforeHydrateRouter: onBeforeHydrateRouter
60
+ },
61
+ setup: (api)=>{
62
+ api.onBeforeRender((context)=>{
63
+ const pluginConfig = api.getRuntimeConfig();
64
+ const mergedConfig = merge(pluginConfig.router || {}, userConfig);
65
+ if ("u" > typeof window && window._SSR_DATA && mergedConfig.unstable_reloadOnURLMismatch) {
66
+ const { ssrContext } = context;
67
+ const currentPathname = normalizePathname(window.location.pathname);
68
+ const initialPathname = ssrContext?.request?.pathname && normalizePathname(ssrContext.request.pathname);
69
+ if (initialPathname && initialPathname !== currentPathname) {
70
+ const errorMsg = `The initial URL ${initialPathname} and the URL ${currentPathname} to be hydrated do not match, reload.`;
71
+ console.error(errorMsg);
72
+ window.location.reload();
73
+ }
74
+ }
75
+ context.router = {
76
+ useMatches: useMatches,
77
+ useLocation: useLocation,
78
+ useNavigate: useNavigate,
79
+ useRouter: useRouter
80
+ };
81
+ });
82
+ api.wrapRoot((App)=>{
83
+ const mergedConfig = merge(api.getRuntimeConfig().router || {}, userConfig);
84
+ const { serverBase = [], supportHtml5History = true, basename = '', routesConfig, createRoutes } = mergedConfig;
85
+ const finalRouteConfig = {
86
+ routes: getGlobalRoutes(),
87
+ globalApp: getGlobalLayoutApp(),
88
+ ...routesConfig
89
+ };
90
+ if (!finalRouteConfig.routes && !createRoutes) return App;
91
+ const hooks = api.getHooks();
92
+ let cachedRouteObjects;
93
+ const getRouteObjects = ()=>{
94
+ if (void 0 !== cachedRouteObjects) return cachedRouteObjects;
95
+ const routeObjects = createRoutes ? createRoutes() : createRouteObjectsFromConfig({
96
+ routesConfig: finalRouteConfig
97
+ }) || [];
98
+ const normalizedRouteObjects = createRoutes ? routeObjects : stripSyntheticNotFoundRoute(routeObjects);
99
+ cachedRouteObjects = hooks.modifyRoutes.call(normalizedRouteObjects);
100
+ return cachedRouteObjects;
101
+ };
102
+ const selectBasePath = (pathname)=>{
103
+ const match = serverBase.find((baseUrl)=>isSegmentPrefix(pathname, baseUrl));
104
+ return match || '/';
105
+ };
106
+ let cachedRouteTree = null;
107
+ let cachedRouter = null;
108
+ let cachedRouterBasepath = null;
109
+ const RouterWrapper = ()=>{
110
+ const runtimeContext = (0, __rspack_external_react.useContext)(InternalRuntimeContext);
111
+ const baseUrl = selectBasePath(location.pathname).replace(/^\/*/, '/');
112
+ const _basename = '/' === baseUrl ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename || '') : baseUrl;
113
+ const routeTree = (0, __rspack_external_react.useMemo)(()=>{
114
+ if (cachedRouteTree) return cachedRouteTree;
115
+ const routeObjects = getRouteObjects();
116
+ if (!routeObjects.length) return null;
117
+ cachedRouteTree = createRouteTreeFromRouteObjects(routeObjects, {
118
+ rscPayloadRouter: getGlobalEnableRsc()
119
+ });
120
+ return cachedRouteTree;
121
+ }, []);
122
+ if (!routeTree) return App ? /*#__PURE__*/ jsx(App, {}) : null;
123
+ const router = (0, __rspack_external_react.useMemo)(()=>{
124
+ const lifecycleContext = {
125
+ framework: 'tanstack',
126
+ phase: 'client-create',
127
+ routes: getRouteObjects(),
128
+ runtimeContext,
129
+ basename: _basename
130
+ };
131
+ hooks.onBeforeCreateRouter.call(lifecycleContext);
132
+ if (cachedRouter && cachedRouterBasepath === _basename) {
133
+ wrapRouterSubscribeWithBlockState(cachedRouter, runtimeContext.unstable_getBlockNavState);
134
+ hooks.onAfterCreateRouter.call({
135
+ ...lifecycleContext,
136
+ router: cachedRouter,
137
+ runtimeContext
138
+ });
139
+ return cachedRouter;
140
+ }
141
+ const history = supportHtml5History ? createBrowserHistory() : createHashHistory();
142
+ const rewrite = createModernBasepathRewrite(_basename);
143
+ const serializationAdapters = getGlobalEnableRsc() ? getTanstackRscSerializationAdapters() : void 0;
144
+ cachedRouter = createRouter({
145
+ routeTree,
146
+ basepath: '/',
147
+ rewrite,
148
+ history,
149
+ context: {},
150
+ ...serializationAdapters ? {
151
+ serializationAdapters
152
+ } : {}
153
+ });
154
+ cachedRouterBasepath = _basename;
155
+ wrapRouterSubscribeWithBlockState(cachedRouter, runtimeContext.unstable_getBlockNavState);
156
+ hooks.onAfterCreateRouter.call({
157
+ ...lifecycleContext,
158
+ router: cachedRouter,
159
+ runtimeContext
160
+ });
161
+ return cachedRouter;
162
+ }, [
163
+ _basename,
164
+ routeTree,
165
+ supportHtml5History,
166
+ runtimeContext
167
+ ]);
168
+ const runtimeState = applyRouterRuntimeState(runtimeContext, {
169
+ framework: 'tanstack',
170
+ basename: _basename,
171
+ instance: router
172
+ });
173
+ const lifecycleContext = {
174
+ framework: 'tanstack',
175
+ phase: 'client-create',
176
+ routes: getRouteObjects(),
177
+ runtimeContext: runtimeState,
178
+ basename: _basename,
179
+ router
180
+ };
181
+ const hasSSRBootstrap = "u" > typeof window && Boolean(window.$_TSR);
182
+ if (hasSSRBootstrap) hooks.onBeforeHydrateRouter.call({
183
+ ...lifecycleContext,
184
+ phase: 'hydrate',
185
+ router,
186
+ runtimeContext: runtimeState
187
+ });
188
+ const RouterContent = hasSSRBootstrap ? /*#__PURE__*/ jsx(__rspack_external_react.Suspense, {
189
+ fallback: null,
190
+ children: /*#__PURE__*/ jsx(RouterClient, {
191
+ router: router
192
+ })
193
+ }) : /*#__PURE__*/ jsx(RouterProvider, {
194
+ router: router
195
+ });
196
+ if (hasSSRBootstrap) hooks.onAfterHydrateRouter.call({
197
+ ...lifecycleContext,
198
+ phase: 'hydrate',
199
+ router,
200
+ runtimeContext: runtimeState
201
+ });
202
+ return App ? /*#__PURE__*/ jsx(App, {
203
+ children: RouterContent
204
+ }) : RouterContent;
205
+ };
206
+ return RouterWrapper;
207
+ });
208
+ }
209
+ };
210
+ return plugin;
211
+ };
212
+ const runtime_plugin = tanstackRouterPlugin;
213
+ export default runtime_plugin;
214
+ export { tanstackRouterPlugin };