@teyik0/furin 0.2.0-alpha.5 → 0.4.0-alpha.1

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 (282) hide show
  1. package/README.md +2 -3
  2. package/dist/adapter/bun.d.ts +1 -1
  3. package/dist/adapter/static.d.ts +1 -1
  4. package/dist/build/bun-build.d.ts +3 -0
  5. package/dist/build/client.d.ts +2 -2
  6. package/dist/build/entry-template.d.ts +6 -0
  7. package/dist/build/entry-template.ts +20 -3
  8. package/dist/build/hydrate.d.ts +1 -1
  9. package/dist/build/index.d.ts +1 -1
  10. package/dist/build/index.js +9363 -5827
  11. package/dist/build/production-instrumentation.d.ts +1 -0
  12. package/dist/build/route-types.d.ts +3 -23
  13. package/dist/build/shared.d.ts +1 -1
  14. package/dist/build/ssg-cache.d.ts +1 -1
  15. package/dist/build/types.d.ts +2 -1
  16. package/dist/cli/index.js +9467 -5870
  17. package/dist/client/boundaries.d.ts +6 -4
  18. package/dist/client/define-route.d.ts +35 -0
  19. package/dist/client/document.d.ts +26 -0
  20. package/dist/client/hmr.d.ts +10 -0
  21. package/dist/client/internal/runtime-types.d.ts +32 -0
  22. package/dist/client/link.d.ts +2 -1
  23. package/dist/client/link.js +1884 -1227
  24. package/dist/client/router/boundary-tree.d.ts +1 -1
  25. package/dist/client/router/index.d.ts +2 -2
  26. package/dist/client/router/link-utils.d.ts +14 -1
  27. package/dist/client/router/search/index.d.ts +3 -7
  28. package/dist/client/router/sync-catch-up.d.ts +4 -4
  29. package/dist/client/router/types.d.ts +34 -26
  30. package/dist/client-only.d.ts +1 -0
  31. package/dist/client-only.js +1 -0
  32. package/dist/client.d.ts +11 -169
  33. package/dist/client.js +197 -36
  34. package/dist/config.d.ts +8 -8
  35. package/dist/config.js +10 -10
  36. package/dist/define-route.d.ts +1355 -0
  37. package/dist/devtools/devtools-element.d.ts +1 -0
  38. package/dist/devtools/protocol.d.ts +119 -0
  39. package/dist/furin.d.ts +24 -39
  40. package/dist/furin.js +14432 -7929
  41. package/dist/isomorphic.d.ts +18 -0
  42. package/dist/plugin/binding-scope.d.ts +2 -0
  43. package/dist/plugin/dead-code-elimination.d.ts +3 -0
  44. package/dist/plugin/index.js +1708 -489
  45. package/dist/plugin/route-config-autofix.d.ts +31 -0
  46. package/dist/plugin/routes.d.ts +27 -0
  47. package/dist/plugin/transform-client.d.ts +0 -2
  48. package/dist/plugin/transform-isomorphic.d.ts +9 -0
  49. package/dist/routes.d.ts +6 -0
  50. package/dist/routes.js +1 -0
  51. package/dist/rsc/build/environment.d.ts +2 -0
  52. package/dist/rsc/build/index.d.ts +15 -0
  53. package/dist/rsc/client-codec.d.ts +1 -0
  54. package/dist/rsc/codec.d.ts +4 -0
  55. package/dist/rsc/flight-drain.d.ts +11 -0
  56. package/dist/rsc/render-error.d.ts +17 -0
  57. package/dist/rsc/server-codec.d.ts +2 -0
  58. package/dist/rsc/server-codec.js +24 -0
  59. package/dist/rsc/shared.d.ts +30 -0
  60. package/dist/rsc/symbols.d.ts +2 -0
  61. package/dist/rsc/transport-benchmark.d.ts +13 -0
  62. package/dist/rsc/version.d.ts +7 -0
  63. package/dist/rsc-client.d.ts +4 -0
  64. package/dist/rsc-client.js +166 -0
  65. package/dist/rsc-server.d.ts +11 -0
  66. package/dist/rsc.d.ts +11 -0
  67. package/dist/rsc.js +353 -0
  68. package/dist/server/cache/index.d.ts +2 -2
  69. package/dist/server/cache/invalidation.d.ts +2 -3
  70. package/dist/server/cache/isr-ssg.d.ts +5 -1
  71. package/dist/server/cache/isr.d.ts +9 -0
  72. package/dist/server/cache/route-cache.d.ts +10 -8
  73. package/dist/server/cache/store-view.d.ts +7 -7
  74. package/dist/server/dev-page-plugin.d.ts +11 -5
  75. package/dist/server/devtools/hub.d.ts +11 -0
  76. package/dist/server/devtools/instrumentation.d.ts +22 -0
  77. package/dist/server/devtools/instrumentation.production.d.ts +44 -0
  78. package/dist/server/devtools/plugin.d.ts +3 -0
  79. package/dist/server/devtools/request-context.d.ts +6 -0
  80. package/dist/server/internal.d.ts +5 -0
  81. package/dist/server/internal.js +3 -3
  82. package/dist/server/lang-detect.d.ts +1 -0
  83. package/dist/server/logger.d.ts +4 -0
  84. package/dist/server/render/assemble.d.ts +4 -9
  85. package/dist/server/render/document.d.ts +4 -0
  86. package/dist/server/render/element.d.ts +3 -2
  87. package/dist/server/render/index.d.ts +2 -2
  88. package/dist/server/render/index.ts +6 -3
  89. package/dist/server/render/isr.d.ts +1 -1
  90. package/dist/server/render/loaders.d.ts +9 -1
  91. package/dist/server/render/not-found.d.ts +2 -2
  92. package/dist/server/render/ppr-route.d.ts +8 -0
  93. package/dist/server/render/route-frame-transport.d.ts +2 -0
  94. package/dist/server/render/shell.ts +0 -1
  95. package/dist/server/render/ssg.d.ts +1 -1
  96. package/dist/server/render/ssr.d.ts +9 -9
  97. package/dist/server/render/template.d.ts +4 -0
  98. package/dist/server/router/defined-route.d.ts +24 -0
  99. package/dist/server/router/discovery.d.ts +1 -1
  100. package/dist/server/router/hmr.d.ts +17 -9
  101. package/dist/server/router/patterns.d.ts +7 -1
  102. package/dist/server/router/plugin.d.ts +8 -10
  103. package/dist/server/router/schema-merge.d.ts +3 -0
  104. package/dist/server/router/schemas.d.ts +14 -9
  105. package/dist/server/router/source-version.d.ts +2 -0
  106. package/dist/server/router/types.d.ts +2 -1
  107. package/dist/server/runtime-env.js +2 -2
  108. package/dist/server/sync/adapter.d.ts +45 -26
  109. package/dist/server/sync/config.d.ts +5 -9
  110. package/dist/server/sync/fingerprint.d.ts +2 -1
  111. package/dist/server/sync/index.d.ts +15 -4
  112. package/dist/server/sync/index.js +3023 -0
  113. package/dist/server/sync/notifier.d.ts +18 -0
  114. package/dist/server/sync/plugin.d.ts +53 -54
  115. package/dist/server/sync/postgres/index.d.ts +19 -0
  116. package/dist/server/sync/postgres/index.js +252 -0
  117. package/dist/server/sync/postgres/migrate.d.ts +2 -0
  118. package/dist/server/sync/postgres/migrate.js +17 -0
  119. package/dist/server/sync/postgres/migration.sql +66 -0
  120. package/dist/server/sync/redis/index.d.ts +30 -0
  121. package/dist/server/sync/redis/index.js +336 -0
  122. package/dist/server/sync/redis/scripts.d.ts +4 -0
  123. package/dist/server/sync/response.d.ts +10 -1
  124. package/dist/server/sync/runtime.d.ts +6 -0
  125. package/dist/server/sync/sqlite/index.d.ts +20 -0
  126. package/dist/server/sync/sqlite/index.js +250 -0
  127. package/dist/server/sync/stream.d.ts +12 -11
  128. package/dist/server-only.d.ts +1 -0
  129. package/dist/server-only.js +1 -0
  130. package/dist/shared/await.d.ts +6 -7
  131. package/dist/shared/elysia-contract.d.ts +41 -0
  132. package/dist/shared/parser.d.ts +2 -0
  133. package/dist/shared/route-frame.d.ts +49 -0
  134. package/dist/shared/route-map.d.ts +5 -0
  135. package/dist/shared/utils/index.d.ts +1 -3
  136. package/package.json +86 -41
  137. package/src/adapter/bun.ts +113 -36
  138. package/src/adapter/package.ts +23 -15
  139. package/src/adapter/static.ts +97 -50
  140. package/src/build/bun-build.ts +11 -0
  141. package/src/build/client.ts +41 -26
  142. package/src/build/entry-template.ts +20 -3
  143. package/src/build/hydrate.ts +128 -33
  144. package/src/build/index.ts +14 -2
  145. package/src/build/production-instrumentation.ts +14 -0
  146. package/src/build/route-types.ts +20 -116
  147. package/src/build/scan-server.ts +3 -2
  148. package/src/build/shared.ts +1 -1
  149. package/src/build/ssg-cache.ts +4 -2
  150. package/src/build/types.ts +2 -1
  151. package/src/cli/config.ts +3 -3
  152. package/src/cli/index.ts +58 -27
  153. package/src/client/boundaries.tsx +51 -19
  154. package/src/client/default-screens.tsx +4 -4
  155. package/src/client/define-route.ts +48 -0
  156. package/src/client/document.tsx +165 -0
  157. package/src/client/hmr.ts +40 -0
  158. package/src/client/internal/runtime-types.ts +33 -0
  159. package/src/client/link.tsx +51 -38
  160. package/src/client/router/boundary-tree.tsx +2 -2
  161. package/src/client/router/context.ts +13 -9
  162. package/src/client/router/index.ts +4 -0
  163. package/src/client/router/link-utils.ts +92 -3
  164. package/src/client/router/navigation.ts +9 -15
  165. package/src/client/router/provider.tsx +98 -53
  166. package/src/client/router/search/index.ts +9 -19
  167. package/src/client/router/search-store.ts +5 -1
  168. package/src/client/router/spa-response.ts +3 -4
  169. package/src/client/router/sync-catch-up.ts +9 -8
  170. package/src/client/router/types.ts +52 -26
  171. package/src/client/sync.ts +3 -2
  172. package/src/client-only.ts +1 -0
  173. package/src/client.ts +28 -283
  174. package/src/config.ts +10 -10
  175. package/src/define-route.ts +959 -0
  176. package/src/devtools/devtools-element.js +885 -0
  177. package/src/devtools/protocol.ts +139 -0
  178. package/src/furin.ts +475 -136
  179. package/src/isomorphic.ts +66 -0
  180. package/src/plugin/binding-scope.ts +158 -0
  181. package/src/plugin/dead-code-elimination.ts +202 -0
  182. package/src/plugin/index.ts +14 -18
  183. package/src/plugin/route-config-autofix.ts +1140 -0
  184. package/src/plugin/routes.ts +891 -0
  185. package/src/plugin/transform-client.ts +160 -330
  186. package/src/plugin/transform-isomorphic.ts +464 -0
  187. package/src/routes.ts +6 -0
  188. package/src/rsc/build/environment.ts +30 -0
  189. package/src/rsc/build/index.ts +94 -0
  190. package/src/rsc/client-codec.ts +11 -0
  191. package/src/rsc/codec.ts +85 -0
  192. package/src/rsc/flight-drain.ts +68 -0
  193. package/src/rsc/react-server-dom-webpack.d.ts +23 -0
  194. package/src/rsc/render-error.ts +120 -0
  195. package/src/rsc/server-codec.ts +25 -0
  196. package/src/rsc/shared.tsx +113 -0
  197. package/src/rsc/symbols.ts +2 -0
  198. package/src/rsc/transport-benchmark.ts +73 -0
  199. package/src/rsc/version.ts +57 -0
  200. package/src/rsc-client.ts +12 -0
  201. package/src/rsc-server.tsx +91 -0
  202. package/src/rsc.tsx +75 -0
  203. package/src/server/auto-invalidate/registry.ts +2 -2
  204. package/src/server/auto-invalidate/runtime.ts +32 -6
  205. package/src/server/cache/dev-loader.ts +70 -10
  206. package/src/server/cache/index.ts +7 -3
  207. package/src/server/cache/invalidation.ts +33 -15
  208. package/src/server/cache/isr-ssg.ts +12 -3
  209. package/src/server/cache/isr.ts +84 -9
  210. package/src/server/cache/route-cache.ts +49 -36
  211. package/src/server/cache/store-view.ts +7 -7
  212. package/src/server/context-logger.ts +7 -7
  213. package/src/server/dev-page-plugin.ts +38 -17
  214. package/src/server/devtools/hub.ts +64 -0
  215. package/src/server/devtools/instrumentation.production.ts +80 -0
  216. package/src/server/devtools/instrumentation.ts +72 -0
  217. package/src/server/devtools/plugin.ts +269 -0
  218. package/src/server/devtools/request-context.ts +76 -0
  219. package/src/server/instance.ts +4 -4
  220. package/src/server/internal.ts +5 -0
  221. package/src/server/lang-detect.ts +14 -10
  222. package/src/server/logger.ts +21 -0
  223. package/src/server/render/assemble.ts +58 -36
  224. package/src/server/render/document.tsx +25 -0
  225. package/src/server/render/element.tsx +24 -9
  226. package/src/server/render/index.ts +6 -3
  227. package/src/server/render/isr.ts +182 -117
  228. package/src/server/render/loaders.ts +289 -50
  229. package/src/server/render/not-found.ts +57 -38
  230. package/src/server/render/ppr-route.ts +146 -0
  231. package/src/server/render/route-frame-transport.ts +62 -0
  232. package/src/server/render/shell.ts +0 -1
  233. package/src/server/render/ssg.ts +7 -6
  234. package/src/server/render/ssr.ts +412 -166
  235. package/src/server/render/template.ts +93 -2
  236. package/src/server/router/defined-route.ts +110 -0
  237. package/src/server/router/discovery.ts +165 -48
  238. package/src/server/router/hmr.ts +126 -53
  239. package/src/server/router/patterns.ts +56 -27
  240. package/src/server/router/plugin.ts +210 -200
  241. package/src/server/router/schema-merge.ts +42 -0
  242. package/src/server/router/schemas.ts +61 -76
  243. package/src/server/router/source-version.ts +16 -0
  244. package/src/server/router/types.ts +3 -1
  245. package/src/server/sync/adapter.ts +44 -28
  246. package/src/server/sync/config.ts +9 -27
  247. package/src/server/sync/fingerprint.ts +8 -3
  248. package/src/server/sync/index.ts +37 -13
  249. package/src/server/sync/notifier.ts +109 -0
  250. package/src/server/sync/plugin.ts +184 -89
  251. package/src/server/sync/postgres/index.ts +301 -0
  252. package/src/server/sync/postgres/migrate.ts +16 -0
  253. package/src/server/sync/postgres/migration.sql +66 -0
  254. package/src/server/sync/redis/index.ts +342 -0
  255. package/src/server/sync/redis/scripts.ts +74 -0
  256. package/src/server/sync/response.ts +145 -16
  257. package/src/server/sync/runtime.ts +27 -0
  258. package/src/server/sync/sql.d.ts +4 -0
  259. package/src/server/sync/sqlite/index.ts +337 -0
  260. package/src/server/sync/sqlite/migration.sql +70 -0
  261. package/src/server/sync/stream.ts +137 -81
  262. package/src/server-only.ts +1 -0
  263. package/src/shared/await.tsx +10 -11
  264. package/src/shared/deferred-ndjson.ts +34 -6
  265. package/src/shared/digest.ts +2 -2
  266. package/src/shared/elysia-contract.ts +63 -0
  267. package/src/shared/parser.ts +5 -0
  268. package/src/shared/route-frame.ts +429 -0
  269. package/src/shared/route-map.ts +37 -0
  270. package/src/shared/search-params.ts +7 -7
  271. package/src/shared/utils/index.ts +6 -22
  272. package/dist/build/server-routes-entry.d.ts +0 -13
  273. package/dist/build/server-routes-entry.ts +0 -30
  274. package/dist/server/dev-inspector.d.ts +0 -89
  275. package/dist/server/router/index.d.ts +0 -6
  276. package/dist/server/router/index.js +0 -27
  277. package/dist/server/router/index.ts +0 -28
  278. package/dist/server/sync/memory-adapter.d.ts +0 -17
  279. package/src/build/server-routes-entry.ts +0 -30
  280. package/src/server/dev-inspector.ts +0 -60
  281. package/src/server/router/index.ts +0 -28
  282. package/src/server/sync/memory-adapter.ts +0 -196
@@ -1,11 +1,13 @@
1
1
  import { existsSync, rmSync, writeFileSync } from "node:fs";
2
2
  import { join, relative, resolve } from "node:path";
3
3
  import { type BunTargetApp, createBuildFingerprint } from "../adapter/bun.ts";
4
+ import { runBunBuild } from "../build/bun-build.ts";
4
5
  import { buildClient } from "../build/client.ts";
5
6
  import { buildEntrySource } from "../build/entry-template.ts";
6
7
  import { copyDirRecursive, ensureDir, toPosixPath } from "../build/shared.ts";
7
8
  import { buildSSGCacheSnapshot } from "../build/ssg-cache.ts";
8
9
  import type { BuildAppOptions, PackageTargetBuildManifest } from "../build/types.ts";
10
+ import { createRoutesPlugin, routeModuleSpecifier, routeSourcePaths } from "../plugin/routes.ts";
9
11
  import { ssgRouteCache } from "../server/cache/ssg.ts";
10
12
  import { generateProdIndexHtml } from "../server/render/shell.ts";
11
13
  import { setProductionTemplateContent } from "../server/render/template.ts";
@@ -46,12 +48,13 @@ export async function buildPackageTarget(
46
48
  ensureDir(targetDir);
47
49
 
48
50
  const { entryChunk, cssChunks } = await buildClient(routes, {
51
+ basePath: prefix,
52
+ clientLogging: options.clientLogging ?? false,
49
53
  outDir: targetDir,
50
- rootLayout: root.path,
54
+ pagesDir,
51
55
  plugins: options.plugins,
52
56
  publicPath: `${prefix}/_client/`,
53
- basePath: prefix,
54
- clientLogging: options.clientLogging ?? false,
57
+ rootLayout: root.path,
55
58
  });
56
59
 
57
60
  // Same fingerprint as the Bun target: hashing only the client chunks would
@@ -108,9 +111,9 @@ export async function buildPackageTarget(
108
111
  routeMetadata[toPosixPath(route.path)] = {
109
112
  segmentBoundaries: route.segmentBoundaries.map((b) => ({
110
113
  depth: b.depth,
111
- path: toPosixPath(b.path),
112
114
  errorPath: b.errorPath ? toPosixPath(b.errorPath) : undefined,
113
115
  notFoundPath: b.notFoundPath ? toPosixPath(b.notFoundPath) : undefined,
116
+ path: toPosixPath(b.path),
114
117
  })),
115
118
  };
116
119
  }
@@ -120,11 +123,14 @@ export async function buildPackageTarget(
120
123
  apps: [
121
124
  {
122
125
  buildId,
126
+ clientLogging: options.clientLogging ?? false,
127
+ modulePaths: routeSourcePaths({ pagesDir, prefix }),
128
+ nativeRoutes: routeModuleSpecifier(app),
123
129
  prefix,
124
- rootPath: root.path,
125
- routes: routes.map((r) => ({ pattern: r.pattern, path: r.path, mode: r.mode })),
126
130
  rootConventions,
131
+ rootPath: root.path,
127
132
  routeMetadata,
133
+ routes: routes.map((r) => ({ mode: r.mode, path: r.path, pattern: r.pattern })),
128
134
  ssgCache,
129
135
  },
130
136
  ],
@@ -134,18 +140,21 @@ export async function buildPackageTarget(
134
140
  const registerEntry = join(targetDir, "register.ts");
135
141
  writeFileSync(registerEntry, registerSource);
136
142
 
137
- const result = await Bun.build({
143
+ const result = await runBunBuild({
138
144
  entrypoints: [registerEntry],
145
+ minify: false,
146
+ naming: { chunk: "[name]-[hash].[ext]", entry: "[name].[ext]" },
139
147
  outdir: targetDir,
140
- target: "bun",
141
148
  // Keep EVERY dependency external (incl. @teyik0/furin): the host must
142
149
  // share one copy of the furin runtime with this register module — only
143
150
  // the package's own page sources get bundled in.
144
151
  packages: "external",
145
- naming: { entry: "[name].[ext]", chunk: "[name]-[hash].[ext]" },
152
+ plugins: [
153
+ ...(options.plugins ?? []),
154
+ createRoutesPlugin({ instances: [app], target: "server" }),
155
+ ],
146
156
  sourcemap: "none",
147
- minify: false,
148
- plugins: options.plugins,
157
+ target: "bun",
149
158
  });
150
159
  if (!result.success) {
151
160
  throw new AggregateError(result.logs, "[furin] package register build failed");
@@ -170,7 +179,7 @@ const CLIENT_DIR = fileURLToPath(new URL("./client", import.meta.url));
170
179
  *
171
180
  * @param {object} [options] Extra furin() options (logger, sync, ...) —
172
181
  * pagesDir/prefix/clientDir are baked into the package and cannot be overridden.
173
- * @returns {Promise<import("elysia").Elysia>}
182
+ * @returns {ReturnType<typeof import("@teyik0/furin").furin>}
174
183
  */
175
184
  export function createFurinApp(options = {}) {
176
185
  return furin({
@@ -185,12 +194,11 @@ export const prefix = ${JSON.stringify(prefix)};
185
194
  `;
186
195
  writeFileSync(join(targetDir, "index.js"), factorySource);
187
196
 
188
- const dtsSource = `import type { Elysia } from "elysia";
189
- import type { FurinOptions } from "@teyik0/furin";
197
+ const dtsSource = `import type { FurinOptions, furin } from "@teyik0/furin";
190
198
 
191
199
  /** Extra furin() options — pagesDir/prefix/clientDir are baked into the package and cannot be overridden. */
192
200
  export type CreateFurinAppOptions = Omit<FurinOptions, "pagesDir" | "prefix" | "clientDir">;
193
- export declare function createFurinApp(options?: CreateFurinAppOptions): Promise<Elysia>;
201
+ export declare function createFurinApp(options?: CreateFurinAppOptions): ReturnType<typeof furin>;
194
202
  export declare const prefix: string;
195
203
  `;
196
204
  writeFileSync(join(targetDir, "index.d.ts"), dtsSource);
@@ -1,3 +1,4 @@
1
+ // biome-ignore-all lint/performance/noAwaitInLoops: static build emits routes in sequence to keep output deterministic
1
2
  import { cpSync, existsSync, rmSync, writeFileSync } from "node:fs";
2
3
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
4
  import { buildClient } from "../build/client.ts";
@@ -5,14 +6,11 @@ import { ensureDir, toPosixPath } from "../build/shared.ts";
5
6
  import type { BuildAppOptions, StaticTargetBuildManifest } from "../build/types.ts";
6
7
  import type { StaticExportConfig } from "../config.ts";
7
8
  import { resolvePath } from "../server/render/assemble.ts";
8
- import { prerenderSSG } from "../server/render/index.ts";
9
9
  import { generateProdIndexHtml } from "../server/render/shell.ts";
10
+ import { prerenderSSG } from "../server/render/ssg.ts";
10
11
  import { setProductionTemplateContent } from "../server/render/template.ts";
11
- import {
12
- createSearchRouteMetadata,
13
- type ResolvedRoute,
14
- type RootLayout,
15
- } from "../server/router/index.ts";
12
+ import { createSearchRouteMetadata } from "../server/router/schemas.ts";
13
+ import type { ResolvedRoute, RootLayout } from "../server/router/types.ts";
16
14
  import type { SearchRouteMetadata } from "../shared/search-params.ts";
17
15
  import { mapWithConcurrency } from "../shared/utils/index.ts";
18
16
 
@@ -51,6 +49,38 @@ function pathToOutputFile(urlPath: string, outDir: string, fileName: string): st
51
49
  return resolved;
52
50
  }
53
51
 
52
+ function sortRouteList(routes: string[]): string[] {
53
+ return routes.toSorted((a, b) => (a < b ? -1 : Number(a > b)));
54
+ }
55
+
56
+ function assertNoStaticExportSkips(onSSR: "error" | "skip", skippedRoutes: string[]): void {
57
+ if (onSSR !== "error" || skippedRoutes.length === 0) {
58
+ return;
59
+ }
60
+ const failed = sortRouteList(skippedRoutes);
61
+ const list = failed.map((r) => ` • ${r}`).join("\n");
62
+ throw new Error(
63
+ `[furin] static: ${failed.length} route(s) cannot be statically exported:\n${list}\n` +
64
+ `Define \`staticParams()\` for dynamic SSG routes or set \`onSSR: "skip"\` in your static config.`
65
+ );
66
+ }
67
+
68
+ function assertNoPrerenderSkips(
69
+ onSSR: "error" | "skip",
70
+ skippedRoutes: string[],
71
+ afterQueueCount: number
72
+ ): void {
73
+ if (onSSR !== "error" || skippedRoutes.length <= afterQueueCount) {
74
+ return;
75
+ }
76
+ const failed = sortRouteList(skippedRoutes.slice(afterQueueCount));
77
+ const list = failed.map((r) => ` • ${r}`).join("\n");
78
+ throw new Error(
79
+ `[furin] static: ${failed.length} route(s) failed to pre-render:\n${list}\n` +
80
+ `Set \`onSSR: "skip"\` in your static config to suppress this error.`
81
+ );
82
+ }
83
+
54
84
  // ── Pre-render worker ─────────────────────────────────────────────────────────
55
85
 
56
86
  async function prerenderAndWrite(
@@ -165,18 +195,18 @@ async function buildTaskQueue(
165
195
  STATIC_CONCURRENCY,
166
196
  async (route) => {
167
197
  if (!DYNAMIC_SEGMENT_RE.test(route.pattern)) {
168
- return { route, paramSets: [{}] };
198
+ return { paramSets: [{}], route };
169
199
  }
170
200
  if (!route.page.staticParams) {
171
201
  console.warn(
172
202
  `[furin] static: skipping dynamic route "${route.pattern}" — no staticParams() defined.`
173
203
  );
174
204
  skippedRoutes.push(route.pattern);
175
- return { route, paramSets: null };
205
+ return { paramSets: null, route };
176
206
  }
177
207
  try {
178
208
  const paramSets = (await route.page.staticParams()) ?? [];
179
- return { route, paramSets };
209
+ return { paramSets, route };
180
210
  } catch (err) {
181
211
  return { error: err, paramSets: null, route };
182
212
  }
@@ -184,12 +214,10 @@ async function buildTaskQueue(
184
214
  );
185
215
 
186
216
  for (const result of staticParamsResults) {
217
+ const { pattern } = result.route;
187
218
  if ("error" in result) {
188
- console.error(
189
- `[furin] static: staticParams() failed for "${result.route.pattern}":`,
190
- result.error
191
- );
192
- skippedRoutes.push(result.route.pattern);
219
+ console.error(`[furin] static: staticParams() failed for "${pattern}":`, result.error);
220
+ skippedRoutes.push(pattern);
193
221
  continue;
194
222
  }
195
223
  if (result.paramSets === null) {
@@ -200,12 +228,14 @@ async function buildTaskQueue(
200
228
  // would throw out of `for...of` and abort the entire build.
201
229
  if (!Array.isArray(result.paramSets)) {
202
230
  console.error(
203
- `[furin] static: staticParams() for "${result.route.pattern}" returned a non-array value; skipping route.`
231
+ `[furin] static: staticParams() for "${pattern}" returned a non-array value; skipping route.`
204
232
  );
205
- skippedRoutes.push(result.route.pattern);
233
+ skippedRoutes.push(pattern);
206
234
  continue;
207
235
  }
208
236
  for (const params of result.paramSets) {
237
+ const urlPath = resolvePath(pattern, params);
238
+ pathToOutputFile(urlPath, outDir, "index.html");
209
239
  tasks.push(() =>
210
240
  prerenderAndWrite(
211
241
  result.route,
@@ -243,6 +273,24 @@ async function runWithConcurrency(
243
273
 
244
274
  // ── Main adapter ──────────────────────────────────────────────────────────────
245
275
 
276
+ function assertNoRequestLoaders(routes: ResolvedRoute[], root: RootLayout): void {
277
+ const offenders: string[] = [];
278
+ if (root.route.requestLoader !== undefined) {
279
+ offenders.push(" • <root layout>");
280
+ }
281
+ const requestScopedRoutes = routes.filter((route) =>
282
+ route.routeChain.some((entry) => entry.requestLoader !== undefined)
283
+ );
284
+ offenders.push(...requestScopedRoutes.map((route) => ` • ${route.pattern}`));
285
+ if (offenders.length === 0) {
286
+ return;
287
+ }
288
+ throw new Error(
289
+ "[furin] requestLoader requires a Bun server and is incompatible with a pure static export.\n" +
290
+ offenders.join("\n")
291
+ );
292
+ }
293
+
246
294
  export async function buildStaticTarget(
247
295
  routes: ResolvedRoute[],
248
296
  rootDir: string,
@@ -250,6 +298,8 @@ export async function buildStaticTarget(
250
298
  root: RootLayout,
251
299
  options: BuildAppOptions
252
300
  ): Promise<StaticTargetBuildManifest> {
301
+ assertNoRequestLoaders(routes, root);
302
+
253
303
  const staticConfig: StaticExportConfig = options.staticConfig ?? {};
254
304
 
255
305
  // Normalize basePath: treat "" and "/" identically as no prefix; strip any
@@ -311,7 +361,22 @@ export async function buildStaticTarget(
311
361
  const ssgRoutes = collectSsgRoutes(routes, onSSR, skippedRoutes);
312
362
  const searchRoutes = createSearchRouteMetadata(ssgRoutes);
313
363
 
314
- // ── 2. Clean & create output directories ─────────────────────────────────
364
+ // ── 2. Resolve and validate every output path before writing artifacts ────
365
+ const tasks = await buildTaskQueue(
366
+ ssgRoutes,
367
+ root,
368
+ outDir,
369
+ renderedRoutes,
370
+ skippedRoutes,
371
+ basePath,
372
+ searchRoutes
373
+ );
374
+ assertNoStaticExportSkips(onSSR, skippedRoutes);
375
+ // Snapshot after queue-building: routes skipped due to missing staticParams() are
376
+ // already recorded; only routes added during task execution are prerender failures.
377
+ const afterQueueCount = skippedRoutes.length;
378
+
379
+ // ── 3. Clean & create output directories ─────────────────────────────────
315
380
  rmSync(outDir, { force: true, recursive: true });
316
381
  ensureDir(outDir);
317
382
 
@@ -319,71 +384,53 @@ export async function buildStaticTarget(
319
384
  rmSync(targetDir, { force: true, recursive: true });
320
385
  ensureDir(targetDir);
321
386
 
322
- // ── 3. Build client bundle ────────────────────────────────────────────────
387
+ // ── 4. Build client bundle ────────────────────────────────────────────────
323
388
  const { entryChunk, cssChunks } = await buildClient(ssgRoutes, {
389
+ basePath,
390
+ clientLogging: Boolean(options.clientLogging),
324
391
  outDir: targetDir,
325
- rootLayout: root.path,
392
+ pagesDir: dirname(root.path),
326
393
  plugins: options.plugins,
327
394
  publicPath,
328
- basePath,
329
- clientLogging: Boolean(options.clientLogging),
395
+ rootLayout: root.path,
330
396
  });
331
397
 
332
- // ── 4. Generate HTML shell template ──────────────────────────────────────
398
+ // ── 5. Generate HTML shell template ──────────────────────────────────────
333
399
  // Emit a <link rel="icon"> only when a favicon.ico exists in the public dir.
334
400
  // This ensures the browser finds it even when the site is served from a sub-path.
335
401
  const publicFavicon = join(rootDir, "public", "favicon.ico");
336
402
  const faviconHref = existsSync(publicFavicon) ? `${basePath}/favicon.ico` : undefined;
337
403
  const shellHtml = generateProdIndexHtml(entryChunk, cssChunks, undefined, faviconHref, true);
338
404
 
339
- // ── 5. Prime the renderer with the production shell ──────────────────────
405
+ // ── 6. Prime the renderer with the production shell ──────────────────────
340
406
  // Must be called before any prerenderSSG invocation.
341
407
  // prepareRender() checks getProductionTemplate() first (before IS_DEV), so
342
408
  // setting the content here is sufficient — no need to flip IS_DEV globally.
343
409
  setProductionTemplateContent(shellHtml);
344
410
 
345
- // ── 6. Copy public/ → outDir/ ─────────────────────────────────────────────
411
+ // ── 7. Copy public/ → outDir/ ─────────────────────────────────────────────
346
412
  const publicSrcDir = join(rootDir, "public");
347
413
  if (existsSync(publicSrcDir)) {
348
414
  cpSync(publicSrcDir, outDir, { recursive: true });
349
415
  }
350
416
 
351
- // ── 7. Copy _client/ chunks → outDir/_client/ ────────────────────────────
417
+ // ── 8. Copy _client/ chunks → outDir/_client/ ────────────────────────────
352
418
  const clientSrcDir = join(targetDir, "client");
353
419
  if (existsSync(clientSrcDir)) {
354
420
  cpSync(clientSrcDir, join(outDir, "_client"), { recursive: true });
355
421
  }
356
422
 
357
- // ── 8. Pre-render SSG routes ──────────────────────────────────────────────
358
- const tasks = await buildTaskQueue(
359
- ssgRoutes,
360
- root,
361
- outDir,
362
- renderedRoutes,
363
- skippedRoutes,
364
- basePath,
365
- searchRoutes
366
- );
367
- // Snapshot after queue-building: routes skipped due to missing staticParams() are
368
- // already recorded; only routes added during task execution are prerender failures.
369
- const afterQueueCount = skippedRoutes.length;
423
+ // ── 9. Pre-render SSG routes ──────────────────────────────────────────────
370
424
  await runWithConcurrency(tasks, STATIC_CONCURRENCY);
371
425
 
372
426
  // Fail the build when onSSR="error" and any prerender task was recorded as skipped.
373
- if (onSSR === "error" && skippedRoutes.length > afterQueueCount) {
374
- const failed = skippedRoutes.slice(afterQueueCount);
375
- const list = failed.map((r) => ` • ${r}`).join("\n");
376
- throw new Error(
377
- `[furin] static: ${failed.length} route(s) failed to pre-render:\n${list}\n` +
378
- `Set \`onSSR: "skip"\` in your static config to suppress this error.`
379
- );
380
- }
427
+ assertNoPrerenderSkips(onSSR, skippedRoutes, afterQueueCount);
381
428
 
382
- // ── 9. Write 404.html (SPA fallback for GitHub Pages) ───────────────────
429
+ // ── 10. Write 404.html (SPA fallback for GitHub Pages) ──────────────────
383
430
  writeFileSync(join(outDir, "404.html"), shellHtml);
384
431
  console.log("[furin] static: wrote 404.html (SPA shell fallback)");
385
432
 
386
- // ── 10. Clean up intermediate build artifacts ────────────────────────────
433
+ // ── 11. Clean up intermediate build artifacts ───────────────────────────
387
434
  rmSync(targetDir, { force: true, recursive: true });
388
435
 
389
436
  console.log(
@@ -395,7 +442,7 @@ export async function buildStaticTarget(
395
442
  basePath,
396
443
  generatedAt: new Date().toISOString(),
397
444
  outDir: toPosixPath(outDir),
398
- renderedRoutes,
399
- skippedRoutes,
445
+ renderedRoutes: sortRouteList(renderedRoutes),
446
+ skippedRoutes: sortRouteList(skippedRoutes),
400
447
  };
401
448
  }
@@ -0,0 +1,11 @@
1
+ type BunBuild = typeof Bun.build;
2
+
3
+ type BunBuildGlobal = typeof globalThis & {
4
+ __FURIN_BUN_BUILD_FOR_TESTS?: BunBuild;
5
+ };
6
+
7
+ export function runBunBuild(config: Bun.BuildConfig): ReturnType<BunBuild> {
8
+ return (((globalThis as BunBuildGlobal).__FURIN_BUN_BUILD_FOR_TESTS ?? Bun.build) as BunBuild)(
9
+ config
10
+ );
11
+ }
@@ -1,12 +1,16 @@
1
1
  import { existsSync, mkdirSync, writeFileSync } from "node:fs";
2
2
  import { basename, join } from "node:path";
3
3
  import { transformForClient } from "../plugin/transform-client";
4
- import type { ResolvedRoute } from "../server/router/index.ts";
4
+ import { createRoutesPlugin } from "../plugin/routes.ts";
5
+ import { environmentGuardPlugin } from "../rsc/build/environment.ts";
6
+ import { detectLoaderFromPath } from "../server/lang-detect.ts";
7
+ import type { ResolvedRoute } from "../server/router/types.ts";
8
+ import { runBunBuild } from "./bun-build.ts";
5
9
  import { generateHydrateEntry } from "./hydrate";
6
10
  import { CLIENT_MODULE_PATH, LINK_MODULE_PATH, SEARCH_MODULE_PATH } from "./shared";
7
11
  import type { BuildClientOptions, BunBuildAliasConfig } from "./types";
8
12
 
9
- const TS_FILE_FILTER = /\.(tsx|ts)$/;
13
+ const SCRIPT_FILE_FILTER = /\.(tsx?|jsx?)$/;
10
14
 
11
15
  export interface BuildClientResult {
12
16
  /** Public paths of all CSS chunks, e.g. `["/_client/chunk-abc.css"]` */
@@ -33,7 +37,16 @@ export interface BuildClientResult {
33
37
  */
34
38
  export async function buildClient(
35
39
  routes: ResolvedRoute[],
36
- { outDir, rootLayout, plugins, publicPath, basePath, clientLogging, clientDirName }: BuildClientOptions
40
+ {
41
+ outDir,
42
+ rootLayout,
43
+ plugins,
44
+ publicPath,
45
+ basePath,
46
+ clientLogging,
47
+ clientDirName,
48
+ pagesDir,
49
+ }: BuildClientOptions
37
50
  ): Promise<BuildClientResult> {
38
51
  // Per-app client dir so several mounted apps build side by side
39
52
  // ("client", "client-admin", …) without clobbering each other.
@@ -59,34 +72,29 @@ export async function buildClient(
59
72
  const transformPlugin: Bun.BunPlugin = {
60
73
  name: "furin-transform-client",
61
74
  setup(build) {
62
- build.onLoad({ filter: TS_FILE_FILTER }, async (args) => {
75
+ build.onLoad({ filter: SCRIPT_FILE_FILTER }, async (args) => {
63
76
  const { path } = args;
64
77
  if (path.includes("node_modules")) {
65
78
  return;
66
79
  }
67
80
 
68
81
  const code = await Bun.file(path).text();
69
- try {
70
- const result = transformForClient(code, path);
71
- // transformForClient now emits TS/TSX directly (no pre-transpile),
72
- // so JSX → React handling is delegated to Bun.build's loader, which
73
- // applies the project tsconfig's automatic runtime by default.
74
- const transformed = result.code
75
- .replaceAll(`"@teyik0/furin/client"`, JSON.stringify(CLIENT_MODULE_PATH))
76
- .replaceAll(`'furin/client'`, JSON.stringify(CLIENT_MODULE_PATH))
77
- .replaceAll(`"@teyik0/furin/link"`, JSON.stringify(LINK_MODULE_PATH))
78
- .replaceAll(`'furin/link'`, JSON.stringify(LINK_MODULE_PATH))
79
- .replaceAll(`"@teyik0/furin/search"`, JSON.stringify(SEARCH_MODULE_PATH))
80
- .replaceAll(`'furin/search'`, JSON.stringify(SEARCH_MODULE_PATH));
82
+ const result = transformForClient(code, path);
83
+ // transformForClient now emits TS/TSX directly (no pre-transpile),
84
+ // so JSX → React handling is delegated to Bun.build's loader, which
85
+ // applies the project tsconfig's automatic runtime by default.
86
+ const transformed = result.code
87
+ .replaceAll(`"@teyik0/furin/client"`, JSON.stringify(CLIENT_MODULE_PATH))
88
+ .replaceAll(`'furin/client'`, JSON.stringify(CLIENT_MODULE_PATH))
89
+ .replaceAll(`"@teyik0/furin/link"`, JSON.stringify(LINK_MODULE_PATH))
90
+ .replaceAll(`'furin/link'`, JSON.stringify(LINK_MODULE_PATH))
91
+ .replaceAll(`"@teyik0/furin/search"`, JSON.stringify(SEARCH_MODULE_PATH))
92
+ .replaceAll(`'furin/search'`, JSON.stringify(SEARCH_MODULE_PATH));
81
93
 
82
- return {
83
- contents: transformed,
84
- loader: path.endsWith(".tsx") ? "tsx" : "ts",
85
- };
86
- } catch (error) {
87
- console.error(`[furin] Transform error for ${path}:`, error);
88
- return;
89
- }
94
+ return {
95
+ contents: transformed,
96
+ loader: detectLoaderFromPath(path),
97
+ };
90
98
  });
91
99
  },
92
100
  };
@@ -114,7 +122,14 @@ export async function buildClient(
114
122
  // Overridable via the `publicPath` option (e.g. "/furin/_client/" for basePath builds).
115
123
  publicPath,
116
124
  // User plugins run before the internal transform so they pre-process files first
117
- plugins: plugins ? [...plugins, transformPlugin] : [transformPlugin],
125
+ plugins: [
126
+ ...(plugins ?? []),
127
+ ...(pagesDir
128
+ ? [createRoutesPlugin({ instances: [{ pagesDir, prefix: basePath }], target: "client" })]
129
+ : []),
130
+ environmentGuardPlugin("client"),
131
+ transformPlugin,
132
+ ],
118
133
  alias: {
119
134
  "@teyik0/furin/client": CLIENT_MODULE_PATH,
120
135
  "@teyik0/furin/link": LINK_MODULE_PATH,
@@ -125,7 +140,7 @@ export async function buildClient(
125
140
  },
126
141
  };
127
142
 
128
- const result = await Bun.build(clientBuildConfig);
143
+ const result = await runBunBuild(clientBuildConfig);
129
144
  for (const output of result.outputs) {
130
145
  console.log(`[furin] ${output.path} (${(output.size / 1024).toFixed(1)} KB)`);
131
146
  }
@@ -20,10 +20,16 @@ const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/server/runtime-env${_ext}`;
20
20
  /** One app's compile context payload — the generated entry can carry several. */
21
21
  export interface EntryAppContext {
22
22
  buildId?: string;
23
+ /** Whether the emitted hydration client sends browser log batches. */
24
+ clientLogging?: boolean;
23
25
  /** Extra lines injected inside this app's `__setCompileContext({...})` call. */
24
26
  extraContext?: string[];
25
27
  /** Extra import lines this app needs (embedded asset imports). */
26
28
  extraImports?: string[];
29
+ /** Additional route modules such as filesystem-derived `_route` layouts. */
30
+ modulePaths?: string[];
31
+ /** Virtual module specifier exporting this app's composed Elysia route tree. */
32
+ nativeRoutes?: string;
27
33
  /** Mount prefix baked into the context for runtime lookup (`""` = root). */
28
34
  prefix?: string;
29
35
  rootConventions?: { errorPath?: string; notFoundPath?: string };
@@ -110,9 +116,12 @@ function buildAppContextBlock(
110
116
  varPrefix: string
111
117
  ): { contextLines: string[]; importLines: string[] } {
112
118
  const allModulePaths = [
113
- app.rootPath,
114
- ...app.routes.map((r) => r.path),
115
- ...collectConventionPaths(app.rootConventions, app.routeMetadata),
119
+ ...new Set([
120
+ app.rootPath,
121
+ ...app.routes.map((r) => r.path),
122
+ ...(app.modulePaths ?? []),
123
+ ...collectConventionPaths(app.rootConventions, app.routeMetadata),
124
+ ]),
116
125
  ];
117
126
  const importLines: string[] = [];
118
127
  const moduleEntries: string[] = [];
@@ -123,6 +132,12 @@ function buildAppContextBlock(
123
132
  importLines.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
124
133
  moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
125
134
  }
135
+ const nativeRoutesVar = `${varPrefix}furinApp`;
136
+ if (app.nativeRoutes) {
137
+ importLines.push(
138
+ `import { furinApp as ${nativeRoutesVar} } from ${JSON.stringify(app.nativeRoutes)};`
139
+ );
140
+ }
126
141
 
127
142
  const routeEntries = app.routes.map(
128
143
  (r) =>
@@ -140,8 +155,10 @@ function buildAppContextBlock(
140
155
  const contextLines = [
141
156
  "__setCompileContext({",
142
157
  ` buildId: ${JSON.stringify(app.buildId ?? "")},`,
158
+ ` clientLogging: ${JSON.stringify(app.clientLogging ?? false)},`,
143
159
  ` prefix: ${JSON.stringify(app.prefix ?? "")},`,
144
160
  ` rootPath: ${JSON.stringify(app.rootPath.replace(/\\/g, "/"))},`,
161
+ app.nativeRoutes ? ` nativeRoutes: ${nativeRoutesVar},` : "",
145
162
  rootConventionsLine,
146
163
  " modules: {",
147
164
  ...moduleEntries,