@teyik0/furin 0.2.0-alpha.4 → 0.3.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 (263) hide show
  1. package/README.md +2 -3
  2. package/dist/adapter/bun.d.ts +17 -1
  3. package/dist/adapter/package.d.ts +26 -0
  4. package/dist/adapter/static.d.ts +1 -1
  5. package/dist/build/bun-build.d.ts +3 -0
  6. package/dist/build/client.d.ts +1 -1
  7. package/dist/build/compile-entry.d.ts +3 -3
  8. package/dist/build/compile-entry.ts +88 -83
  9. package/dist/build/entry-template.d.ts +37 -9
  10. package/dist/build/entry-template.ts +109 -56
  11. package/dist/build/hydrate.d.ts +2 -2
  12. package/dist/build/index.js +6817 -4544
  13. package/dist/build/production-instrumentation.d.ts +1 -0
  14. package/dist/build/route-types.d.ts +1 -1
  15. package/dist/build/scan-server.d.ts +10 -4
  16. package/dist/build/server-routes-entry.d.ts +3 -3
  17. package/dist/build/server-routes-entry.ts +5 -11
  18. package/dist/build/ssg-cache.d.ts +1 -1
  19. package/dist/build/types.d.ts +37 -1
  20. package/dist/cli/config.d.ts +6 -1
  21. package/dist/cli/index.js +6854 -4549
  22. package/dist/client/boundaries.d.ts +4 -4
  23. package/dist/client/internal/runtime-types.d.ts +30 -0
  24. package/dist/client/link.d.ts +2 -1
  25. package/dist/client/link.js +712 -338
  26. package/dist/client/router/boundary-tree.d.ts +1 -1
  27. package/dist/client/router/index.d.ts +1 -1
  28. package/dist/client/router/link-utils.d.ts +5 -1
  29. package/dist/client/router/sync-catch-up.d.ts +4 -4
  30. package/dist/client/router/types.d.ts +2 -2
  31. package/dist/client-only.d.ts +1 -0
  32. package/dist/client-only.js +1 -0
  33. package/dist/client.d.ts +108 -111
  34. package/dist/client.js +16 -17
  35. package/dist/config.d.ts +11 -2
  36. package/dist/config.js +7 -3
  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 +40 -21
  40. package/dist/furin.js +7198 -3518
  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 +933 -423
  45. package/dist/plugin/transform-client.d.ts +0 -2
  46. package/dist/plugin/transform-isomorphic.d.ts +9 -0
  47. package/dist/rsc/build/environment.d.ts +2 -0
  48. package/dist/rsc/build/index.d.ts +15 -0
  49. package/dist/rsc/client-codec.d.ts +1 -0
  50. package/dist/rsc/codec.d.ts +4 -0
  51. package/dist/rsc/flight-drain.d.ts +11 -0
  52. package/dist/rsc/render-error.d.ts +17 -0
  53. package/dist/rsc/server-codec.d.ts +2 -0
  54. package/dist/rsc/server-codec.js +24 -0
  55. package/dist/rsc/shared.d.ts +30 -0
  56. package/dist/rsc/symbols.d.ts +2 -0
  57. package/dist/rsc/transport-benchmark.d.ts +13 -0
  58. package/dist/rsc/version.d.ts +7 -0
  59. package/dist/rsc-client.d.ts +4 -0
  60. package/dist/rsc-client.js +166 -0
  61. package/dist/rsc-server.d.ts +11 -0
  62. package/dist/rsc.d.ts +11 -0
  63. package/dist/rsc.js +353 -0
  64. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  65. package/dist/server/cache/dev-loader.d.ts +3 -2
  66. package/dist/server/cache/index.d.ts +2 -2
  67. package/dist/server/cache/invalidation.d.ts +20 -2
  68. package/dist/server/cache/isr-ssg.d.ts +5 -1
  69. package/dist/server/cache/isr.d.ts +15 -2
  70. package/dist/server/cache/registry.d.ts +3 -3
  71. package/dist/server/cache/route-cache.d.ts +10 -8
  72. package/dist/server/cache/ssg.d.ts +6 -2
  73. package/dist/server/cache/store-view.d.ts +11 -0
  74. package/dist/server/dev-page-plugin.d.ts +4 -0
  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/instance.d.ts +86 -0
  81. package/dist/server/internal.d.ts +18 -1
  82. package/dist/server/internal.js +74 -7
  83. package/dist/server/lang-detect.d.ts +1 -0
  84. package/dist/server/render/assemble.d.ts +4 -0
  85. package/dist/server/render/element.d.ts +1 -1
  86. package/dist/server/render/index.d.ts +2 -2
  87. package/dist/server/render/index.ts +6 -2
  88. package/dist/server/render/loaders.d.ts +8 -0
  89. package/dist/server/render/not-found.d.ts +1 -1
  90. package/dist/server/render/ppr-route.d.ts +8 -0
  91. package/dist/server/render/route-frame-transport.d.ts +2 -0
  92. package/dist/server/render/shell.ts +0 -1
  93. package/dist/server/render/ssr.d.ts +2 -3
  94. package/dist/server/render/template.d.ts +3 -2
  95. package/dist/server/router/discovery.d.ts +1 -1
  96. package/dist/server/router/hmr.d.ts +1 -1
  97. package/dist/server/router/index.d.ts +2 -1
  98. package/dist/server/router/index.js +23 -23
  99. package/dist/server/router/index.ts +1 -1
  100. package/dist/server/router/patterns.d.ts +1 -1
  101. package/dist/server/router/plugin.d.ts +4 -7
  102. package/dist/server/router/schema-merge.d.ts +3 -0
  103. package/dist/server/router/schemas.d.ts +12 -7
  104. package/dist/server/router/types.d.ts +1 -1
  105. package/dist/server/runtime-env.js +2 -2
  106. package/dist/server/sync/adapter.d.ts +45 -26
  107. package/dist/server/sync/config.d.ts +9 -2
  108. package/dist/server/sync/fingerprint.d.ts +2 -1
  109. package/dist/server/sync/index.d.ts +15 -4
  110. package/dist/server/sync/index.js +2868 -0
  111. package/dist/server/sync/notifier.d.ts +18 -0
  112. package/dist/server/sync/plugin.d.ts +53 -54
  113. package/dist/server/sync/postgres/index.d.ts +19 -0
  114. package/dist/server/sync/postgres/index.js +252 -0
  115. package/dist/server/sync/postgres/migrate.d.ts +2 -0
  116. package/dist/server/sync/postgres/migrate.js +16 -0
  117. package/dist/server/sync/postgres/migration.sql +66 -0
  118. package/dist/server/sync/redis/index.d.ts +30 -0
  119. package/dist/server/sync/redis/index.js +336 -0
  120. package/dist/server/sync/redis/scripts.d.ts +4 -0
  121. package/dist/server/sync/response.d.ts +10 -1
  122. package/dist/server/sync/runtime.d.ts +6 -0
  123. package/dist/server/sync/sqlite/index.d.ts +20 -0
  124. package/dist/server/sync/sqlite/index.js +250 -0
  125. package/dist/server/sync/stream.d.ts +12 -4
  126. package/dist/server-only.d.ts +1 -0
  127. package/dist/server-only.js +1 -0
  128. package/dist/shared/parser.d.ts +2 -0
  129. package/dist/shared/prefix.d.ts +15 -0
  130. package/dist/shared/route-frame.d.ts +49 -0
  131. package/dist/shared/utils/index.d.ts +1 -1
  132. package/package.json +59 -12
  133. package/src/adapter/bun.ts +175 -55
  134. package/src/adapter/package.ts +208 -0
  135. package/src/adapter/static.ts +94 -48
  136. package/src/build/bun-build.ts +11 -0
  137. package/src/build/client.ts +31 -27
  138. package/src/build/compile-entry.ts +88 -83
  139. package/src/build/entry-template.ts +109 -56
  140. package/src/build/hydrate.ts +37 -10
  141. package/src/build/index.ts +111 -28
  142. package/src/build/production-instrumentation.ts +14 -0
  143. package/src/build/route-types.ts +7 -8
  144. package/src/build/scan-server.ts +26 -9
  145. package/src/build/server-routes-entry.ts +5 -11
  146. package/src/build/ssg-cache.ts +13 -4
  147. package/src/build/types.ts +38 -1
  148. package/src/cli/config.ts +9 -3
  149. package/src/cli/index.ts +73 -24
  150. package/src/client/boundaries.tsx +42 -18
  151. package/src/client/default-screens.tsx +4 -4
  152. package/src/client/internal/runtime-types.ts +31 -0
  153. package/src/client/link.tsx +35 -37
  154. package/src/client/router/boundary-tree.tsx +2 -2
  155. package/src/client/router/context.ts +13 -9
  156. package/src/client/router/index.ts +3 -0
  157. package/src/client/router/link-utils.ts +66 -3
  158. package/src/client/router/navigation.ts +9 -15
  159. package/src/client/router/provider.tsx +74 -52
  160. package/src/client/router/search/index.ts +5 -7
  161. package/src/client/router/search-store.ts +5 -1
  162. package/src/client/router/spa-response.ts +3 -4
  163. package/src/client/router/sync-catch-up.ts +9 -8
  164. package/src/client/router/types.ts +2 -2
  165. package/src/client/sync.ts +3 -2
  166. package/src/client-only.ts +1 -0
  167. package/src/client.ts +221 -174
  168. package/src/config.ts +14 -1
  169. package/src/devtools/devtools-element.js +884 -0
  170. package/src/devtools/protocol.ts +139 -0
  171. package/src/furin.ts +414 -171
  172. package/src/isomorphic.ts +66 -0
  173. package/src/plugin/binding-scope.ts +158 -0
  174. package/src/plugin/dead-code-elimination.ts +202 -0
  175. package/src/plugin/index.ts +14 -18
  176. package/src/plugin/transform-client.ts +237 -273
  177. package/src/plugin/transform-isomorphic.ts +464 -0
  178. package/src/rsc/build/environment.ts +30 -0
  179. package/src/rsc/build/index.ts +94 -0
  180. package/src/rsc/client-codec.ts +11 -0
  181. package/src/rsc/codec.ts +85 -0
  182. package/src/rsc/flight-drain.ts +68 -0
  183. package/src/rsc/react-server-dom-webpack.d.ts +23 -0
  184. package/src/rsc/render-error.ts +120 -0
  185. package/src/rsc/server-codec.ts +25 -0
  186. package/src/rsc/shared.tsx +113 -0
  187. package/src/rsc/symbols.ts +2 -0
  188. package/src/rsc/transport-benchmark.ts +73 -0
  189. package/src/rsc/version.ts +57 -0
  190. package/src/rsc-client.ts +12 -0
  191. package/src/rsc-server.tsx +91 -0
  192. package/src/rsc.tsx +75 -0
  193. package/src/server/auto-invalidate/registry.ts +23 -1
  194. package/src/server/auto-invalidate/runtime.ts +58 -7
  195. package/src/server/cache/dev-loader.ts +143 -44
  196. package/src/server/cache/index.ts +7 -3
  197. package/src/server/cache/invalidation.ts +100 -41
  198. package/src/server/cache/isr-ssg.ts +12 -3
  199. package/src/server/cache/isr.ts +95 -5
  200. package/src/server/cache/registry.ts +17 -20
  201. package/src/server/cache/route-cache.ts +49 -36
  202. package/src/server/cache/ssg.ts +19 -4
  203. package/src/server/cache/store-view.ts +30 -0
  204. package/src/server/context-logger.ts +7 -7
  205. package/src/server/dev-page-plugin.ts +8 -6
  206. package/src/server/devtools/hub.ts +64 -0
  207. package/src/server/devtools/instrumentation.production.ts +80 -0
  208. package/src/server/devtools/instrumentation.ts +72 -0
  209. package/src/server/devtools/plugin.ts +267 -0
  210. package/src/server/devtools/request-context.ts +76 -0
  211. package/src/server/instance.ts +252 -0
  212. package/src/server/internal.ts +102 -5
  213. package/src/server/lang-detect.ts +14 -10
  214. package/src/server/render/assemble.ts +73 -10
  215. package/src/server/render/element.tsx +11 -8
  216. package/src/server/render/index.ts +6 -2
  217. package/src/server/render/isr.ts +154 -110
  218. package/src/server/render/loaders.ts +259 -45
  219. package/src/server/render/not-found.ts +18 -17
  220. package/src/server/render/ppr-route.ts +146 -0
  221. package/src/server/render/route-frame-transport.ts +62 -0
  222. package/src/server/render/shell.ts +0 -1
  223. package/src/server/render/ssg.ts +6 -5
  224. package/src/server/render/ssr.ts +170 -70
  225. package/src/server/render/template.ts +59 -25
  226. package/src/server/router/discovery.ts +39 -22
  227. package/src/server/router/hmr.ts +15 -13
  228. package/src/server/router/index.ts +1 -1
  229. package/src/server/router/patterns.ts +16 -15
  230. package/src/server/router/plugin.ts +151 -158
  231. package/src/server/router/schema-merge.ts +52 -0
  232. package/src/server/router/schemas.ts +55 -73
  233. package/src/server/router/types.ts +1 -1
  234. package/src/server/sync/adapter.ts +44 -28
  235. package/src/server/sync/config.ts +22 -9
  236. package/src/server/sync/fingerprint.ts +8 -3
  237. package/src/server/sync/index.ts +37 -13
  238. package/src/server/sync/notifier.ts +109 -0
  239. package/src/server/sync/plugin.ts +184 -90
  240. package/src/server/sync/postgres/index.ts +301 -0
  241. package/src/server/sync/postgres/migrate.ts +15 -0
  242. package/src/server/sync/postgres/migration.sql +66 -0
  243. package/src/server/sync/redis/index.ts +342 -0
  244. package/src/server/sync/redis/scripts.ts +74 -0
  245. package/src/server/sync/response.ts +141 -16
  246. package/src/server/sync/runtime.ts +27 -0
  247. package/src/server/sync/sql.d.ts +4 -0
  248. package/src/server/sync/sqlite/index.ts +337 -0
  249. package/src/server/sync/sqlite/migration.sql +70 -0
  250. package/src/server/sync/stream.ts +136 -70
  251. package/src/server-only.ts +1 -0
  252. package/src/shared/await.tsx +4 -4
  253. package/src/shared/deferred-ndjson.ts +34 -6
  254. package/src/shared/digest.ts +2 -2
  255. package/src/shared/parser.ts +5 -0
  256. package/src/shared/prefix.ts +38 -0
  257. package/src/shared/route-frame.ts +429 -0
  258. package/src/shared/search-params.ts +7 -7
  259. package/src/shared/utils/index.ts +5 -3
  260. package/dist/server/dev-inspector.d.ts +0 -89
  261. package/dist/server/sync/memory-adapter.d.ts +0 -17
  262. package/src/server/dev-inspector.ts +0 -60
  263. package/src/server/sync/memory-adapter.ts +0 -196
@@ -1,9 +1,14 @@
1
+ // biome-ignore-all lint/performance/noAwaitInLoops: build phases run in sequence because later phases consume prior artifacts
1
2
  import { existsSync, writeFileSync } from "node:fs";
2
3
  import { join, relative, resolve } from "node:path";
3
- import { buildBunTarget } from "../adapter/bun";
4
+ import { buildBunTarget, type BunTargetApp } from "../adapter/bun";
5
+ import { buildPackageTarget } from "../adapter/package";
4
6
  import { buildStaticTarget } from "../adapter/static";
5
7
  import { BUILD_TARGETS, type BuildTarget, type FurinPlugin } from "../config";
8
+ import { isomorphicTransformPlugin } from "../plugin/transform-isomorphic.ts";
9
+ import { normalizePrefix } from "../server/instance.ts";
6
10
  import { scanPages } from "../server/router/index.ts";
11
+ import { assertNoPrefixSlugCollisions } from "../shared/prefix.ts";
7
12
  import { scanFurinInstances } from "./scan-server";
8
13
  import { ensureDir, toBuildRouteManifestEntry, toPosixPath } from "./shared";
9
14
  import type { BuildAppOptions, BuildAppResult, BuildManifest } from "./types";
@@ -22,27 +27,63 @@ export type {
22
27
  TargetBuildManifest,
23
28
  } from "./types";
24
29
 
30
+ // "package" is intentionally excluded from `--target all` — it is an
31
+ // alternative packaging of ONE app, not an additional deploy target.
25
32
  const IMPLEMENTED_TARGETS = ["bun", "static"] as const satisfies BuildTarget[];
26
33
  export const BUILD_OUTPUT_DIR = ".furin/build";
34
+ let isomorphicRuntimePluginRegistered = false;
27
35
 
28
- function resolvePagesDirFromServer(serverEntry: string | null, rootDir: string): string | null {
29
- if (!serverEntry) {
30
- return null;
36
+ function registerIsomorphicRuntimePlugin(): void {
37
+ if (isomorphicRuntimePluginRegistered) {
38
+ return;
31
39
  }
32
- const detected = scanFurinInstances(serverEntry);
33
- if (detected.length === 0) {
34
- return null;
40
+ Bun.plugin(isomorphicTransformPlugin("server"));
41
+ isomorphicRuntimePluginRegistered = true;
42
+ }
43
+
44
+ /**
45
+ * Resolves the list of apps to build, in priority order:
46
+ * 1. explicit `apps` config (furin.config.ts),
47
+ * 2. explicit single `pagesDir` option (mounted at root),
48
+ * 3. every `furin({ pagesDir, prefix })` call detected in the server entry,
49
+ * 4. the `src/pages` default.
50
+ */
51
+ function resolveAppSpecs(
52
+ options: BuildAppOptions,
53
+ serverEntry: string | null,
54
+ rootDir: string
55
+ ): Array<{ pagesDir: string; prefix: string }> {
56
+ if (options.apps && options.apps.length > 0) {
57
+ // normalizePrefix is the choke point for config/CLI-provided prefixes:
58
+ // `"/"` → `""` (otherwise the bun target derives `publicPath: "//_client/"`,
59
+ // a protocol-relative URL), trailing slashes are trimmed, and invalid
60
+ // prefixes throw before any build output is written.
61
+ return options.apps.map((app) => ({
62
+ pagesDir: resolve(rootDir, app.pagesDir),
63
+ prefix: normalizePrefix(app.prefix),
64
+ }));
65
+ }
66
+ if (options.pagesDir) {
67
+ return [{ pagesDir: resolve(rootDir, options.pagesDir), prefix: "" }];
35
68
  }
36
- // Use the first detected pagesDir relative to rootDir
37
- return resolve(rootDir, detected[0] as string);
69
+ if (serverEntry) {
70
+ const detected = scanFurinInstances(serverEntry);
71
+ if (detected.length > 0) {
72
+ return detected.map((app) => ({
73
+ pagesDir: resolve(rootDir, app.pagesDir),
74
+ prefix: app.prefix,
75
+ }));
76
+ }
77
+ }
78
+ return [{ pagesDir: resolve(rootDir, "src/pages"), prefix: "" }];
38
79
  }
39
80
 
40
81
  export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
41
82
  const rootDir = resolve(options.rootDir ?? process.cwd());
42
83
  const buildRoot = join(rootDir, BUILD_OUTPUT_DIR);
43
84
  const serverEntry = (() => {
44
- // Static-only builds don't need a server entry point
45
- if (options.target === "static") {
85
+ // Static and package builds don't need a server entry point
86
+ if (options.target === "static" || options.target === "package") {
46
87
  return null;
47
88
  }
48
89
  if (options.serverEntry) {
@@ -79,11 +120,21 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
79
120
  console.debug("[furin] Skipped plugin at runtime:", err);
80
121
  }
81
122
  }
123
+ registerIsomorphicRuntimePlugin();
82
124
 
83
- // Priority: explicit config > auto-detected from server entry > default
84
- const rawPagesDir =
85
- options.pagesDir ?? resolvePagesDirFromServer(serverEntry, rootDir) ?? "src/pages";
86
- const pagesDir = resolve(rootDir, rawPagesDir);
125
+ const appSpecs = resolveAppSpecs(options, serverEntry, rootDir);
126
+ const duplicatePrefix = appSpecs.find(
127
+ (spec, index) => appSpecs.findIndex((other) => other.prefix === spec.prefix) !== index
128
+ );
129
+ if (duplicatePrefix) {
130
+ throw new Error(
131
+ `[furin] Two apps are configured with the prefix "${duplicatePrefix.prefix || "/"}" — give each app a unique prefix.`
132
+ );
133
+ }
134
+ // Distinct prefixes can still slug to the same client dir (`/a-b` vs `/a/b`
135
+ // → `client-a-b`), which would let a later app's assets overwrite an earlier
136
+ // one's — reject before any target writes output.
137
+ assertNoPrefixSlugCollisions(appSpecs.map((spec) => spec.prefix));
87
138
 
88
139
  const requestedTargets =
89
140
  options.target === "all"
@@ -95,8 +146,14 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
95
146
  return target as BuildTarget;
96
147
  });
97
148
 
98
- // scanPages throws if root.tsx is missing, so root is always defined here.
99
- const { root, routes } = await scanPages(pagesDir);
149
+ // scanPages throws if root.tsx is missing, so root is always defined per app.
150
+ const apps: BunTargetApp[] = [];
151
+ for (const spec of appSpecs) {
152
+ const { root, routes } = await scanPages(spec.pagesDir);
153
+ apps.push({ pagesDir: spec.pagesDir, prefix: spec.prefix, root, routes });
154
+ }
155
+ // The root-mounted app drives single-app manifest fields and the static target.
156
+ const primaryApp = (apps.find((app) => app.prefix === "") ?? apps[0]) as BunTargetApp;
100
157
 
101
158
  ensureDir(buildRoot);
102
159
 
@@ -104,31 +161,57 @@ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult
104
161
  version: 1,
105
162
  generatedAt: new Date().toISOString(),
106
163
  rootDir: toPosixPath(rootDir),
107
- pagesDir: toPosixPath(relative(rootDir, pagesDir)),
108
- rootPath: toPosixPath(relative(rootDir, root.path)),
164
+ pagesDir: toPosixPath(relative(rootDir, primaryApp.pagesDir)),
165
+ rootPath: toPosixPath(relative(rootDir, primaryApp.root.path)),
109
166
  serverEntry: serverEntry ? toPosixPath(relative(rootDir, serverEntry)) : null,
110
- routes: routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
167
+ routes: primaryApp.routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
168
+ apps: apps.map((app) => ({
169
+ pagesDir: toPosixPath(relative(rootDir, app.pagesDir)),
170
+ prefix: app.prefix,
171
+ routes: app.routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
172
+ })),
111
173
  targets: {},
112
174
  };
113
175
 
114
176
  for (const target of requestedTargets) {
115
177
  switch (target) {
116
178
  case "bun":
117
- manifest.targets.bun = await buildBunTarget(
118
- routes,
179
+ manifest.targets.bun = await buildBunTarget(apps, rootDir, buildRoot, serverEntry, options);
180
+ break;
181
+ case "static":
182
+ if (apps.length > 1) {
183
+ console.warn(
184
+ "[furin] `--target static` exports the root-mounted app only — prefixed apps are skipped."
185
+ );
186
+ }
187
+ // The static adapter only knows about staticConfig.basePath — the mount
188
+ // prefix is a server-side concept it never sees. Exporting a prefixed
189
+ // app without a matching basePath produces root-relative asset URLs.
190
+ if (primaryApp.prefix !== "") {
191
+ console.warn(
192
+ `[furin] \`--target static\` exports an app mounted at "${primaryApp.prefix}", but the ` +
193
+ `static export ignores the mount prefix — set \`static.basePath\` to "${primaryApp.prefix}" ` +
194
+ "(or the sub-path the site is served from) so asset URLs resolve correctly."
195
+ );
196
+ }
197
+ manifest.targets.static = await buildStaticTarget(
198
+ primaryApp.routes,
119
199
  rootDir,
120
200
  buildRoot,
121
- root,
122
- serverEntry,
201
+ primaryApp.root,
123
202
  options
124
203
  );
125
204
  break;
126
- case "static":
127
- manifest.targets.static = await buildStaticTarget(
128
- routes,
205
+ case "package":
206
+ if (apps.length > 1) {
207
+ throw new Error(
208
+ "[furin] `--target package` builds exactly one app — configure a single pagesDir/prefix."
209
+ );
210
+ }
211
+ manifest.targets.package = await buildPackageTarget(
212
+ primaryApp,
129
213
  rootDir,
130
214
  buildRoot,
131
- root,
132
215
  options
133
216
  );
134
217
  break;
@@ -0,0 +1,14 @@
1
+ import { resolve } from "node:path";
2
+
3
+ const INSTRUMENTATION_IMPORT = /(?:^|[/\\])devtools[/\\]instrumentation(?:\.ts|\.js)?$/;
4
+
5
+ export function productionInstrumentationPlugin(): Bun.BunPlugin {
6
+ return {
7
+ name: "furin-production-instrumentation",
8
+ setup(build) {
9
+ build.onResolve({ filter: INSTRUMENTATION_IMPORT }, () => ({
10
+ path: resolve(import.meta.dir, "../server/devtools/instrumentation.production.ts"),
11
+ }));
12
+ },
13
+ };
14
+ }
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { mergeRouteSchemas, type ResolvedRoute } from "../server/router/index.ts";
3
+ import { mergeRouteSchemas } from "../server/router/schema-merge.ts";
4
+ import type { ResolvedRoute } from "../server/router/types.ts";
4
5
 
5
6
  /** @internal Exported for unit testing only. */
6
7
  export function patternToTypeString(pattern: string): string {
@@ -29,7 +30,9 @@ function schemaToTypeStringWithDefaults(schema: unknown, defaultsAreRequired: bo
29
30
  const parts: string[] = [];
30
31
  for (const item of s.anyOf as unknown[]) {
31
32
  const t = schemaToTypeStringWithDefaults(item, defaultsAreRequired);
32
- if (t !== "null") parts.push(t);
33
+ if (t !== "null") {
34
+ parts.push(t);
35
+ }
33
36
  }
34
37
  return parts.join(" | ") || "unknown";
35
38
  }
@@ -54,8 +57,7 @@ function schemaToTypeStringWithDefaults(schema: unknown, defaultsAreRequired: bo
54
57
  const required = new Set<string>(Array.isArray(s.required) ? (s.required as string[]) : []);
55
58
  const props = Object.entries(s.properties as Record<string, unknown>)
56
59
  .map(([k, v]) => {
57
- const isPresent =
58
- required.has(k) || (defaultsAreRequired && hasNonNullSchemaDefault(v));
60
+ const isPresent = required.has(k) || (defaultsAreRequired && hasNonNullSchemaDefault(v));
59
61
  return `${k}${isPresent ? "" : "?"}: ${schemaToTypeStringWithDefaults(v, defaultsAreRequired)}`;
60
62
  })
61
63
  .join("; ");
@@ -76,10 +78,7 @@ function hasNonNullSchemaDefault(schema: unknown): boolean {
76
78
  return false;
77
79
  }
78
80
  const s = schema as Record<string, unknown>;
79
- return (
80
- "default" in s &&
81
- s.default != null
82
- );
81
+ return "default" in s && s.default !== null;
83
82
  }
84
83
 
85
84
  function tagKeyToPropertyName(tag: string): string {
@@ -1,16 +1,25 @@
1
1
  import { readFileSync } from "node:fs";
2
- import { parse, type CallExpression, type ObjectExpression, type ObjectProperty } from "yuku-parser";
2
+ import type { CallExpression, ObjectExpression, ObjectProperty } from "@yuku-toolchain/types";
3
+ import { normalizePrefix } from "../server/instance.ts";
3
4
  import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
4
5
  import { walkAST } from "../shared/utils/ast-walk.ts";
6
+ import { parseSource } from "../shared/parser.ts";
7
+
8
+ export interface ScannedFurinInstance {
9
+ pagesDir: string;
10
+ /** Normalized mount prefix (`""` when absent). */
11
+ prefix: string;
12
+ }
5
13
 
6
14
  /**
7
- * Statically scans a server entry file and returns all `pagesDir` string
8
- * literal values found inside `furin({ pagesDir: "..." })` call expressions.
15
+ * Statically scans a server entry file and returns the `pagesDir` (and
16
+ * optional `prefix`) string literals found inside `furin({ pagesDir: "..." })`
17
+ * call expressions.
9
18
  *
10
- * Dynamic paths (template literals, variables) are silently ignored.
19
+ * Dynamic values (template literals, variables) are silently ignored.
11
20
  * Returns an empty array when nothing is detected.
12
21
  */
13
- export function scanFurinInstances(serverEntryPath: string): string[] {
22
+ export function scanFurinInstances(serverEntryPath: string): ScannedFurinInstance[] {
14
23
  const code = readFileSync(serverEntryPath, "utf8");
15
24
  const lang = detectLangFromPath(serverEntryPath);
16
25
 
@@ -19,14 +28,14 @@ export function scanFurinInstances(serverEntryPath: string): string[] {
19
28
  return [];
20
29
  }
21
30
 
22
- const { program, diagnostics } = parse(code, { sourceType: "module", lang });
31
+ const { program, diagnostics } = parseSource(code, lang);
23
32
  const firstError = diagnostics.find((d) => d.severity === "error");
24
33
  if (firstError) {
25
34
  console.error("[furin] scan-server: parse error:", firstError.message, "in", serverEntryPath);
26
35
  return [];
27
36
  }
28
37
 
29
- const results: string[] = [];
38
+ const results: ScannedFurinInstance[] = [];
30
39
  walkAST(program, (node) => {
31
40
  if (node.type === "CallExpression") {
32
41
  checkFurinCall(node as unknown as CallExpression, results);
@@ -36,7 +45,7 @@ export function scanFurinInstances(serverEntryPath: string): string[] {
36
45
  }
37
46
 
38
47
  /** Checks whether `node` is a `furin({ pagesDir: "..." })` call and, if so, pushes the value. */
39
- function checkFurinCall(node: CallExpression, out: string[]): void {
48
+ function checkFurinCall(node: CallExpression, out: ScannedFurinInstance[]): void {
40
49
  const isFurinCall = node.callee.type === "Identifier" && node.callee.name === "furin";
41
50
 
42
51
  if (!isFurinCall || node.arguments.length === 0) {
@@ -56,10 +65,18 @@ function checkFurinCall(node: CallExpression, out: string[]): void {
56
65
 
57
66
  const pagesDir = extractStringProperty(firstArg, "pagesDir");
58
67
  if (pagesDir !== null) {
59
- out.push(pagesDir);
68
+ const prefix = extractStringProperty(firstArg, "prefix");
69
+ out.push({ pagesDir, prefix: normalizeScannedPrefix(prefix) });
60
70
  }
61
71
  }
62
72
 
73
+ // Delegates to the runtime's normalizePrefix so a literal that would make
74
+ // `furin()` throw at startup fails at build time too, with the same message —
75
+ // silently "fixing" it here would build assets the runtime never serves.
76
+ function normalizeScannedPrefix(prefix: string | null): string {
77
+ return normalizePrefix(prefix ?? undefined);
78
+ }
79
+
63
80
  function isObjectExpressionNode(node: { type: string }): node is ObjectExpression {
64
81
  return node.type === "ObjectExpression";
65
82
  }
@@ -7,26 +7,20 @@ import { ensureDir } from "./shared";
7
7
  * Generates `server.ts` — an intermediate entry used to produce `server.js`.
8
8
  *
9
9
  * Equivalent to `_compile-entry.ts` but for disk (non-binary) production builds:
10
- * 1. Statically imports every page module so Bun bundles them into server.js
11
- * 2. Sets production mode and registers everything in a single CompileContext
12
- * 3. Imports server.ts to boot the app
10
+ * 1. Statically imports every page module of every app so Bun bundles them
11
+ * 2. Sets production mode and registers one CompileContext PER APP
12
+ * 3. Imports server.ts to boot the composed app
13
13
  *
14
14
  * This file is intermediate: `adapter/bun.ts` runs `Bun.build()` on it to produce
15
15
  * the self-contained `server.js` bundle, then deletes this file.
16
16
  */
17
17
  export function generateServerRoutesEntry(options: BuildEntryOptions): string {
18
- const { buildId, outDir, rootPath, routes, serverEntry, rootConventions, routeMetadata, ssgCache } =
19
- options;
18
+ const { apps, outDir, serverEntry } = options;
20
19
  ensureDir(outDir);
21
20
  const source = buildEntrySource({
22
- buildId,
21
+ apps: apps.map(({ embed: _embed, ...context }) => context),
23
22
  headerComment: "// Auto-generated by furin build — do not edit",
24
- rootPath,
25
- routes,
26
23
  serverEntry,
27
- rootConventions,
28
- routeMetadata,
29
- ssgCache,
30
24
  });
31
25
 
32
26
  // Named "server.ts" so Bun.build() outputs "server.js"
@@ -1,14 +1,23 @@
1
+ // biome-ignore-all lint/performance/noAwaitInLoops: SSG cache generation runs routes sequentially for deterministic cache output
1
2
  import type { SsgCacheEntry } from "../server/cache/index.ts";
2
- import { prerenderSSG } from "../server/render/index.ts";
3
- import { createSearchRouteMetadata, type ResolvedRoute, type RootLayout } from "../server/router/index.ts";
4
3
  import { resolvePath } from "../server/render/assemble.ts";
4
+ import { prerenderSSG } from "../server/render/index.ts";
5
+ import {
6
+ createSearchRouteMetadata,
7
+ type ResolvedRoute,
8
+ type RootLayout,
9
+ } from "../server/router/index.ts";
5
10
 
6
11
  export type SSGCacheSnapshot = Record<string, SsgCacheEntry>;
7
12
 
8
13
  export async function buildSSGCacheSnapshot(
9
14
  routes: ResolvedRoute[],
10
15
  root: RootLayout,
11
- origin: string
16
+ origin: string,
17
+ // Build-time renders run OUTSIDE any instance scope (default bucket), so a
18
+ // prefixed app's mount prefix must be passed explicitly — otherwise the
19
+ // snapshot HTML bakes in basePath "" and prerendered <Link> hrefs lose it.
20
+ basePath?: string
12
21
  ): Promise<SSGCacheSnapshot> {
13
22
  const snapshot: SSGCacheSnapshot = {};
14
23
  const searchRoutes = createSearchRouteMetadata(routes);
@@ -20,7 +29,7 @@ export async function buildSSGCacheSnapshot(
20
29
 
21
30
  const paramSets = await route.page.staticParams();
22
31
  for (const params of paramSets) {
23
- const entry = await prerenderSSG(route, params, root, origin, undefined, searchRoutes);
32
+ const entry = await prerenderSSG(route, params, root, origin, basePath, searchRoutes);
24
33
  if (entry instanceof Response) {
25
34
  continue;
26
35
  }
@@ -8,6 +8,11 @@ export interface BuildClientOptions {
8
8
  * correct physical URLs. Pass "" for root deployments.
9
9
  */
10
10
  basePath: string;
11
+ /**
12
+ * On-disk client dir name under outDir (default "client"). Multi-instance
13
+ * builds pass "client-<slug>" per mounted app.
14
+ */
15
+ clientDirName?: string;
11
16
  /** Inject the evlog client logger into the hydrate entry. Off by default. */
12
17
  clientLogging: boolean;
13
18
  outDir: string;
@@ -20,6 +25,12 @@ export interface BuildClientOptions {
20
25
  */
21
26
  publicPath: string;
22
27
  rootLayout: string;
28
+ /**
29
+ * Skip furin-env.d.ts generation. Set for non-root instances in
30
+ * multi-instance dev — the file lives at the project root and only the root
31
+ * app owns it.
32
+ */
33
+ skipRouteTypes?: boolean;
23
34
  }
24
35
 
25
36
  export interface BuildRouteManifestEntry {
@@ -35,15 +46,36 @@ export interface TargetBuildManifest {
35
46
  buildId: string;
36
47
  clientDir: string | null;
37
48
  generatedAt: string;
49
+ rscManifestPath?: string;
38
50
  serverEntry: string | null;
39
51
  serverPath: string | null;
40
52
  targetDir: string;
41
53
  templatePath: string | null;
42
54
  }
43
55
 
44
- export type AnyTargetManifest = TargetBuildManifest | StaticTargetBuildManifest;
56
+ /** Build manifest entry produced by the `package` adapter. */
57
+ export interface PackageTargetBuildManifest {
58
+ buildId: string;
59
+ generatedAt: string;
60
+ prefix: string;
61
+ targetDir: string;
62
+ }
63
+
64
+ export type AnyTargetManifest =
65
+ | TargetBuildManifest
66
+ | StaticTargetBuildManifest
67
+ | PackageTargetBuildManifest;
68
+
69
+ /** One mounted app in a multi-instance build. */
70
+ export interface BuildAppSpec {
71
+ pagesDir: string;
72
+ /** Mount prefix (`""` or absent = root). */
73
+ prefix?: string;
74
+ }
45
75
 
46
76
  export interface BuildManifest {
77
+ /** Every mounted app built into this artifact (multi-instance). */
78
+ apps?: Array<{ pagesDir: string; prefix: string; routes: BuildRouteManifestEntry[] }>;
47
79
  generatedAt: string;
48
80
  pagesDir: string;
49
81
  rootDir: string;
@@ -55,6 +87,11 @@ export interface BuildManifest {
55
87
  }
56
88
 
57
89
  export interface BuildAppOptions {
90
+ /**
91
+ * Explicit multi-app build (furin.config.ts `apps`). Overrides `pagesDir`
92
+ * and server-entry auto-detection.
93
+ */
94
+ apps?: BuildAppSpec[];
58
95
  /** Inject the evlog client logger into the hydrate entry. Defaults to false. */
59
96
  clientLogging?: boolean;
60
97
  compile?: "server" | "embed";
package/src/cli/config.ts CHANGED
@@ -14,7 +14,12 @@ const DEFAULT_CONFIG_FILENAMES = [
14
14
 
15
15
  interface ResolvedCliConfig extends FurinConfig {
16
16
  configPath: string | null;
17
- pagesDir: string;
17
+ /**
18
+ * Only set when the config file declares one — leaving it undefined lets
19
+ * buildApp fall back to scanning server.ts for `furin({ pagesDir, prefix })`
20
+ * calls (multi-instance detection).
21
+ */
22
+ pagesDir?: string;
18
23
  plugins?: Bun.BunPlugin[];
19
24
  rootDir: string;
20
25
  }
@@ -34,7 +39,6 @@ export async function loadCliConfig(
34
39
  return {
35
40
  configPath: null,
36
41
  rootDir,
37
- pagesDir: resolve(rootDir, "src/pages"),
38
42
  };
39
43
  }
40
44
 
@@ -63,6 +67,8 @@ export async function loadCliConfig(
63
67
  plugins,
64
68
  configPath,
65
69
  rootDir: resolvedRootDir,
66
- pagesDir: resolve(resolvedRootDir, configToValidate.pagesDir ?? "src/pages"),
70
+ pagesDir: configToValidate.pagesDir
71
+ ? resolve(resolvedRootDir, configToValidate.pagesDir)
72
+ : undefined,
67
73
  };
68
74
  }
package/src/cli/index.ts CHANGED
@@ -4,10 +4,11 @@ import { resolve } from "node:path";
4
4
  import { parseArgs } from "node:util";
5
5
  import { buildApp } from "../build/index.ts";
6
6
  import { BUILD_TARGETS, type BuildTarget } from "../config.ts";
7
+ import { normalizePrefix } from "../server/instance.ts";
7
8
  import { loadCliConfig } from "./config.ts";
8
9
 
9
10
  const argv = process.argv.slice(2);
10
- const command = argv[0];
11
+ const [command] = argv;
11
12
 
12
13
  function log(msg: string): void {
13
14
  console.log(`\x1b[32m◆\x1b[0m ${msg}`);
@@ -34,34 +35,67 @@ function resolveCompileMode(
34
35
  return configCompile;
35
36
  }
36
37
 
38
+ function extractCompileFlag(args: string[]): {
39
+ compileFlag: string | boolean | undefined;
40
+ parseableArgs: string[];
41
+ } {
42
+ const parseableArgs: string[] = [];
43
+ let compileFlag: string | boolean | undefined;
44
+
45
+ for (let index = 0; index < args.length; index += 1) {
46
+ const arg = args[index];
47
+ if (arg === "--") {
48
+ parseableArgs.push(...args.slice(index));
49
+ break;
50
+ }
51
+ if (arg === "--compile") {
52
+ const next = args[index + 1];
53
+ if (next && !next.startsWith("-")) {
54
+ compileFlag = next;
55
+ index += 1;
56
+ } else {
57
+ compileFlag = true;
58
+ }
59
+ continue;
60
+ }
61
+ if (arg?.startsWith("--compile=")) {
62
+ compileFlag = arg.slice("--compile=".length);
63
+ continue;
64
+ }
65
+ if (arg) {
66
+ parseableArgs.push(arg);
67
+ }
68
+ }
69
+
70
+ return { compileFlag, parseableArgs };
71
+ }
72
+
37
73
  if (command === "build") {
38
- const { values: rawValues } = parseArgs({
39
- args: argv.slice(1),
40
- options: {
41
- target: { type: "string" },
42
- pagesDir: { type: "string" },
43
- config: { type: "string" },
44
- },
45
- strict: false,
46
- });
74
+ const buildArgv = argv.slice(1);
75
+ const { compileFlag, parseableArgs } = extractCompileFlag(buildArgv);
76
+ let rawValues: ReturnType<typeof parseArgs>["values"];
77
+ try {
78
+ rawValues = parseArgs({
79
+ args: parseableArgs,
80
+ options: {
81
+ target: { type: "string" },
82
+ pagesDir: { type: "string" },
83
+ prefix: { type: "string" },
84
+ config: { type: "string" },
85
+ },
86
+ strict: true,
87
+ }).values;
88
+ } catch (error) {
89
+ bail(error instanceof Error ? error.message : String(error));
90
+ }
47
91
 
48
92
  const values = rawValues as {
49
93
  target?: string;
50
94
  pagesDir?: string;
95
+ prefix?: string;
51
96
  config?: string;
52
97
  };
53
98
 
54
- // --compile has an optional value: absent → undefined, present alone → true, present with "embed" → "embed"
55
- const buildArgv = argv.slice(1);
56
- const compileIdx = buildArgv.indexOf("--compile");
57
- let compileFlag: string | boolean | undefined;
58
- if (compileIdx < 0) {
59
- compileFlag = undefined;
60
- } else {
61
- const next = buildArgv[compileIdx + 1];
62
- compileFlag = next && !next.startsWith("-") ? next : true;
63
- }
64
-
65
99
  const target = values.target ?? "bun";
66
100
 
67
101
  if (target !== "all" && !(BUILD_TARGETS as readonly string[]).includes(target)) {
@@ -70,9 +104,9 @@ if (command === "build") {
70
104
 
71
105
  const config = await loadCliConfig(process.cwd(), values.config);
72
106
 
73
- const isStaticTarget = target === "static";
107
+ const isServerlessTarget = target === "static" || target === "package";
74
108
 
75
- const resolvedServerEntry = isStaticTarget
109
+ const resolvedServerEntry = isServerlessTarget
76
110
  ? undefined
77
111
  : (() => {
78
112
  const entry = resolve(config.rootDir, config.serverEntry ?? "src/server.ts");
@@ -89,7 +123,20 @@ if (command === "build") {
89
123
  target: target as BuildTarget | "all",
90
124
  compile: resolveCompileMode(compileFlag, config.bun?.compile),
91
125
  rootDir: config.rootDir,
92
- pagesDir: values.pagesDir ?? config.pagesDir,
126
+ // --pagesDir/--prefix build a single explicit app; otherwise fall back to
127
+ // the config's `apps` list (then to server.ts scanning inside buildApp).
128
+ // normalizePrefix here so a bad --prefix fails before buildApp starts
129
+ // (resolveAppSpecs normalizes config-provided prefixes the same way).
130
+ apps:
131
+ (values.pagesDir ?? config.pagesDir)
132
+ ? [
133
+ {
134
+ pagesDir: values.pagesDir ?? (config.pagesDir as string),
135
+ prefix: normalizePrefix(values.prefix),
136
+ },
137
+ ]
138
+ : config.apps,
139
+ pagesDir: undefined,
93
140
  serverEntry: resolvedServerEntry,
94
141
  plugins: config.plugins,
95
142
  staticConfig: config.static,
@@ -106,7 +153,9 @@ USAGE furin build [options]
106
153
 
107
154
  OPTIONS
108
155
  --target ${BUILD_TARGETS.join(" | ")} | all (default: bun)
156
+ "package" builds a publishable Elysia-plugin artifact (register.js + factory + client assets)
109
157
  --pagesDir Pages directory
158
+ --prefix Mount prefix for the built app (e.g. /admin) — pairs with --pagesDir
110
159
  --config Config file path
111
160
  --compile server | embed Compile to binary: "server" keeps client on disk, "embed" is self-contained
112
161
  `