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