@teyik0/furin 0.7.0-alpha.3 → 0.9.0-alpha.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 (541) hide show
  1. package/README.md +38 -1
  2. package/dist/adapter/build.d.ts +9 -0
  3. package/dist/adapter/bun/app.d.ts +1 -0
  4. package/dist/adapter/bun/server.d.ts +1 -0
  5. package/dist/adapter/bun.d.ts +9 -3
  6. package/dist/adapter/package.d.ts +1 -0
  7. package/dist/adapter/prerender.d.ts +21 -0
  8. package/dist/adapter/runtime-build.d.ts +31 -14
  9. package/dist/adapter/server-bundle.d.ts +20 -0
  10. package/dist/adapter/static.d.ts +2 -2
  11. package/dist/adapter/types.d.ts +81 -0
  12. package/dist/adapter/vercel/bootstrap.d.ts +5 -0
  13. package/dist/adapter/vercel/data.d.ts +23 -0
  14. package/dist/adapter/vercel/handler.d.ts +10 -0
  15. package/dist/adapter/vercel.d.ts +13 -3
  16. package/dist/adapter/vercel.js +516 -0
  17. package/dist/build/bun-build.d.ts +4 -0
  18. package/dist/build/client.d.ts +2 -2
  19. package/dist/build/compile-entry.ts +1 -0
  20. package/dist/build/discover-mounts-process.d.ts +1 -0
  21. package/dist/build/discover-mounts.d.ts +6 -0
  22. package/dist/build/discover-pages-process.d.ts +1 -0
  23. package/dist/build/discover-pages.d.ts +11 -0
  24. package/dist/build/discovery-module.d.ts +3 -0
  25. package/dist/build/discovery-plugins.d.ts +34 -0
  26. package/dist/build/elysia-aot-worker.d.ts +4 -0
  27. package/dist/build/elysia-aot.d.ts +8 -2
  28. package/dist/build/entry-template.d.ts +14 -7
  29. package/dist/build/entry-template.ts +39 -19
  30. package/dist/build/hydrate.d.ts +17 -3
  31. package/dist/build/index.d.ts +7 -3
  32. package/dist/build/index.js +8852 -16175
  33. package/dist/build/preload-manifest.d.ts +2 -2
  34. package/dist/build/private-server-sourcemaps.d.ts +2 -0
  35. package/dist/build/route-metadata.d.ts +31 -0
  36. package/dist/build/route-types.d.ts +8 -2
  37. package/dist/build/server-app-entry.d.ts +2 -2
  38. package/dist/build/shared.d.ts +5 -7
  39. package/dist/build/types.d.ts +27 -16
  40. package/dist/cache.js +117 -32
  41. package/dist/cli/config.d.ts +2 -2
  42. package/dist/cli/dev-worker.d.ts +1 -0
  43. package/dist/cli/dev-worker.ts +123 -0
  44. package/dist/cli/dev.d.ts +1 -0
  45. package/dist/cli/index.js +9104 -16086
  46. package/dist/client/browser-events-runtime.d.ts +2 -2
  47. package/dist/client/browser-events.d.ts +2 -0
  48. package/dist/client/chunks/client-k29262a4.js +59 -0
  49. package/dist/client/chunks/client-k88668k7.js +159 -0
  50. package/dist/client/chunks/client-rjhxgt64.js +1035 -0
  51. package/dist/client/chunks/client-xy6g38kx.js +317 -0
  52. package/dist/client/default-screens.d.ts +0 -11
  53. package/dist/client/define-route.d.ts +2 -1
  54. package/dist/client/dev-error-overlay.ts +18 -1
  55. package/dist/client/hmr.d.ts +1 -1
  56. package/dist/client/internal/runtime-types.d.ts +7 -1
  57. package/dist/client/link.d.ts +1 -0
  58. package/dist/client/link.js +1738 -4184
  59. package/dist/client/mutation.d.ts +5 -0
  60. package/dist/client/query-store.d.ts +22 -7
  61. package/dist/client/query.d.ts +6 -8
  62. package/dist/client/request-id.d.ts +1 -0
  63. package/dist/client/route-api.d.ts +4 -4
  64. package/dist/client/route-element.d.ts +51 -0
  65. package/dist/client/router/boundary-tree.d.ts +1 -9
  66. package/dist/client/router/cache-generation.d.ts +15 -0
  67. package/dist/client/router/context.d.ts +13 -2
  68. package/dist/client/router/fetch-observers.d.ts +9 -0
  69. package/dist/client/router/hmr-refresh.d.ts +12 -0
  70. package/dist/client/router/index.d.ts +5 -7
  71. package/dist/client/router/link-utils.d.ts +0 -2
  72. package/dist/client/router/navigation-controller.d.ts +78 -0
  73. package/dist/client/router/navigation.d.ts +1 -0
  74. package/dist/client/router/page-fetch.d.ts +38 -0
  75. package/dist/client/router/provider.d.ts +2 -3
  76. package/dist/client/router/search/index.d.ts +11 -4
  77. package/dist/client/router/search/index.js +60 -0
  78. package/dist/client/router/spa-response.d.ts +9 -26
  79. package/dist/client/router/transport.d.ts +4 -0
  80. package/dist/client/router/types.d.ts +120 -46
  81. package/dist/client/router/use-fetch-interception.d.ts +12 -0
  82. package/dist/client/router/use-history-sync.d.ts +7 -0
  83. package/dist/client/router/use-hmr-refresh.d.ts +8 -0
  84. package/dist/client/router/use-sync-stream.d.ts +16 -0
  85. package/dist/client/server-error.d.ts +4 -7
  86. package/dist/client/{router/sync-catch-up.d.ts → sync-catch-up.d.ts} +7 -4
  87. package/dist/client/sync-client.d.ts +74 -0
  88. package/dist/client/sync.d.ts +43 -30
  89. package/dist/client.d.ts +3 -1
  90. package/dist/client.js +775 -937
  91. package/dist/config.d.ts +17 -13
  92. package/dist/config.js +3 -31
  93. package/dist/define-route.d.ts +262 -517
  94. package/dist/devtools/protocol.d.ts +4 -3
  95. package/dist/furin.d.ts +42 -31
  96. package/dist/furin.js +21464 -19101
  97. package/dist/plugin/binding-scope.d.ts +15 -2
  98. package/dist/plugin/client-plugin.d.ts +19 -0
  99. package/dist/plugin/dead-code-elimination.d.ts +14 -3
  100. package/dist/plugin/dev-routes.d.ts +22 -0
  101. package/dist/plugin/hmr-data-signature.d.ts +3 -0
  102. package/dist/plugin/hmr-hook-signature.d.ts +9 -0
  103. package/dist/plugin/index.d.ts +1 -6
  104. package/dist/plugin/index.js +1757 -1218
  105. package/dist/plugin/module-bindings.d.ts +11 -0
  106. package/dist/plugin/module-transform.d.ts +29 -0
  107. package/dist/plugin/preflight.d.ts +8 -0
  108. package/dist/plugin/route-chains.d.ts +35 -0
  109. package/dist/plugin/route-config.d.ts +28 -0
  110. package/dist/plugin/route-modules.d.ts +35 -0
  111. package/dist/plugin/route-registry.d.ts +32 -0
  112. package/dist/plugin/route-tree.d.ts +43 -0
  113. package/dist/plugin/strip-server-stages.d.ts +10 -0
  114. package/dist/plugin/strip-server.d.ts +10 -0
  115. package/dist/plugin/transform-client-module.d.ts +3 -0
  116. package/dist/plugin/transform-client.d.ts +0 -2
  117. package/dist/plugin/transform-isomorphic.d.ts +3 -2
  118. package/dist/routes.d.ts +15 -5
  119. package/dist/rsc/build/client-references.d.ts +8 -0
  120. package/dist/rsc/build/discover.d.ts +7 -0
  121. package/dist/rsc/build/flight-loader.d.ts +1 -0
  122. package/dist/rsc/build/index.d.ts +8 -4
  123. package/dist/rsc/build/paths.d.ts +2 -0
  124. package/dist/rsc/bundled-client-codec.d.ts +1 -0
  125. package/dist/rsc/client-codec.d.ts +3 -0
  126. package/dist/rsc/client-references.d.ts +22 -0
  127. package/dist/rsc/server-client-codec.d.ts +1 -0
  128. package/dist/rsc/server-codec.js +3889 -2
  129. package/dist/rsc/slot-proxy.d.ts +11 -0
  130. package/dist/rsc/transport-benchmark.d.ts +2 -1
  131. package/dist/rsc-client.js +16 -164
  132. package/dist/rsc-server.d.ts +1 -5
  133. package/dist/rsc.d.ts +1 -5
  134. package/dist/rsc.js +233 -73
  135. package/dist/server/assets/client-dir.d.ts +1 -0
  136. package/dist/server/auto-invalidate/index.d.ts +2 -2
  137. package/dist/server/auto-invalidate/plugin.d.ts +21 -15
  138. package/dist/server/auto-invalidate/types.d.ts +0 -2
  139. package/dist/server/bounded-body.d.ts +6 -0
  140. package/dist/server/browser-events/plugin.d.ts +4 -1
  141. package/dist/server/browser-events/shutdown.d.ts +1 -1
  142. package/dist/server/cache/dev-loader.d.ts +6 -1
  143. package/dist/server/cache/index.d.ts +4 -9
  144. package/dist/server/cache/invalidation.d.ts +7 -2
  145. package/dist/server/cache/isr-ssg.d.ts +6 -1
  146. package/dist/server/cache/page-cache.d.ts +5 -0
  147. package/dist/server/cache/purger.d.ts +14 -4
  148. package/dist/server/cache/redis/index.d.ts +1 -34
  149. package/dist/server/cache/redis/index.js +51 -25
  150. package/dist/server/cache/redis/scripts.d.ts +1 -1
  151. package/dist/server/cache/route-cache.d.ts +18 -2
  152. package/dist/server/cache/runtime-cache.d.ts +7 -0
  153. package/dist/server/context-logger.d.ts +6 -1
  154. package/dist/server/dev/graph.d.ts +4 -3
  155. package/dist/server/dev/runtime.d.ts +4 -0
  156. package/dist/server/dev/runtime.production.d.ts +1 -0
  157. package/dist/server/dev/server-only-module.d.ts +5 -0
  158. package/dist/server/dev/stack-location.d.ts +9 -0
  159. package/dist/server/dev-page-imports.d.ts +42 -0
  160. package/dist/server/dev-page-plugin.d.ts +2 -49
  161. package/dist/server/devtools/instrumentation.d.ts +1 -3
  162. package/dist/server/devtools/instrumentation.production.d.ts +0 -2
  163. package/dist/server/devtools/request-context.d.ts +7 -0
  164. package/dist/server/evlog.d.ts +18 -6
  165. package/dist/server/instance.d.ts +52 -44
  166. package/dist/server/internal.d.ts +51 -14
  167. package/dist/server/internal.js +9852 -2088
  168. package/dist/server/lang-detect.d.ts +4 -4
  169. package/dist/server/logging/ingest-plugin.d.ts +31 -0
  170. package/dist/server/mode.d.ts +17 -0
  171. package/dist/server/prerender.d.ts +32 -0
  172. package/dist/server/prod/runtime.d.ts +5 -0
  173. package/dist/server/production-server.js +1000 -731
  174. package/dist/server/registrations.d.ts +14 -0
  175. package/dist/server/render/assemble.d.ts +0 -22
  176. package/dist/server/render/buffered-render.d.ts +20 -0
  177. package/dist/server/render/document-stream.d.ts +45 -0
  178. package/dist/server/render/document.d.ts +1 -1
  179. package/dist/server/render/element.d.ts +2 -5
  180. package/dist/server/render/index.d.ts +4 -5
  181. package/dist/server/render/index.ts +4 -21
  182. package/dist/server/render/isr-revalidation.d.ts +5 -0
  183. package/dist/server/render/isr-shared-cache.d.ts +23 -0
  184. package/dist/server/render/isr.d.ts +19 -1
  185. package/dist/server/render/loader-errors.d.ts +33 -0
  186. package/dist/server/render/loader-segments.d.ts +40 -0
  187. package/dist/server/render/loaders.d.ts +16 -32
  188. package/dist/server/render/mixed-cache.d.ts +2 -2
  189. package/dist/server/render/navigation-head.d.ts +13 -0
  190. package/dist/server/render/not-found-plugin.d.ts +18 -0
  191. package/dist/server/render/observe-loader.d.ts +1 -0
  192. package/dist/server/render/ppr-document.d.ts +1 -1
  193. package/dist/server/render/ppr-request.d.ts +0 -2
  194. package/dist/server/render/ppr-route.d.ts +13 -2
  195. package/dist/server/render/prepare-render.d.ts +57 -0
  196. package/dist/server/render/request-loaders.d.ts +19 -0
  197. package/dist/server/render/route-frame-transport.d.ts +7 -1
  198. package/dist/server/render/shell.d.ts +1 -1
  199. package/dist/server/render/shell.ts +1 -1
  200. package/dist/server/render/ssr-transport.d.ts +11 -0
  201. package/dist/server/render/ssr.d.ts +1 -116
  202. package/dist/server/render/static-params.d.ts +15 -0
  203. package/dist/server/router/discovery.d.ts +1 -0
  204. package/dist/server/router/dispatch.d.ts +96 -0
  205. package/dist/server/router/hmr.d.ts +14 -24
  206. package/dist/server/router/patterns.d.ts +15 -20
  207. package/dist/server/router/plugin.d.ts +26 -24
  208. package/dist/server/router/schemas.d.ts +6 -24
  209. package/dist/server/router/types.d.ts +0 -2
  210. package/dist/server/runtime-env.d.ts +6 -1
  211. package/dist/server/server-entry.d.ts +5 -0
  212. package/dist/server/server-entry.js +36 -0
  213. package/dist/server/static-export.d.ts +38 -0
  214. package/dist/server/static-export.js +10777 -0
  215. package/dist/server/sync/adapter.d.ts +13 -1
  216. package/dist/server/sync/atomic.d.ts +0 -2
  217. package/dist/server/sync/config.d.ts +7 -1
  218. package/dist/server/sync/connection.d.ts +13 -0
  219. package/dist/server/sync/drizzle/index.js +113 -68
  220. package/dist/server/sync/fingerprint.d.ts +0 -1
  221. package/dist/server/sync/index.d.ts +5 -6
  222. package/dist/server/sync/index.js +2763 -1320
  223. package/dist/server/sync/invalidate-sync.d.ts +14 -0
  224. package/dist/server/sync/local-client.d.ts +9 -0
  225. package/dist/server/sync/plugin.d.ts +18 -55
  226. package/dist/server/sync/postgres/execute.d.ts +2 -2
  227. package/dist/server/sync/postgres/index.d.ts +3 -1
  228. package/dist/server/sync/postgres/index.js +68 -37
  229. package/dist/server/sync/postgres/migration.sql +2 -0
  230. package/dist/server/sync/principal-journal.d.ts +11 -0
  231. package/dist/server/sync/prisma/index.js +74 -39
  232. package/dist/server/sync/queries.d.ts +57 -16
  233. package/dist/server/sync/redis/index.d.ts +2 -1
  234. package/dist/server/sync/redis/index.js +50 -5
  235. package/dist/server/sync/redis/scripts.d.ts +2 -1
  236. package/dist/server/sync/response.d.ts +3 -0
  237. package/dist/server/sync/runtime.d.ts +8 -1
  238. package/dist/server/sync/sqlite/index.d.ts +3 -1
  239. package/dist/server/sync/sqlite/index.js +59 -27
  240. package/dist/server/sync/stream.d.ts +32 -7
  241. package/dist/server/weak-registry.d.ts +9 -0
  242. package/dist/shared/await.d.ts +2 -1
  243. package/dist/shared/browser-events.d.ts +4 -0
  244. package/dist/shared/compact-json.d.ts +1 -0
  245. package/dist/shared/defer.d.ts +1 -1
  246. package/dist/shared/deferred-ndjson.d.ts +3 -2
  247. package/dist/shared/elysia-contract.d.ts +2 -0
  248. package/dist/shared/in-memory-apps.d.ts +7 -0
  249. package/dist/shared/invalidation-header.d.ts +13 -0
  250. package/dist/shared/navigation-data.d.ts +7 -0
  251. package/dist/shared/prefix.d.ts +11 -5
  252. package/dist/shared/route-frame.d.ts +3 -0
  253. package/dist/shared/route-map.d.ts +7 -0
  254. package/dist/shared/route-matches.d.ts +41 -0
  255. package/dist/shared/route-specificity.d.ts +2 -0
  256. package/dist/shared/search-params.d.ts +2 -3
  257. package/dist/shared/sync-query.d.ts +6 -13
  258. package/dist/shared/utils/ast-walk.d.ts +3 -11
  259. package/dist/sync-client.d.ts +3 -0
  260. package/dist/sync-client.js +1600 -0
  261. package/package.json +39 -23
  262. package/src/adapter/README.md +43 -15
  263. package/src/adapter/build.ts +270 -0
  264. package/src/adapter/bun/app.ts +5 -0
  265. package/src/adapter/bun/server.ts +14 -0
  266. package/src/adapter/bun.ts +104 -264
  267. package/src/adapter/furin-app.d.ts +15 -0
  268. package/src/adapter/package.ts +32 -27
  269. package/src/adapter/prerender.ts +97 -0
  270. package/src/adapter/runtime-build.ts +98 -102
  271. package/src/adapter/server-bundle.ts +110 -0
  272. package/src/adapter/static.ts +276 -260
  273. package/src/adapter/types.ts +89 -0
  274. package/src/adapter/vercel/bootstrap.ts +85 -0
  275. package/src/adapter/vercel/data.ts +21 -0
  276. package/src/adapter/vercel/handler.ts +132 -0
  277. package/src/adapter/vercel.ts +329 -659
  278. package/src/build/bun-build.ts +3 -0
  279. package/src/build/client.ts +27 -63
  280. package/src/build/compile-entry.ts +1 -0
  281. package/src/build/discover-mounts-process.ts +17 -0
  282. package/src/build/discover-mounts.ts +40 -0
  283. package/src/build/discover-pages-process.ts +151 -0
  284. package/src/build/discover-pages.ts +69 -0
  285. package/src/build/discovery-module.ts +70 -0
  286. package/src/build/discovery-plugins.ts +101 -0
  287. package/src/build/elysia-aot-worker.ts +92 -9
  288. package/src/build/elysia-aot.ts +164 -70
  289. package/src/build/entry-template.ts +39 -19
  290. package/src/build/hydrate.ts +131 -50
  291. package/src/build/index.ts +136 -132
  292. package/src/build/preload-manifest.ts +3 -3
  293. package/src/build/private-server-sourcemaps.ts +5 -0
  294. package/src/build/production-instrumentation.ts +22 -19
  295. package/src/build/route-metadata.ts +90 -0
  296. package/src/build/route-types.ts +93 -11
  297. package/src/build/server-app-entry.ts +4 -21
  298. package/src/build/shared.ts +15 -49
  299. package/src/build/types.ts +23 -20
  300. package/src/build/virtual-entry.ts +2 -4
  301. package/src/cli/config.ts +137 -6
  302. package/src/cli/dev-worker.ts +123 -0
  303. package/src/cli/dev.ts +91 -0
  304. package/src/cli/index.ts +82 -21
  305. package/src/cli/preview.ts +2 -1
  306. package/src/client/boundaries.tsx +5 -14
  307. package/src/client/browser-events-runtime.ts +97 -23
  308. package/src/client/browser-events.ts +8 -0
  309. package/src/client/default-screens.tsx +2 -2
  310. package/src/client/define-route.ts +3 -2
  311. package/src/client/dev-error-overlay.ts +18 -1
  312. package/src/client/hmr.ts +1 -1
  313. package/src/client/internal/runtime-types.ts +7 -1
  314. package/src/client/link.tsx +80 -43
  315. package/src/client/mutation.ts +98 -3
  316. package/src/client/query-store.ts +213 -31
  317. package/src/client/query.tsx +12 -29
  318. package/src/client/request-id.ts +10 -0
  319. package/src/client/route-api.tsx +6 -6
  320. package/src/client/route-element.tsx +155 -0
  321. package/src/client/router/boundary-tree.tsx +2 -93
  322. package/src/client/router/cache-generation.ts +37 -0
  323. package/src/client/router/context.ts +40 -4
  324. package/src/client/router/fetch-observers.ts +46 -0
  325. package/src/client/router/hmr-refresh.ts +50 -0
  326. package/src/client/router/index.ts +7 -29
  327. package/src/client/router/link-utils.ts +19 -31
  328. package/src/client/router/navigation-controller.ts +580 -0
  329. package/src/client/router/navigation.ts +3 -2
  330. package/src/client/router/optimistic.ts +2 -3
  331. package/src/client/router/page-fetch.ts +272 -0
  332. package/src/client/router/provider.tsx +227 -1079
  333. package/src/client/router/search/index.ts +38 -14
  334. package/src/client/router/spa-response.ts +19 -70
  335. package/src/client/router/transport.ts +23 -0
  336. package/src/client/router/types.ts +203 -61
  337. package/src/client/router/use-fetch-interception.ts +52 -0
  338. package/src/client/router/use-history-sync.ts +56 -0
  339. package/src/client/router/use-hmr-refresh.ts +51 -0
  340. package/src/client/router/use-sync-stream.ts +181 -0
  341. package/src/client/server-error.ts +2 -40
  342. package/src/client/sync-catch-up.ts +194 -0
  343. package/src/client/sync-client.ts +528 -0
  344. package/src/client/sync.ts +275 -378
  345. package/src/client.ts +10 -5
  346. package/src/config.ts +34 -49
  347. package/src/define-route.ts +487 -882
  348. package/src/devtools/devtools-element.js +10 -3
  349. package/src/devtools/protocol.ts +3 -3
  350. package/src/furin.ts +258 -1086
  351. package/src/plugin/binding-scope.ts +219 -94
  352. package/src/plugin/client-plugin.ts +145 -0
  353. package/src/plugin/dead-code-elimination.ts +390 -111
  354. package/src/plugin/dev-routes.ts +453 -0
  355. package/src/plugin/hmr-data-signature.ts +161 -0
  356. package/src/plugin/hmr-dependencies.ts +2 -4
  357. package/src/plugin/hmr-hook-signature.ts +252 -0
  358. package/src/plugin/index.ts +3 -96
  359. package/src/plugin/module-bindings.ts +216 -0
  360. package/src/plugin/module-transform.ts +63 -0
  361. package/src/plugin/preflight.ts +66 -0
  362. package/src/plugin/route-chains.ts +309 -0
  363. package/src/plugin/route-config.ts +451 -0
  364. package/src/plugin/route-modules.ts +209 -0
  365. package/src/plugin/route-registry.ts +362 -0
  366. package/src/plugin/route-tree.ts +248 -0
  367. package/src/plugin/strip-server-stages.ts +197 -0
  368. package/src/plugin/strip-server.ts +22 -0
  369. package/src/plugin/transform-client-module.ts +36 -31
  370. package/src/plugin/transform-client.ts +33 -1114
  371. package/src/plugin/transform-dev-page.ts +84 -92
  372. package/src/plugin/transform-isomorphic.ts +396 -185
  373. package/src/routes.ts +15 -5
  374. package/src/rsc/build/client-references.ts +267 -0
  375. package/src/rsc/build/discover.ts +226 -0
  376. package/src/rsc/build/flight-loader.ts +69 -0
  377. package/src/rsc/build/index.ts +59 -34
  378. package/src/rsc/build/paths.ts +7 -0
  379. package/src/rsc/bundled-client-codec.ts +4 -0
  380. package/src/rsc/client-codec.ts +12 -3
  381. package/src/rsc/client-references.ts +138 -0
  382. package/src/rsc/codec.ts +30 -21
  383. package/src/rsc/flight-drain.ts +6 -29
  384. package/src/rsc/react-server-dom-webpack.d.ts +1 -1
  385. package/src/rsc/server-client-codec.ts +14 -0
  386. package/src/rsc/server-codec.ts +5 -8
  387. package/src/rsc/slot-proxy.ts +34 -0
  388. package/src/rsc/transport-benchmark.ts +1 -1
  389. package/src/rsc-server.tsx +5 -23
  390. package/src/rsc.tsx +5 -29
  391. package/src/server/assets/client-dir.ts +110 -0
  392. package/src/server/assets/production.ts +1 -1
  393. package/src/server/auto-invalidate/index.ts +2 -7
  394. package/src/server/auto-invalidate/plugin.ts +43 -15
  395. package/src/server/auto-invalidate/registry.ts +6 -6
  396. package/src/server/auto-invalidate/runtime.ts +16 -98
  397. package/src/server/auto-invalidate/types.ts +0 -6
  398. package/src/server/bounded-body.ts +38 -0
  399. package/src/server/browser-events/plugin.ts +167 -26
  400. package/src/server/browser-events/shutdown.ts +28 -9
  401. package/src/server/cache/dev-loader.ts +30 -16
  402. package/src/server/cache/index.ts +2 -15
  403. package/src/server/cache/invalidation.ts +27 -46
  404. package/src/server/cache/isr-ssg.ts +6 -1
  405. package/src/server/cache/isr.ts +11 -11
  406. package/src/server/cache/page-cache-state.ts +2 -2
  407. package/src/server/cache/page-cache.ts +13 -9
  408. package/src/server/cache/purger.ts +64 -8
  409. package/src/server/cache/redis/index.ts +17 -27
  410. package/src/server/cache/redis/scripts.ts +21 -4
  411. package/src/server/cache/registry.ts +3 -3
  412. package/src/server/cache/route-cache.ts +47 -13
  413. package/src/server/cache/runtime-cache.ts +144 -24
  414. package/src/server/cache/ssg.ts +7 -5
  415. package/src/server/context-logger.ts +46 -8
  416. package/src/server/dev/diagnostics.ts +6 -56
  417. package/src/server/dev/error.ts +3 -34
  418. package/src/server/dev/graph.ts +35 -22
  419. package/src/server/dev/rewrite-module-specifiers.ts +16 -19
  420. package/src/server/dev/runtime.production.ts +4 -0
  421. package/src/server/dev/runtime.ts +404 -0
  422. package/src/server/dev/server-only-module.ts +23 -0
  423. package/src/server/dev/stack-location.ts +46 -0
  424. package/src/server/dev/symbolicate.ts +4 -3
  425. package/src/server/dev-page-imports.ts +185 -0
  426. package/src/server/dev-page-plugin.ts +57 -303
  427. package/src/server/devtools/hub.ts +3 -3
  428. package/src/server/devtools/instrumentation.production.ts +0 -8
  429. package/src/server/devtools/instrumentation.ts +6 -8
  430. package/src/server/devtools/plugin.ts +2 -1
  431. package/src/server/devtools/request-context.ts +67 -16
  432. package/src/server/evlog.ts +119 -55
  433. package/src/server/instance.ts +187 -138
  434. package/src/server/internal.ts +66 -62
  435. package/src/server/lang-detect.ts +31 -22
  436. package/src/server/logging/ingest-plugin.ts +117 -0
  437. package/src/server/mode.ts +19 -0
  438. package/src/server/prerender.ts +122 -0
  439. package/src/server/prod/runtime.ts +181 -0
  440. package/src/server/production-server.ts +43 -17
  441. package/src/server/registrations.ts +38 -0
  442. package/src/server/render/assemble.ts +5 -8
  443. package/src/server/render/buffered-render.ts +171 -0
  444. package/src/server/render/document-stream.ts +188 -0
  445. package/src/server/render/document.tsx +5 -3
  446. package/src/server/render/element.tsx +6 -73
  447. package/src/server/render/index.ts +4 -21
  448. package/src/server/render/isr-revalidation.ts +246 -0
  449. package/src/server/render/isr-shared-cache.ts +141 -0
  450. package/src/server/render/isr.ts +212 -516
  451. package/src/server/render/loader-errors.ts +183 -0
  452. package/src/server/render/loader-segments.ts +149 -0
  453. package/src/server/render/loaders.ts +126 -547
  454. package/src/server/render/mixed-cache.ts +28 -27
  455. package/src/server/render/navigation-head.ts +33 -0
  456. package/src/server/render/not-found-plugin.ts +31 -0
  457. package/src/server/render/not-found.ts +32 -8
  458. package/src/server/render/observe-loader.ts +46 -0
  459. package/src/server/render/ppr-document.ts +36 -14
  460. package/src/server/render/ppr-request.ts +2 -2
  461. package/src/server/render/ppr-route.ts +79 -18
  462. package/src/server/render/prepare-render.ts +251 -0
  463. package/src/server/render/request-loaders.ts +216 -0
  464. package/src/server/render/route-frame-transport.ts +41 -4
  465. package/src/server/render/shell.ts +1 -1
  466. package/src/server/render/ssg.ts +9 -20
  467. package/src/server/render/ssr-transport.ts +221 -0
  468. package/src/server/render/ssr.ts +95 -956
  469. package/src/server/render/static-params.ts +32 -0
  470. package/src/server/render/template.ts +7 -7
  471. package/src/server/router/discovery.ts +82 -159
  472. package/src/server/router/dispatch.ts +337 -0
  473. package/src/server/router/hmr.ts +177 -190
  474. package/src/server/router/patterns.ts +39 -65
  475. package/src/server/router/plugin.ts +234 -359
  476. package/src/server/router/schemas.ts +6 -39
  477. package/src/server/router/source-version.ts +5 -4
  478. package/src/server/router/types.ts +0 -2
  479. package/src/server/runtime-env.ts +6 -1
  480. package/src/server/server-entry.ts +39 -0
  481. package/src/server/static-export.ts +143 -0
  482. package/src/server/sync/adapter.ts +16 -1
  483. package/src/server/sync/atomic.ts +16 -36
  484. package/src/server/sync/config.ts +12 -14
  485. package/src/server/sync/connection.ts +71 -0
  486. package/src/server/sync/drizzle/index.ts +12 -20
  487. package/src/server/sync/fingerprint.ts +1 -1
  488. package/src/server/sync/index.ts +5 -10
  489. package/src/server/sync/invalidate-sync.ts +56 -0
  490. package/src/server/sync/local-client.ts +84 -0
  491. package/src/server/sync/mutation-handler.ts +6 -0
  492. package/src/server/sync/plugin.ts +193 -144
  493. package/src/server/sync/postgres/execute.ts +9 -1
  494. package/src/server/sync/postgres/index.ts +78 -39
  495. package/src/server/sync/postgres/migration.sql +2 -0
  496. package/src/server/sync/principal-journal.ts +78 -0
  497. package/src/server/sync/prisma/index.ts +1 -0
  498. package/src/server/sync/queries.ts +133 -40
  499. package/src/server/sync/query-context.ts +38 -5
  500. package/src/server/sync/redis/index.ts +38 -6
  501. package/src/server/sync/redis/scripts.ts +10 -1
  502. package/src/server/sync/response.ts +47 -67
  503. package/src/server/sync/runtime.ts +74 -19
  504. package/src/server/sync/sqlite/index.ts +96 -61
  505. package/src/server/sync/sqlite/migration.sql +1 -0
  506. package/src/server/sync/stream.ts +62 -29
  507. package/src/server/sync/validation.ts +1 -1
  508. package/src/server/weak-registry.ts +37 -0
  509. package/src/shared/await.tsx +2 -1
  510. package/src/shared/browser-events.ts +1 -1
  511. package/src/shared/compact-json.ts +1 -1
  512. package/src/shared/defer.ts +1 -1
  513. package/src/shared/deferred-ndjson.ts +142 -120
  514. package/src/shared/elysia-contract.ts +7 -0
  515. package/src/shared/error.ts +1 -4
  516. package/src/shared/in-memory-apps.ts +24 -0
  517. package/src/shared/invalidation-header.ts +38 -0
  518. package/src/shared/navigation-data.ts +29 -0
  519. package/src/shared/prefix.ts +42 -6
  520. package/src/shared/route-frame.ts +82 -26
  521. package/src/shared/route-map.ts +23 -39
  522. package/src/shared/route-matches.ts +59 -0
  523. package/src/shared/route-specificity.ts +28 -0
  524. package/src/shared/search-params.ts +6 -35
  525. package/src/shared/sync-query.ts +30 -23
  526. package/src/shared/utils/ast-walk.ts +9 -25
  527. package/src/shared/utils/index.ts +1 -1
  528. package/src/sync-client.ts +14 -0
  529. package/dist/build/scan-server.d.ts +0 -14
  530. package/dist/build/ssg-cache.d.ts +0 -18
  531. package/dist/plugin/route-config-autofix.d.ts +0 -31
  532. package/dist/plugin/routes.d.ts +0 -34
  533. package/dist/server/logger.d.ts +0 -4
  534. package/dist/server/router/hmr.production.d.ts +0 -7
  535. package/src/build/scan-server.ts +0 -112
  536. package/src/build/ssg-cache.ts +0 -126
  537. package/src/client/router/sync-catch-up.ts +0 -120
  538. package/src/plugin/route-config-autofix.ts +0 -1149
  539. package/src/plugin/routes.ts +0 -1140
  540. package/src/server/logger.ts +0 -21
  541. package/src/server/router/hmr.production.ts +0 -18
@@ -1,1149 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs";
2
- import { extname, join, relative, resolve } from "node:path";
3
- import MagicString from "magic-string";
4
- import { walk } from "yuku-ast";
5
- import type { ImportDeclaration, Program } from "yuku-parser";
6
- import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
7
- import { parseDynamicRouteSegment } from "../server/router/patterns.ts";
8
- import { parseSource } from "../shared/parser.ts";
9
- import type { AstNode } from "../shared/utils/ast-walk.ts";
10
-
11
- /**
12
- * Dev-time auto-fix for `config({ layout })`, mirroring how TanStack Router's
13
- * generator rewrites `createFileRoute` ids: the watcher scans route files,
14
- * computes the expected ancestor layout from the file system, and rewrites
15
- * the config object (plus the import) when the declared reference is missing
16
- * or points at the wrong module. Content edits never retrigger the watcher,
17
- * so the rewrite cannot loop; the dev renderer re-reads the file on the next
18
- * request through its cache-busting import.
19
- */
20
-
21
- const DIRECTORY_SPLIT_RE = /[\\/]/;
22
- const NON_ALPHANUMERIC_RE = /[^a-zA-Z0-9]/g;
23
-
24
- export interface ExpectedLayout {
25
- /** Preferred local binding, e.g. "rootRoute". */
26
- identifier: string;
27
- /** Import specifier relative from the route file, e.g. "../root". */
28
- importPath: string;
29
- }
30
-
31
- export type LayoutProbe = (path: string) => boolean;
32
-
33
- function routeConfigError(filePath: string, message: string): Error {
34
- return new Error(`[furin] ${filePath}: ${message}`);
35
- }
36
-
37
- function asAstNode(node: unknown): AstNode | null {
38
- if (!node || typeof node !== "object" || !("type" in node)) {
39
- return null;
40
- }
41
- return unwrapTSExpression(node as { type: string }) as AstNode;
42
- }
43
-
44
- function propertyName(node: unknown): string | null {
45
- const ast = asAstNode(node);
46
- if (ast?.type === "Identifier" && typeof ast.name === "string") {
47
- return ast.name;
48
- }
49
- if (ast?.type === "Literal" && typeof ast.value === "string") {
50
- return ast.value;
51
- }
52
- return null;
53
- }
54
-
55
- const WITHOUT_EXTENSION_RE = /\.(ts|tsx|jsx|js|mts|cts)$/;
56
- const NESTED_LAYOUT_FILE_NAMES = ["_route.tsx", "_route.ts", "_route.jsx", "_route.js"];
57
- const ROOT_LAYOUT_FILE_NAMES = ["root.tsx", "root.ts"];
58
-
59
- function withoutExtension(path: string): string {
60
- return path.replace(WITHOUT_EXTENSION_RE, "");
61
- }
62
-
63
- function toImportSpecifier(fromFile: string, toFile: string): string {
64
- const specifier = withoutExtension(relative(resolve(fromFile, ".."), toFile)).replaceAll(
65
- "\\",
66
- "/"
67
- );
68
- return specifier.startsWith(".") ? specifier : `./${specifier}`;
69
- }
70
-
71
- /** Deterministic binding for a layout file: `root.tsx` → `rootRoute`, `board/_route.tsx` → `boardRoute`. */
72
- export function layoutIdentifierFor(layoutPath: string): string {
73
- const extension = extname(layoutPath);
74
- const stem = layoutPath.slice(0, layoutPath.length - extension.length);
75
- const base = stem.split(DIRECTORY_SPLIT_RE).pop() ?? "";
76
- const source =
77
- base === "_route" ? (resolve(layoutPath, "..").split(DIRECTORY_SPLIT_RE).pop() ?? "") : base;
78
- const name = (source || "root").replace(NON_ALPHANUMERIC_RE, "");
79
- return `${name || "root"}Route`;
80
- }
81
-
82
- /**
83
- * The layout a route file should reference: the nearest `_route.ts(x)` walking
84
- * up from its own directory (a `_route` file looks at its PARENT directory),
85
- * falling back to the `root.tsx` convention at the pages root. Returns null
86
- * for the root layout itself — nothing above it exists.
87
- */
88
- export function expectedLayoutFor(
89
- filePath: string,
90
- pagesDir: string,
91
- probe: LayoutProbe = existsSync
92
- ): ExpectedLayout | null {
93
- const pages = resolve(pagesDir);
94
- const file = resolve(filePath);
95
- const directory = resolve(file, "..");
96
- const extension = extname(file);
97
- const stem = file.slice(0, file.length - extension.length);
98
- if (stem === join(pages, "root")) {
99
- return null; // the root layout references nothing above it
100
- }
101
- const isLayoutFile = stem === join(directory, "_route");
102
-
103
- let current = directory;
104
- while (current.startsWith(pages) && current !== pages) {
105
- if (!(isLayoutFile && current === directory)) {
106
- for (const candidate of NESTED_LAYOUT_FILE_NAMES) {
107
- const layoutPath = join(current, candidate);
108
- if (probe(layoutPath)) {
109
- return {
110
- identifier: layoutIdentifierFor(layoutPath),
111
- importPath: toImportSpecifier(file, layoutPath),
112
- };
113
- }
114
- }
115
- }
116
- current = resolve(current, "..");
117
- }
118
-
119
- for (const candidate of ROOT_LAYOUT_FILE_NAMES) {
120
- const layoutPath = join(pages, candidate);
121
- if (probe(layoutPath)) {
122
- return {
123
- identifier: layoutIdentifierFor(layoutPath),
124
- importPath: toImportSpecifier(file, layoutPath),
125
- };
126
- }
127
- }
128
- return null;
129
- }
130
-
131
- interface LayoutImport {
132
- binding: string;
133
- resolvedPath: string;
134
- }
135
-
136
- interface CollectedImports {
137
- /** Every local import binding (collision detection for injected imports). */
138
- allBindings: Set<string>;
139
- imports: LayoutImport[];
140
- /** End offset of the last import statement (import insertion point). */
141
- lastImportEnd: number;
142
- /** Local binding of the `t` TypeBox import, when present. */
143
- tBinding: string | null;
144
- }
145
-
146
- const LEADING_SLASH_RE = /^\//;
147
-
148
- function collectLayoutImports(program: Program, filePath: string): CollectedImports {
149
- const imports: LayoutImport[] = [];
150
- const allBindings = new Set<string>();
151
- let lastImportEnd = 0;
152
- let tBinding: string | null = null;
153
- for (const statement of program.body) {
154
- if (statement.type !== "ImportDeclaration") {
155
- continue;
156
- }
157
- const declaration = statement as unknown as ImportDeclaration;
158
- if (declaration.importKind === "type") {
159
- continue;
160
- }
161
- const end = typeof declaration.end === "number" ? declaration.end : 0;
162
- if (end > lastImportEnd) {
163
- lastImportEnd = end;
164
- }
165
- const collected = collectImportBinding(declaration, filePath, imports, allBindings);
166
- if (collected !== null && tBinding === null) {
167
- tBinding = collected;
168
- }
169
- }
170
- return { allBindings, imports, lastImportEnd, tBinding };
171
- }
172
-
173
- function collectImportBinding(
174
- declaration: ImportDeclaration,
175
- filePath: string,
176
- imports: LayoutImport[],
177
- allBindings: Set<string>
178
- ): string | null {
179
- const specifierValue = declaration.source.value;
180
- if (typeof specifierValue !== "string") {
181
- return null;
182
- }
183
- const resolvedPath = withoutExtension(resolve(filePath, "..", specifierValue));
184
- let tBinding: string | null = null;
185
- for (const specifier of declaration.specifiers as unknown as AstNode[]) {
186
- if (specifier.type !== "ImportSpecifier") {
187
- continue;
188
- }
189
- const imported = asAstNode(specifier.imported);
190
- const local = asAstNode(specifier.local);
191
- if (
192
- imported?.type !== "Identifier" ||
193
- typeof imported.name !== "string" ||
194
- local?.type !== "Identifier" ||
195
- typeof local.name !== "string"
196
- ) {
197
- continue;
198
- }
199
- allBindings.add(local.name);
200
- if (imported.name === "route") {
201
- imports.push({ binding: local.name, resolvedPath });
202
- }
203
- if (imported.name === "t" && specifierValue === "elysia") {
204
- tBinding = local.name;
205
- }
206
- }
207
- return tBinding;
208
- }
209
-
210
- function belongsToBuilderChain(node: unknown, bindings: BuilderBindings): boolean {
211
- const expression = asAstNode(node);
212
- if (expression?.type !== "CallExpression") {
213
- return false;
214
- }
215
- const callee = asAstNode(expression.callee);
216
- if (callee?.type === "Identifier" && typeof callee.name === "string") {
217
- return bindings.rootBindings.has(callee.name) || bindings.routeBindings.has(callee.name);
218
- }
219
- if (callee?.type !== "MemberExpression") {
220
- return false;
221
- }
222
- return belongsToBuilderChain(callee.object, bindings);
223
- }
224
-
225
- function collectConfigObjects(program: Program, bindings: BuilderBindings): AstNode[] {
226
- const objects: AstNode[] = [];
227
- walk(program as never, {
228
- CallExpression(call) {
229
- const callee = asAstNode(call.callee);
230
- if (callee?.type !== "MemberExpression" || typeof callee.property !== "object") {
231
- return;
232
- }
233
- const property = asAstNode(callee.property);
234
- if (property?.type !== "Identifier" || property.name !== "config") {
235
- return;
236
- }
237
- if (!belongsToBuilderChain(callee.object, bindings)) {
238
- return;
239
- }
240
- const argument = Array.isArray(call.arguments) ? asAstNode(call.arguments[0]) : null;
241
- if (argument?.type === "ObjectExpression") {
242
- objects.push(argument);
243
- }
244
- },
245
- });
246
- return objects;
247
- }
248
-
249
- function builderChainHasMethod(
250
- program: Program,
251
- bindings: BuilderBindings,
252
- methodName: string
253
- ): boolean {
254
- let found = false;
255
- walk(program as never, {
256
- CallExpression(call) {
257
- const callee = asAstNode(call.callee);
258
- if (callee?.type !== "MemberExpression") {
259
- return;
260
- }
261
- const property = asAstNode(callee.property);
262
- if (
263
- property?.type === "Identifier" &&
264
- property.name === methodName &&
265
- belongsToBuilderChain(callee.object, bindings)
266
- ) {
267
- found = true;
268
- }
269
- },
270
- });
271
- return found;
272
- }
273
-
274
- interface BuilderBindings {
275
- documentBindings: Set<string>;
276
- rootBindings: Set<string>;
277
- routeBindings: Set<string>;
278
- routeSpecifiers: Map<string, { end: number; start: number }>;
279
- }
280
-
281
- const FURIN_BUILDER_MODULES = new Set([
282
- "@teyik0/furin",
283
- "furin",
284
- "@teyik0/furin/client",
285
- "furin/client",
286
- ]);
287
-
288
- function collectDocumentBinding(
289
- importedName: string,
290
- localName: string,
291
- bindings: BuilderBindings
292
- ): void {
293
- if (
294
- (importedName === "HeadContent" || importedName === "Scripts") &&
295
- localName === importedName
296
- ) {
297
- bindings.documentBindings.add(importedName);
298
- }
299
- }
300
-
301
- function collectBindingsFromDeclaration(
302
- declaration: ImportDeclaration,
303
- bindings: BuilderBindings
304
- ): void {
305
- const specifierValue = declaration.source.value;
306
- if (typeof specifierValue !== "string" || !FURIN_BUILDER_MODULES.has(specifierValue)) {
307
- return;
308
- }
309
- for (const specifier of declaration.specifiers as unknown as AstNode[]) {
310
- if (specifier.type !== "ImportSpecifier") {
311
- continue;
312
- }
313
- const imported = asAstNode(specifier.imported);
314
- const local = asAstNode(specifier.local);
315
- if (imported?.type !== "Identifier" || typeof imported.name !== "string") {
316
- continue;
317
- }
318
- if (local?.type !== "Identifier" || typeof local.name !== "string") {
319
- continue;
320
- }
321
- if (imported.name === "defineRoute") {
322
- bindings.routeBindings.add(local.name);
323
- if (typeof specifier.start === "number" && typeof specifier.end === "number") {
324
- bindings.routeSpecifiers.set(local.name, {
325
- end: specifier.end,
326
- start: specifier.start,
327
- });
328
- }
329
- }
330
- if (imported.name === "defineRootRoute") {
331
- bindings.rootBindings.add(local.name);
332
- }
333
- collectDocumentBinding(imported.name, local.name, bindings);
334
- }
335
- }
336
-
337
- function collectBuilderBindings(program: Program): BuilderBindings {
338
- const bindings: BuilderBindings = {
339
- documentBindings: new Set(),
340
- rootBindings: new Set(),
341
- routeBindings: new Set(),
342
- routeSpecifiers: new Map(),
343
- };
344
- for (const statement of program.body) {
345
- if (statement.type === "ImportDeclaration") {
346
- collectBindingsFromDeclaration(statement as unknown as ImportDeclaration, bindings);
347
- }
348
- }
349
- return bindings;
350
- }
351
-
352
- function prependMissingDocumentImports(bindings: BuilderBindings, magic: MagicString): void {
353
- const missing = ["HeadContent", "Scripts"].filter(
354
- (binding) => !bindings.documentBindings.has(binding)
355
- );
356
- if (missing.length > 0) {
357
- magic.prepend(`import { ${missing.join(", ")} } from "@teyik0/furin";\n`);
358
- }
359
- }
360
-
361
- interface ChainHead {
362
- binding: string;
363
- builderEnd: number;
364
- chainEnd: number;
365
- end: number;
366
- exportedAsRoute: boolean;
367
- isRoot: boolean;
368
- start: number;
369
- terminal: string | null;
370
- }
371
-
372
- function terminalName(node: unknown): string | null {
373
- const expression = asAstNode(node);
374
- if (expression?.type !== "CallExpression") {
375
- return null;
376
- }
377
- const callee = asAstNode(expression.callee);
378
- if (callee?.type !== "MemberExpression") {
379
- return null;
380
- }
381
- const property = asAstNode(callee.property);
382
- return property?.type === "Identifier" && typeof property.name === "string"
383
- ? property.name
384
- : null;
385
- }
386
-
387
- /** `defineRoute()` / `defineRootRoute()` calls and their observable chain terminal. */
388
- function collectChainHeads(program: Program, bindings: BuilderBindings): ChainHead[] {
389
- const heads: ChainHead[] = [];
390
- walk(program as never, {
391
- CallExpression(call, context) {
392
- const callee = asAstNode(call.callee);
393
- if (callee?.type !== "Identifier" || typeof callee.name !== "string") {
394
- return;
395
- }
396
- const callEnd = typeof call.end === "number" ? call.end : 0;
397
- const calleeStart = typeof callee.start === "number" ? callee.start : 0;
398
- const calleeEnd = typeof callee.end === "number" ? callee.end : 0;
399
- const ancestors = context.ancestors() as AstNode[];
400
- const declaration = ancestors.find((ancestor) => ancestor.type === "VariableDeclarator");
401
- const identifier = asAstNode(declaration?.id);
402
- const chain = asAstNode(declaration?.init);
403
- const variableDeclaration = ancestors.find(
404
- (ancestor) => ancestor.type === "VariableDeclaration"
405
- );
406
- const exportedAsRoute =
407
- identifier?.type === "Identifier" &&
408
- identifier.name === "route" &&
409
- variableDeclaration?.kind === "const" &&
410
- ancestors.some((ancestor) => ancestor.type === "ExportNamedDeclaration");
411
- const terminal = terminalName(declaration?.init);
412
- if (bindings.rootBindings.has(callee.name)) {
413
- heads.push({
414
- binding: callee.name,
415
- builderEnd: calleeEnd,
416
- chainEnd: typeof chain?.end === "number" ? chain.end : callEnd,
417
- end: callEnd,
418
- exportedAsRoute,
419
- isRoot: true,
420
- start: calleeStart,
421
- terminal,
422
- });
423
- return;
424
- }
425
- if (bindings.routeBindings.has(callee.name)) {
426
- heads.push({
427
- binding: callee.name,
428
- builderEnd: calleeEnd,
429
- chainEnd: typeof chain?.end === "number" ? chain.end : callEnd,
430
- end: callEnd,
431
- exportedAsRoute,
432
- isRoot: false,
433
- start: calleeStart,
434
- terminal,
435
- });
436
- }
437
- },
438
- });
439
- return heads;
440
- }
441
-
442
- interface FixContext {
443
- allBindings: Set<string>;
444
- ancestorHasLoader: boolean;
445
- ancestorHasQuery: boolean;
446
- dynamicParams: string[];
447
- expected: ExpectedLayout | null;
448
- expectedResolvedPath: string;
449
- filePath: string;
450
- hasLoader: boolean;
451
- hasStaticParams: boolean;
452
- imports: LayoutImport[];
453
- isRootLayout: boolean;
454
- lastImportEnd: number;
455
- magic: MagicString;
456
- source: string;
457
- tBinding: string | null;
458
- }
459
-
460
- type InferredRenderingMode = "isr" | "ssg" | "ssr";
461
-
462
- function inferRenderingMode({
463
- hasLoader,
464
- hasQuery,
465
- hasRevalidate,
466
- isRootLayout,
467
- }: {
468
- hasLoader: boolean;
469
- hasQuery: boolean;
470
- hasRevalidate: boolean;
471
- isRootLayout: boolean;
472
- }): InferredRenderingMode {
473
- if (isRootLayout || hasQuery) {
474
- return "ssr";
475
- }
476
- if (!hasLoader) {
477
- return "ssg";
478
- }
479
- return hasRevalidate ? "isr" : "ssr";
480
- }
481
-
482
- function findSourceFile(
483
- directory: string,
484
- fileNames: readonly string[],
485
- probe: LayoutProbe
486
- ): string | null {
487
- for (const fileName of fileNames) {
488
- const candidate = join(directory, fileName);
489
- if (probe(candidate)) {
490
- return candidate;
491
- }
492
- }
493
- return null;
494
- }
495
-
496
- function ancestorLayoutPaths(filePath: string, pagesDir: string, probe: LayoutProbe): string[] {
497
- const pages = resolve(pagesDir);
498
- const file = resolve(filePath);
499
- const directory = resolve(file, "..");
500
- const isLayoutFile = withoutExtension(file) === join(directory, "_route");
501
- const paths: string[] = [];
502
- let current = isLayoutFile ? resolve(directory, "..") : directory;
503
-
504
- while (current.startsWith(pages) && current !== pages) {
505
- const layoutPath = findSourceFile(current, NESTED_LAYOUT_FILE_NAMES, probe);
506
- if (layoutPath) {
507
- paths.push(layoutPath);
508
- }
509
- current = resolve(current, "..");
510
- }
511
-
512
- const rootPath = findSourceFile(pages, ROOT_LAYOUT_FILE_NAMES, probe);
513
- if (rootPath) {
514
- paths.push(rootPath);
515
- }
516
- return paths;
517
- }
518
-
519
- interface AncestorRenderingRequirements {
520
- hasLoader: boolean;
521
- hasQuery: boolean;
522
- }
523
-
524
- function layoutRenderingRequirements(filePath: string): AncestorRenderingRequirements {
525
- try {
526
- const source = readFileSync(filePath, "utf8");
527
- const parsed = parseSource(source, detectLangFromPath(filePath));
528
- const bindings = collectBuilderBindings(parsed.program);
529
- return {
530
- hasLoader: builderChainHasMethod(parsed.program, bindings, "loader"),
531
- hasQuery: collectConfigObjects(parsed.program, bindings).some(
532
- (object) => collectConfigProperties(object).query !== undefined
533
- ),
534
- };
535
- } catch {
536
- return { hasLoader: false, hasQuery: true };
537
- }
538
- }
539
-
540
- function ancestorRenderingRequirements(
541
- filePath: string,
542
- pagesDir: string,
543
- probe: LayoutProbe
544
- ): AncestorRenderingRequirements {
545
- return ancestorLayoutPaths(filePath, pagesDir, probe).reduce<AncestorRenderingRequirements>(
546
- (requirements, layoutPath) => {
547
- const current = layoutRenderingRequirements(layoutPath);
548
- return {
549
- hasLoader: requirements.hasLoader || current.hasLoader,
550
- hasQuery: requirements.hasQuery || current.hasQuery,
551
- };
552
- },
553
- { hasLoader: false, hasQuery: false }
554
- );
555
- }
556
-
557
- const wordBoundaryRe = (binding: string): RegExp => new RegExp(`\\b${binding}\\b`);
558
-
559
- function resolveBinding(ctx: FixContext, insertionOffset: number): string {
560
- const existing = ctx.imports.find(
561
- (candidate) => candidate.resolvedPath === ctx.expectedResolvedPath
562
- );
563
- if (existing) {
564
- return existing.binding;
565
- }
566
- const expected = ctx.expected as ExpectedLayout;
567
- const taken = new Set(ctx.imports.map((candidate) => candidate.binding));
568
- let binding = expected.identifier;
569
- let suffix = 2;
570
- while (taken.has(binding) || wordBoundaryRe(binding).test(ctx.source)) {
571
- binding = `${expected.identifier}${suffix}`;
572
- suffix += 1;
573
- }
574
- ctx.magic.appendRight(
575
- insertionOffset,
576
- `\nimport { route as ${binding} } from "${expected.importPath}";${
577
- ctx.source[insertionOffset] === "\n" || ctx.source[insertionOffset] === "\r" ? "" : "\n"
578
- }`
579
- );
580
- ctx.imports.push({ binding, resolvedPath: ctx.expectedResolvedPath });
581
- return binding;
582
- }
583
-
584
- function removeObjectProperties(
585
- object: AstNode,
586
- properties: AstNode[],
587
- removed: Set<AstNode>,
588
- magic: MagicString
589
- ): void {
590
- if (removed.size === 0) {
591
- return;
592
- }
593
- if (
594
- removed.size === properties.length &&
595
- typeof object.start === "number" &&
596
- typeof object.end === "number"
597
- ) {
598
- magic.remove(object.start + 1, object.end - 1);
599
- return;
600
- }
601
-
602
- let firstRemovedIndex: number | null = null;
603
- for (let index = 0; index <= properties.length; index += 1) {
604
- const property = properties[index];
605
- if (property && removed.has(property)) {
606
- firstRemovedIndex ??= index;
607
- continue;
608
- }
609
- if (firstRemovedIndex === null) {
610
- continue;
611
- }
612
- const first = properties[firstRemovedIndex];
613
- const last = properties[index - 1];
614
- const previous = properties[firstRemovedIndex - 1];
615
- if (!(first && last && typeof first.start === "number" && typeof last.end === "number")) {
616
- firstRemovedIndex = null;
617
- continue;
618
- }
619
- if (property && typeof property.start === "number") {
620
- magic.remove(first.start, property.start);
621
- } else if (previous && typeof previous.end === "number") {
622
- magic.remove(previous.end, last.end);
623
- } else {
624
- magic.remove(first.start, last.end);
625
- }
626
- firstRemovedIndex = null;
627
- }
628
- }
629
-
630
- interface ConfigProperties {
631
- all: AstNode[];
632
- layout: AstNode | undefined;
633
- mode: AstNode | undefined;
634
- params: AstNode | undefined;
635
- query: AstNode | undefined;
636
- revalidate: AstNode | undefined;
637
- staticParams: AstNode | undefined;
638
- }
639
-
640
- function collectConfigProperties(object: AstNode): ConfigProperties {
641
- const propertyValues: unknown[] = Array.isArray(object.properties) ? object.properties : [];
642
- const all = propertyValues
643
- .map((property) => asAstNode(property))
644
- .filter((property): property is AstNode => property !== null);
645
- const findProperty = (name: string) =>
646
- all.find((property) => property.type === "Property" && propertyName(property.key) === name);
647
- return {
648
- all,
649
- layout: findProperty("layout"),
650
- mode: findProperty("mode"),
651
- params: findProperty("params"),
652
- query: findProperty("query"),
653
- revalidate: findProperty("revalidate"),
654
- staticParams: findProperty("staticParams"),
655
- };
656
- }
657
-
658
- function validateConfigProperties(
659
- properties: ConfigProperties,
660
- ctx: FixContext
661
- ): { inferredMode: InferredRenderingMode | null; layoutValue: AstNode | null } {
662
- const layoutValue = properties.layout ? asAstNode(properties.layout.value) : null;
663
- if (properties.staticParams) {
664
- throw routeConfigError(ctx.filePath, "staticParams must be chained after config");
665
- }
666
- if (!ctx.isRootLayout && properties.layout && layoutValue?.type !== "Identifier") {
667
- throw routeConfigError(ctx.filePath, "use a static layout route reference");
668
- }
669
- const modeValue = properties.mode ? asAstNode(properties.mode.value) : null;
670
- if (
671
- properties.revalidate &&
672
- modeValue?.type === "Literal" &&
673
- (modeValue.value === "ssg" || modeValue.value === "ssr")
674
- ) {
675
- throw routeConfigError(ctx.filePath, "revalidate requires mode isr");
676
- }
677
- if (ctx.hasStaticParams && modeValue?.type === "Literal" && modeValue.value === "ssr") {
678
- throw routeConfigError(ctx.filePath, "staticParams requires mode ssg or isr");
679
- }
680
- if (!properties.revalidate && modeValue?.type === "Literal" && modeValue.value === "isr") {
681
- throw routeConfigError(ctx.filePath, "isr requires revalidate > 0 (or use ssg/ssr)");
682
- }
683
-
684
- const inferredMode = properties.mode
685
- ? null
686
- : inferRenderingMode({
687
- hasLoader: ctx.hasLoader || ctx.ancestorHasLoader,
688
- hasQuery: Boolean(properties.query) || ctx.ancestorHasQuery,
689
- hasRevalidate: Boolean(properties.revalidate),
690
- isRootLayout: ctx.isRootLayout,
691
- });
692
- if (ctx.hasStaticParams && inferredMode === "ssr") {
693
- throw routeConfigError(ctx.filePath, "staticParams requires mode ssg or isr");
694
- }
695
- return { inferredMode, layoutValue };
696
- }
697
-
698
- function removeInvalidConfigProperties(
699
- object: AstNode,
700
- properties: ConfigProperties,
701
- inferredMode: InferredRenderingMode | null,
702
- ctx: FixContext
703
- ): Set<AstNode> {
704
- const removed = new Set<AstNode>();
705
- if (ctx.isRootLayout && properties.layout) {
706
- removed.add(properties.layout);
707
- }
708
- if (properties.revalidate && inferredMode !== null && inferredMode !== "isr") {
709
- removed.add(properties.revalidate);
710
- }
711
- removeObjectProperties(object, properties.all, removed, ctx.magic);
712
- return removed;
713
- }
714
-
715
- function repointLayout(layoutValue: AstNode | null, ctx: FixContext): boolean {
716
- if (layoutValue?.type === "Identifier" && ctx.expected) {
717
- const binding = resolveBinding(ctx, Math.max(ctx.lastImportEnd, 0));
718
- if (
719
- binding !== layoutValue.name &&
720
- typeof layoutValue.start === "number" &&
721
- typeof layoutValue.end === "number"
722
- ) {
723
- ctx.magic.update(layoutValue.start, layoutValue.end, binding);
724
- return true;
725
- }
726
- }
727
- return false;
728
- }
729
-
730
- function missingConfigEntries(
731
- properties: ConfigProperties,
732
- inferredMode: InferredRenderingMode | null,
733
- ctx: FixContext
734
- ): string[] {
735
- const missing: string[] = [];
736
- if (!(properties.layout || ctx.isRootLayout) && ctx.expected) {
737
- const binding = resolveBinding(ctx, Math.max(ctx.lastImportEnd, 0));
738
- missing.push(`layout: ${binding}`);
739
- }
740
- if (!properties.mode) {
741
- missing.push(`mode: "${inferredMode}"`);
742
- }
743
- if (!properties.params && ctx.dynamicParams.length > 0) {
744
- const typeBoxBinding = ensureTImport(ctx);
745
- const body = ctx.dynamicParams
746
- .map((name) => `${paramKey(name)}: ${typeBoxBinding}.String()`)
747
- .join(", ");
748
- missing.push(`params: ${typeBoxBinding}.Object({ ${body} })`);
749
- }
750
- return missing;
751
- }
752
-
753
- /** Dynamic param names derived from the file path segments (`[id]` → id, `[...all]` → "*"). */
754
- export function dynamicParamsFor(filePath: string, pagesDir: string): string[] {
755
- const relativePath = withoutExtension(relative(resolve(pagesDir), resolve(filePath)))
756
- .replaceAll("\\", "/")
757
- .replace(LEADING_SLASH_RE, "");
758
- const params = new Set<string>();
759
- for (const segment of relativePath.split("/")) {
760
- if (segment.length === 0) {
761
- continue;
762
- }
763
- const dynamic = parseDynamicRouteSegment(segment, filePath);
764
- if (!dynamic) {
765
- continue;
766
- }
767
- params.add(dynamic.catchAll ? "*" : dynamic.name);
768
- }
769
- return [...params];
770
- }
771
-
772
- const IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/;
773
-
774
- const paramKey = (name: string): string => (IDENTIFIER_RE.test(name) ? name : JSON.stringify(name));
775
-
776
- function ensureTImport(ctx: FixContext): string {
777
- if (ctx.tBinding) {
778
- return ctx.tBinding;
779
- }
780
- const offset = Math.max(ctx.lastImportEnd, 0);
781
- let binding = "t";
782
- let suffix = 2;
783
- while (ctx.allBindings.has(binding)) {
784
- binding = `t${suffix}`;
785
- suffix += 1;
786
- }
787
- ctx.magic.appendRight(offset, `\nimport { ${binding} } from "elysia";`);
788
- ctx.tBinding = binding;
789
- return binding;
790
- }
791
-
792
- function insertConfigEntries(
793
- object: AstNode,
794
- entries: string[],
795
- properties: ConfigProperties,
796
- removed: Set<AstNode>,
797
- ctx: FixContext
798
- ): boolean {
799
- if (entries.length === 0) {
800
- return false;
801
- }
802
- const first = properties.all.find(
803
- (property) => !removed.has(property) && typeof property.start === "number"
804
- );
805
- if (first && typeof first.start === "number") {
806
- ctx.magic.appendRight(first.start, `${entries.join(", ")}, `);
807
- return true;
808
- }
809
- if (typeof object.start === "number") {
810
- ctx.magic.appendRight(object.start + 1, ` ${entries.join(", ")} `);
811
- return true;
812
- }
813
- return false;
814
- }
815
-
816
- function injectMissingConfigKeys(object: AstNode, ctx: FixContext): boolean {
817
- const properties = collectConfigProperties(object);
818
- const { inferredMode, layoutValue } = validateConfigProperties(properties, ctx);
819
- const removed = removeInvalidConfigProperties(object, properties, inferredMode, ctx);
820
- const layoutRepointed = repointLayout(layoutValue, ctx);
821
- const missing = missingConfigEntries(properties, inferredMode, ctx);
822
- const entriesInserted = insertConfigEntries(object, missing, properties, removed, ctx);
823
- return removed.size > 0 || layoutRepointed || entriesInserted;
824
- }
825
-
826
- interface RouteFileConvention {
827
- dynamicParams: string[];
828
- expected: ExpectedLayout | null;
829
- fileName: string | undefined;
830
- filePath: string;
831
- isNestedLayout: boolean;
832
- isRootLayout: boolean;
833
- }
834
-
835
- function routeFileConvention(
836
- filePath: string,
837
- pagesDir: string,
838
- probe: LayoutProbe
839
- ): RouteFileConvention {
840
- const pages = resolve(pagesDir);
841
- const file = resolve(filePath);
842
- const extensionlessFile = withoutExtension(file);
843
- return {
844
- dynamicParams: dynamicParamsFor(filePath, pagesDir),
845
- expected: expectedLayoutFor(filePath, pagesDir, probe),
846
- fileName: extensionlessFile.split(DIRECTORY_SPLIT_RE).pop(),
847
- filePath,
848
- isNestedLayout: extensionlessFile === join(resolve(file, ".."), "_route"),
849
- isRootLayout: extensionlessFile === join(pages, "root"),
850
- };
851
- }
852
-
853
- /** `, params: t.Object({ … })` fragment for scaffolds — empty when no dynamic segment. */
854
- function scaffoldParamsDeclaration(dynamicParams: string[]): string {
855
- if (dynamicParams.length === 0) {
856
- return "";
857
- }
858
- const body = dynamicParams.map((name) => `${paramKey(name)}: t.String()`).join(", ");
859
- return `, params: t.Object({ ${body} })`;
860
- }
861
-
862
- function scaffoldEmptyRoute(convention: RouteFileConvention): string | null {
863
- const { dynamicParams, expected, fileName, filePath, isNestedLayout, isRootLayout } = convention;
864
- if (isRootLayout) {
865
- return `import { defineRootRoute, HeadContent, Scripts } from "@teyik0/furin";
866
-
867
- export const route = defineRootRoute()
868
- .config({ mode: "ssr" })
869
- .layout(({ children }) => (
870
- <html lang="en">
871
- <head>
872
- <HeadContent />
873
- </head>
874
- <body>
875
- {children}
876
- <Scripts />
877
- </body>
878
- </html>
879
- ));
880
- `;
881
- }
882
- if (isNestedLayout && expected) {
883
- const tImport = dynamicParams.length > 0 ? `\nimport { t } from "elysia"` : "";
884
- return `import { defineRoute } from "@teyik0/furin";${tImport}
885
- import { route as ${expected.identifier} } from "${expected.importPath}";
886
-
887
- export const route = defineRoute()
888
- .config({ layout: ${expected.identifier}, mode: "ssr"${scaffoldParamsDeclaration(dynamicParams)} })
889
- .layout(({ children }) => children);
890
- `;
891
- }
892
- if (fileName === "error" || fileName === "not-found") {
893
- return null;
894
- }
895
- if (!expected) {
896
- throw routeConfigError(filePath, "no ancestor layout found; create pages/root.tsx");
897
- }
898
- const tImport = dynamicParams.length > 0 ? `\nimport { t } from "elysia"` : "";
899
- return `import { defineRoute } from "@teyik0/furin";${tImport}
900
- import { route as ${expected.identifier} } from "${expected.importPath}";
901
-
902
- export const route = defineRoute()
903
- .config({ layout: ${expected.identifier}, mode: "ssg"${scaffoldParamsDeclaration(dynamicParams)} })
904
- .page(() => null);
905
- `;
906
- }
907
-
908
- function validateRouteChain(heads: ChainHead[], convention: RouteFileConvention): ChainHead | null {
909
- if (heads.length === 0) {
910
- return null;
911
- }
912
- if (heads.length > 1) {
913
- throw routeConfigError(convention.filePath, "one route per file");
914
- }
915
- const [routeHead] = heads;
916
- if (!routeHead) {
917
- return null;
918
- }
919
- if (!convention.isRootLayout && routeHead.isRoot) {
920
- throw routeConfigError(
921
- convention.filePath,
922
- "use defineRoute + config({ layout }) outside pages/root.tsx"
923
- );
924
- }
925
- if (!(convention.isRootLayout || convention.expected)) {
926
- throw routeConfigError(convention.filePath, "no ancestor layout found; create pages/root.tsx");
927
- }
928
- if ((convention.isRootLayout || convention.isNestedLayout) && routeHead.terminal === "page") {
929
- throw routeConfigError(convention.filePath, "layout files must end with .layout()");
930
- }
931
- if (!(convention.isRootLayout || convention.isNestedLayout) && routeHead.terminal !== "page") {
932
- throw routeConfigError(convention.filePath, "page files must end with .page()");
933
- }
934
- if (!routeHead.exportedAsRoute) {
935
- throw routeConfigError(
936
- convention.filePath,
937
- "route files must export a route terminal as export const route"
938
- );
939
- }
940
- return routeHead;
941
- }
942
-
943
- function rewriteRootBuilder(
944
- routeHead: ChainHead,
945
- bindings: BuilderBindings,
946
- isRootLayout: boolean,
947
- magic: MagicString
948
- ): boolean {
949
- if (!(isRootLayout && !routeHead.isRoot)) {
950
- return false;
951
- }
952
- const existingRootBinding = bindings.rootBindings.values().next().value as string | undefined;
953
- const replacement = existingRootBinding ?? "defineRootRoute";
954
- if (!existingRootBinding) {
955
- const specifier = bindings.routeSpecifiers.get(routeHead.binding);
956
- if (specifier) {
957
- magic.update(specifier.start, specifier.end, "defineRootRoute");
958
- }
959
- }
960
- magic.update(routeHead.start, routeHead.builderEnd, replacement);
961
- return true;
962
- }
963
-
964
- function insertCompleteConfig(routeHead: ChainHead, ctx: FixContext): void {
965
- const binding =
966
- ctx.expected && !ctx.isRootLayout ? resolveBinding(ctx, Math.max(ctx.lastImportEnd, 0)) : null;
967
- const entries: string[] = [];
968
- if (binding && !ctx.isRootLayout) {
969
- entries.push(`layout: ${binding}`);
970
- }
971
- const mode = inferRenderingMode({
972
- hasLoader: ctx.hasLoader || ctx.ancestorHasLoader,
973
- hasQuery: ctx.ancestorHasQuery,
974
- hasRevalidate: false,
975
- isRootLayout: ctx.isRootLayout,
976
- });
977
- if (ctx.hasStaticParams && mode === "ssr") {
978
- throw routeConfigError(ctx.filePath, "staticParams requires mode ssg or isr");
979
- }
980
- entries.push(`mode: "${mode}"`);
981
- if (ctx.dynamicParams.length > 0) {
982
- const typeBoxBinding = ensureTImport(ctx);
983
- const params = ctx.dynamicParams
984
- .map((name) => `${paramKey(name)}: ${typeBoxBinding}.String()`)
985
- .join(", ");
986
- entries.push(`params: ${typeBoxBinding}.Object({ ${params} })`);
987
- }
988
- ctx.magic.appendRight(routeHead.end, `.config({ ${entries.join(", ")} })`);
989
- }
990
-
991
- function insertMissingLayoutTerminal(
992
- routeHead: ChainHead,
993
- bindings: BuilderBindings,
994
- convention: RouteFileConvention,
995
- magic: MagicString
996
- ): boolean {
997
- if (!(convention.isRootLayout || convention.isNestedLayout) || routeHead.terminal === "layout") {
998
- return false;
999
- }
1000
- if (convention.isRootLayout) {
1001
- prependMissingDocumentImports(bindings, magic);
1002
- }
1003
- const layout = convention.isRootLayout
1004
- ? '(\n <html lang="en">\n <head>\n <HeadContent />\n </head>\n <body>\n {children}\n <Scripts />\n </body>\n </html>\n )'
1005
- : "children";
1006
- magic.appendRight(routeHead.chainEnd, `.layout(({ children }) => ${layout})`);
1007
- return true;
1008
- }
1009
-
1010
- function legacyRootLayoutBody(node: unknown): AstNode | null {
1011
- const callback = asAstNode(node);
1012
- if (callback?.type !== "ArrowFunctionExpression") {
1013
- return null;
1014
- }
1015
- const params = Array.isArray(callback.params) ? callback.params : [];
1016
- const pattern = asAstNode(params[0]);
1017
- const properties = Array.isArray(pattern?.properties) ? pattern.properties : [];
1018
- const property = properties.length === 1 ? asAstNode(properties[0]) : null;
1019
- const value = asAstNode(property?.value);
1020
- const body = asAstNode(callback.body);
1021
- if (
1022
- pattern?.type !== "ObjectPattern" ||
1023
- property?.type !== "Property" ||
1024
- propertyName(property.key) !== "children" ||
1025
- value?.type !== "Identifier" ||
1026
- value.name !== "children" ||
1027
- body?.type !== "Identifier" ||
1028
- body.name !== "children"
1029
- ) {
1030
- return null;
1031
- }
1032
- return body;
1033
- }
1034
-
1035
- function rewriteLegacyRootLayout(
1036
- program: Program,
1037
- bindings: BuilderBindings,
1038
- convention: RouteFileConvention,
1039
- magic: MagicString
1040
- ): boolean {
1041
- if (!convention.isRootLayout) {
1042
- return false;
1043
- }
1044
- let touched = false;
1045
- walk(program as never, {
1046
- CallExpression(call) {
1047
- const callee = asAstNode(call.callee);
1048
- if (callee?.type !== "MemberExpression" || !belongsToBuilderChain(callee.object, bindings)) {
1049
- return;
1050
- }
1051
- const property = asAstNode(callee.property);
1052
- if (property?.type !== "Identifier" || property.name !== "layout") {
1053
- return;
1054
- }
1055
- const argument = Array.isArray(call.arguments) ? call.arguments[0] : null;
1056
- const body = legacyRootLayoutBody(argument);
1057
- if (typeof body?.start === "number" && typeof body.end === "number") {
1058
- prependMissingDocumentImports(bindings, magic);
1059
- magic.update(
1060
- body.start,
1061
- body.end,
1062
- '(\n <html lang="en">\n <head>\n <HeadContent />\n </head>\n <body>\n {children}\n <Scripts />\n </body>\n </html>\n )'
1063
- );
1064
- touched = true;
1065
- }
1066
- },
1067
- });
1068
- return touched;
1069
- }
1070
-
1071
- /**
1072
- * Rewrites `defineRoute()` chains so every route file declares a full
1073
- * `config({ layout, mode, ... })`:
1074
- * - chain without `config()` → inserts `.config({ layout, mode })`
1075
- * - `config()` without `layout` → injects the expected ancestor layout
1076
- * - `config()` with a WRONG layout → repoints the reference (and its import)
1077
- * - `config()` without `mode` → infers the mode using the runtime cascade
1078
- * - layout file without `layout()` → appends an identity layout terminal
1079
- * The root layout (`pages/root.tsx`, built with `defineRootRoute()`) never
1080
- * receives a parent `layout` config entry. Returns the new source, or null when unchanged
1081
- * (idempotent — running the fix twice returns null on the second pass).
1082
- */
1083
- export function fixRouteConfigLayout(
1084
- source: string,
1085
- filePath: string,
1086
- pagesDir: string,
1087
- probe: LayoutProbe = existsSync
1088
- ): string | null {
1089
- const convention = routeFileConvention(filePath, pagesDir, probe);
1090
- if (source.trim().length === 0) {
1091
- return scaffoldEmptyRoute(convention);
1092
- }
1093
- if (!(source.includes("defineRoute") || source.includes("defineRootRoute"))) {
1094
- return null;
1095
- }
1096
- const parsed = parseSource(source, detectLangFromPath(filePath));
1097
- const magic = new MagicString(source);
1098
- const expectedResolvedPath = convention.expected
1099
- ? withoutExtension(resolve(filePath, "..", convention.expected.importPath))
1100
- : "";
1101
- const { imports, lastImportEnd, allBindings, tBinding } = collectLayoutImports(
1102
- parsed.program,
1103
- filePath
1104
- );
1105
- const bindings = collectBuilderBindings(parsed.program);
1106
- const hasLoader = builderChainHasMethod(parsed.program, bindings, "loader");
1107
- const hasStaticParams = builderChainHasMethod(parsed.program, bindings, "staticParams");
1108
- const heads = collectChainHeads(parsed.program, bindings).filter(
1109
- (candidate) => candidate.end !== 0
1110
- );
1111
- const routeHead = validateRouteChain(heads, convention);
1112
- if (!routeHead) {
1113
- return null;
1114
- }
1115
- let touched = rewriteRootBuilder(routeHead, bindings, convention.isRootLayout, magic);
1116
- const configObjects = collectConfigObjects(parsed.program, bindings);
1117
- const ancestorRequirements = ancestorRenderingRequirements(filePath, pagesDir, probe);
1118
- const ctx: FixContext = {
1119
- allBindings,
1120
- ancestorHasLoader: ancestorRequirements.hasLoader,
1121
- ancestorHasQuery: ancestorRequirements.hasQuery,
1122
- dynamicParams: dynamicParamsFor(filePath, pagesDir),
1123
- expected: convention.expected,
1124
- expectedResolvedPath,
1125
- filePath,
1126
- hasLoader,
1127
- hasStaticParams,
1128
- imports,
1129
- isRootLayout: convention.isRootLayout,
1130
- lastImportEnd,
1131
- magic,
1132
- source,
1133
- tBinding,
1134
- };
1135
- if (configObjects.length === 0) {
1136
- insertCompleteConfig(routeHead, ctx);
1137
- touched = true;
1138
- }
1139
- for (const object of configObjects) {
1140
- touched = injectMissingConfigKeys(object, ctx) || touched;
1141
- }
1142
- touched = insertMissingLayoutTerminal(routeHead, bindings, convention, magic) || touched;
1143
- touched = rewriteLegacyRootLayout(parsed.program, bindings, convention, magic) || touched;
1144
-
1145
- const output = magic.toString();
1146
- // Structural idempotence: a rewrite that produces the exact input is no
1147
- // change at all (protects every future branch from breaking the contract).
1148
- return output === source ? null : output;
1149
- }