@teyik0/furin 0.2.0-alpha.3 → 0.2.0-alpha.5

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 (98) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +37 -33
  3. package/dist/adapter/bun.d.ts +17 -1
  4. package/dist/adapter/package.d.ts +26 -0
  5. package/dist/build/client.d.ts +1 -1
  6. package/dist/build/compile-entry.d.ts +3 -3
  7. package/dist/build/compile-entry.ts +114 -109
  8. package/dist/build/entry-template.d.ts +35 -9
  9. package/dist/build/entry-template.ts +206 -156
  10. package/dist/build/hydrate.d.ts +1 -1
  11. package/dist/build/index.js +643 -247
  12. package/dist/build/scan-server.d.ts +10 -4
  13. package/dist/build/server-routes-entry.d.ts +3 -3
  14. package/dist/build/server-routes-entry.ts +30 -36
  15. package/dist/build/ssg-cache.d.ts +1 -1
  16. package/dist/build/types.d.ts +36 -1
  17. package/dist/cli/config.d.ts +6 -1
  18. package/dist/cli/index.js +662 -260
  19. package/dist/client/link.js +163 -16
  20. package/dist/client/router/abort.d.ts +1 -0
  21. package/dist/client/router/sync-catch-up.d.ts +29 -0
  22. package/dist/config.d.ts +11 -2
  23. package/dist/config.js +5 -1
  24. package/dist/furin.d.ts +37 -19
  25. package/dist/furin.js +982 -378
  26. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  27. package/dist/server/cache/dev-loader.d.ts +3 -2
  28. package/dist/server/cache/invalidation.d.ts +19 -0
  29. package/dist/server/cache/isr.d.ts +6 -2
  30. package/dist/server/cache/registry.d.ts +3 -3
  31. package/dist/server/cache/ssg.d.ts +6 -2
  32. package/dist/server/cache/store-view.d.ts +11 -0
  33. package/dist/server/instance.d.ts +86 -0
  34. package/dist/server/internal.d.ts +16 -1
  35. package/dist/server/internal.js +72 -5
  36. package/dist/server/render/template.d.ts +3 -2
  37. package/dist/server/sync/adapter.d.ts +62 -0
  38. package/dist/server/sync/config.d.ts +11 -0
  39. package/dist/server/sync/fingerprint.d.ts +6 -0
  40. package/dist/server/sync/index.d.ts +2 -2
  41. package/dist/server/sync/memory-adapter.d.ts +17 -0
  42. package/dist/server/sync/plugin.d.ts +12 -195
  43. package/dist/server/sync/response.d.ts +4 -0
  44. package/dist/server/sync/stream.d.ts +24 -5
  45. package/dist/shared/prefix.d.ts +15 -0
  46. package/package.json +6 -1
  47. package/src/adapter/README.md +13 -13
  48. package/src/adapter/bun.ts +113 -50
  49. package/src/adapter/package.ts +206 -0
  50. package/src/build/client.ts +158 -152
  51. package/src/build/compile-entry.ts +114 -109
  52. package/src/build/entry-template.ts +206 -156
  53. package/src/build/hydrate.ts +342 -340
  54. package/src/build/index.ts +223 -152
  55. package/src/build/route-types.ts +145 -145
  56. package/src/build/scan-server.ts +111 -95
  57. package/src/build/server-routes-entry.ts +30 -36
  58. package/src/build/shared.ts +80 -80
  59. package/src/build/ssg-cache.ts +36 -32
  60. package/src/build/types.ts +125 -89
  61. package/src/cli/config.ts +9 -3
  62. package/src/cli/index.ts +21 -3
  63. package/src/client/router/abort.ts +5 -0
  64. package/src/client/router/link-utils.ts +184 -184
  65. package/src/client/router/provider.tsx +883 -804
  66. package/src/client/router/sync-catch-up.ts +113 -0
  67. package/src/client/router/types.ts +276 -276
  68. package/src/client/sync.ts +98 -98
  69. package/src/client.ts +369 -369
  70. package/src/config.ts +14 -1
  71. package/src/furin.ts +619 -465
  72. package/src/server/auto-invalidate/registry.ts +23 -1
  73. package/src/server/auto-invalidate/runtime.ts +159 -134
  74. package/src/server/cache/dev-loader.ts +77 -38
  75. package/src/server/cache/invalidation.ts +71 -30
  76. package/src/server/cache/isr.ts +20 -5
  77. package/src/server/cache/registry.ts +17 -20
  78. package/src/server/cache/ssg.ts +19 -4
  79. package/src/server/cache/store-view.ts +30 -0
  80. package/src/server/dev-page-plugin.ts +454 -454
  81. package/src/server/instance.ts +252 -0
  82. package/src/server/internal.ts +100 -5
  83. package/src/server/render/assemble.ts +160 -160
  84. package/src/server/render/isr.ts +323 -323
  85. package/src/server/render/not-found.ts +9 -3
  86. package/src/server/render/ssr.ts +603 -585
  87. package/src/server/render/template.ts +57 -24
  88. package/src/server/router/plugin.ts +384 -384
  89. package/src/server/sync/adapter.ts +65 -0
  90. package/src/server/sync/config.ts +60 -29
  91. package/src/server/sync/fingerprint.ts +44 -0
  92. package/src/server/sync/index.ts +15 -14
  93. package/src/server/sync/memory-adapter.ts +196 -0
  94. package/src/server/sync/plugin.ts +199 -108
  95. package/src/server/sync/response.ts +77 -0
  96. package/src/server/sync/stream.ts +162 -144
  97. package/src/shared/deferred-ndjson.ts +7 -0
  98. package/src/shared/prefix.ts +38 -0
@@ -1,152 +1,223 @@
1
- import { existsSync, writeFileSync } from "node:fs";
2
- import { join, relative, resolve } from "node:path";
3
- import { buildBunTarget } from "../adapter/bun";
4
- import { buildStaticTarget } from "../adapter/static";
5
- import { BUILD_TARGETS, type BuildTarget, type FurinPlugin } from "../config";
6
- import { scanPages } from "../server/router/index.ts";
7
- import { scanFurinInstances } from "./scan-server";
8
- import { ensureDir, toBuildRouteManifestEntry, toPosixPath } from "./shared";
9
- import type { BuildAppOptions, BuildAppResult, BuildManifest } from "./types";
10
-
11
- // biome-ignore lint/performance/noBarrelFile: intentional — build/index.ts is the public build API entry
12
- export { buildClient } from "./client";
13
- export { writeDevFiles } from "./hydrate";
14
- export { patternToTypeString, schemaToTypeString, writeRouteTypes } from "./route-types";
15
- export { buildSSGCacheSnapshot, type SSGCacheSnapshot } from "./ssg-cache";
16
- export type {
17
- BuildAppOptions,
18
- BuildAppResult,
19
- BuildClientOptions,
20
- BuildManifest,
21
- BuildRouteManifestEntry,
22
- TargetBuildManifest,
23
- } from "./types";
24
-
25
- const IMPLEMENTED_TARGETS = ["bun", "static"] as const satisfies BuildTarget[];
26
- export const BUILD_OUTPUT_DIR = ".furin/build";
27
-
28
- function resolvePagesDirFromServer(serverEntry: string | null, rootDir: string): string | null {
29
- if (!serverEntry) {
30
- return null;
31
- }
32
- const detected = scanFurinInstances(serverEntry);
33
- if (detected.length === 0) {
34
- return null;
35
- }
36
- // Use the first detected pagesDir relative to rootDir
37
- return resolve(rootDir, detected[0] as string);
38
- }
39
-
40
- export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
41
- const rootDir = resolve(options.rootDir ?? process.cwd());
42
- const buildRoot = join(rootDir, BUILD_OUTPUT_DIR);
43
- const serverEntry = (() => {
44
- // Static-only builds don't need a server entry point
45
- if (options.target === "static") {
46
- return null;
47
- }
48
- if (options.serverEntry) {
49
- const resolved = resolve(rootDir, options.serverEntry);
50
- if (existsSync(resolved)) {
51
- return resolved;
52
- }
53
- }
54
- const entry = resolve(rootDir, "src/server.ts");
55
- if (!existsSync(entry)) {
56
- throw new Error("[furin] Entrypoint server.ts not found");
57
- }
58
- return entry;
59
- })();
60
-
61
- // Register user plugins as runtime module loaders BEFORE scanPages.
62
- //
63
- // scanPages() calls import() on every page file, which triggers all their
64
- // static imports (e.g. ".mdx" files). If the loader is not yet registered
65
- // at that point, Bun caches the raw/unprocessed result — and that cached
66
- // value is reused for every subsequent import(), including prerenderSSG().
67
- //
68
- // Build-only plugins (e.g. bun-plugin-tailwind uses onBeforeParse which
69
- // doesn't exist in the runtime context) are skipped silently — they only
70
- // affect the Bun.build() client bundle, not server-side rendering.
71
- for (const plugin of options.plugins ?? []) {
72
- const { buildOnly, ...runtimePlugin } = plugin as FurinPlugin;
73
- if (buildOnly) {
74
- continue;
75
- }
76
- try {
77
- Bun.plugin(runtimePlugin);
78
- } catch (err) {
79
- console.debug("[furin] Skipped plugin at runtime:", err);
80
- }
81
- }
82
-
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);
87
-
88
- const requestedTargets =
89
- options.target === "all"
90
- ? [...IMPLEMENTED_TARGETS]
91
- : [options.target].map((target) => {
92
- if (!(BUILD_TARGETS as readonly string[]).includes(target)) {
93
- throw new Error(`[furin] Unsupported build target "${target}"`);
94
- }
95
- return target as BuildTarget;
96
- });
97
-
98
- // scanPages throws if root.tsx is missing, so root is always defined here.
99
- const { root, routes } = await scanPages(pagesDir);
100
-
101
- ensureDir(buildRoot);
102
-
103
- const manifest: BuildManifest = {
104
- version: 1,
105
- generatedAt: new Date().toISOString(),
106
- rootDir: toPosixPath(rootDir),
107
- pagesDir: toPosixPath(relative(rootDir, pagesDir)),
108
- rootPath: toPosixPath(relative(rootDir, root.path)),
109
- serverEntry: serverEntry ? toPosixPath(relative(rootDir, serverEntry)) : null,
110
- routes: routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
111
- targets: {},
112
- };
113
-
114
- for (const target of requestedTargets) {
115
- switch (target) {
116
- case "bun":
117
- manifest.targets.bun = await buildBunTarget(
118
- routes,
119
- rootDir,
120
- buildRoot,
121
- root,
122
- serverEntry,
123
- options
124
- );
125
- break;
126
- case "static":
127
- manifest.targets.static = await buildStaticTarget(
128
- routes,
129
- rootDir,
130
- buildRoot,
131
- root,
132
- options
133
- );
134
- break;
135
- case "node":
136
- case "vercel":
137
- case "cloudflare":
138
- throw new Error(
139
- `[furin] \`--target ${target}\` is planned but not implemented yet in this branch.`
140
- );
141
- default:
142
- throw new Error(`[furin] Unsupported build target "${target}"`);
143
- }
144
- }
145
-
146
- writeFileSync(join(buildRoot, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`);
147
-
148
- return {
149
- manifest,
150
- targets: manifest.targets,
151
- };
152
- }
1
+ import { existsSync, writeFileSync } from "node:fs";
2
+ import { join, relative, resolve } from "node:path";
3
+ import { buildBunTarget, type BunTargetApp } from "../adapter/bun";
4
+ import { buildPackageTarget } from "../adapter/package";
5
+ import { buildStaticTarget } from "../adapter/static";
6
+ import { BUILD_TARGETS, type BuildTarget, type FurinPlugin } from "../config";
7
+ import { normalizePrefix } from "../server/instance.ts";
8
+ import { scanPages } from "../server/router/index.ts";
9
+ import { assertNoPrefixSlugCollisions } from "../shared/prefix.ts";
10
+ import { scanFurinInstances } from "./scan-server";
11
+ import { ensureDir, toBuildRouteManifestEntry, toPosixPath } from "./shared";
12
+ import type { BuildAppOptions, BuildAppResult, BuildManifest } from "./types";
13
+
14
+ // biome-ignore lint/performance/noBarrelFile: intentional — build/index.ts is the public build API entry
15
+ export { buildClient } from "./client";
16
+ export { writeDevFiles } from "./hydrate";
17
+ export { patternToTypeString, schemaToTypeString, writeRouteTypes } from "./route-types";
18
+ export { buildSSGCacheSnapshot, type SSGCacheSnapshot } from "./ssg-cache";
19
+ export type {
20
+ BuildAppOptions,
21
+ BuildAppResult,
22
+ BuildClientOptions,
23
+ BuildManifest,
24
+ BuildRouteManifestEntry,
25
+ TargetBuildManifest,
26
+ } from "./types";
27
+
28
+ // "package" is intentionally excluded from `--target all` — it is an
29
+ // alternative packaging of ONE app, not an additional deploy target.
30
+ const IMPLEMENTED_TARGETS = ["bun", "static"] as const satisfies BuildTarget[];
31
+ export const BUILD_OUTPUT_DIR = ".furin/build";
32
+
33
+ /**
34
+ * Resolves the list of apps to build, in priority order:
35
+ * 1. explicit `apps` config (furin.config.ts),
36
+ * 2. explicit single `pagesDir` option (mounted at root),
37
+ * 3. every `furin({ pagesDir, prefix })` call detected in the server entry,
38
+ * 4. the `src/pages` default.
39
+ */
40
+ function resolveAppSpecs(
41
+ options: BuildAppOptions,
42
+ serverEntry: string | null,
43
+ rootDir: string
44
+ ): Array<{ pagesDir: string; prefix: string }> {
45
+ if (options.apps && options.apps.length > 0) {
46
+ // normalizePrefix is the choke point for config/CLI-provided prefixes:
47
+ // `"/"` → `""` (otherwise the bun target derives `publicPath: "//_client/"`,
48
+ // a protocol-relative URL), trailing slashes are trimmed, and invalid
49
+ // prefixes throw before any build output is written.
50
+ return options.apps.map((app) => ({
51
+ pagesDir: resolve(rootDir, app.pagesDir),
52
+ prefix: normalizePrefix(app.prefix),
53
+ }));
54
+ }
55
+ if (options.pagesDir) {
56
+ return [{ pagesDir: resolve(rootDir, options.pagesDir), prefix: "" }];
57
+ }
58
+ if (serverEntry) {
59
+ const detected = scanFurinInstances(serverEntry);
60
+ if (detected.length > 0) {
61
+ return detected.map((app) => ({
62
+ pagesDir: resolve(rootDir, app.pagesDir),
63
+ prefix: app.prefix,
64
+ }));
65
+ }
66
+ }
67
+ return [{ pagesDir: resolve(rootDir, "src/pages"), prefix: "" }];
68
+ }
69
+
70
+ export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
71
+ const rootDir = resolve(options.rootDir ?? process.cwd());
72
+ const buildRoot = join(rootDir, BUILD_OUTPUT_DIR);
73
+ const serverEntry = (() => {
74
+ // Static and package builds don't need a server entry point
75
+ if (options.target === "static" || options.target === "package") {
76
+ return null;
77
+ }
78
+ if (options.serverEntry) {
79
+ const resolved = resolve(rootDir, options.serverEntry);
80
+ if (existsSync(resolved)) {
81
+ return resolved;
82
+ }
83
+ }
84
+ const entry = resolve(rootDir, "src/server.ts");
85
+ if (!existsSync(entry)) {
86
+ throw new Error("[furin] Entrypoint server.ts not found");
87
+ }
88
+ return entry;
89
+ })();
90
+
91
+ // Register user plugins as runtime module loaders BEFORE scanPages.
92
+ //
93
+ // scanPages() calls import() on every page file, which triggers all their
94
+ // static imports (e.g. ".mdx" files). If the loader is not yet registered
95
+ // at that point, Bun caches the raw/unprocessed result — and that cached
96
+ // value is reused for every subsequent import(), including prerenderSSG().
97
+ //
98
+ // Build-only plugins (e.g. bun-plugin-tailwind uses onBeforeParse which
99
+ // doesn't exist in the runtime context) are skipped silently — they only
100
+ // affect the Bun.build() client bundle, not server-side rendering.
101
+ for (const plugin of options.plugins ?? []) {
102
+ const { buildOnly, ...runtimePlugin } = plugin as FurinPlugin;
103
+ if (buildOnly) {
104
+ continue;
105
+ }
106
+ try {
107
+ Bun.plugin(runtimePlugin);
108
+ } catch (err) {
109
+ console.debug("[furin] Skipped plugin at runtime:", err);
110
+ }
111
+ }
112
+
113
+ const appSpecs = resolveAppSpecs(options, serverEntry, rootDir);
114
+ const duplicatePrefix = appSpecs.find(
115
+ (spec, index) => appSpecs.findIndex((other) => other.prefix === spec.prefix) !== index
116
+ );
117
+ if (duplicatePrefix) {
118
+ throw new Error(
119
+ `[furin] Two apps are configured with the prefix "${duplicatePrefix.prefix || "/"}" — give each app a unique prefix.`
120
+ );
121
+ }
122
+ // Distinct prefixes can still slug to the same client dir (`/a-b` vs `/a/b`
123
+ // → `client-a-b`), which would let a later app's assets overwrite an earlier
124
+ // one's — reject before any target writes output.
125
+ assertNoPrefixSlugCollisions(appSpecs.map((spec) => spec.prefix));
126
+
127
+ const requestedTargets =
128
+ options.target === "all"
129
+ ? [...IMPLEMENTED_TARGETS]
130
+ : [options.target].map((target) => {
131
+ if (!(BUILD_TARGETS as readonly string[]).includes(target)) {
132
+ throw new Error(`[furin] Unsupported build target "${target}"`);
133
+ }
134
+ return target as BuildTarget;
135
+ });
136
+
137
+ // scanPages throws if root.tsx is missing, so root is always defined per app.
138
+ const apps: BunTargetApp[] = [];
139
+ for (const spec of appSpecs) {
140
+ const { root, routes } = await scanPages(spec.pagesDir);
141
+ apps.push({ pagesDir: spec.pagesDir, prefix: spec.prefix, root, routes });
142
+ }
143
+ // The root-mounted app drives single-app manifest fields and the static target.
144
+ const primaryApp = (apps.find((app) => app.prefix === "") ?? apps[0]) as BunTargetApp;
145
+
146
+ ensureDir(buildRoot);
147
+
148
+ const manifest: BuildManifest = {
149
+ version: 1,
150
+ generatedAt: new Date().toISOString(),
151
+ rootDir: toPosixPath(rootDir),
152
+ pagesDir: toPosixPath(relative(rootDir, primaryApp.pagesDir)),
153
+ rootPath: toPosixPath(relative(rootDir, primaryApp.root.path)),
154
+ serverEntry: serverEntry ? toPosixPath(relative(rootDir, serverEntry)) : null,
155
+ routes: primaryApp.routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
156
+ apps: apps.map((app) => ({
157
+ pagesDir: toPosixPath(relative(rootDir, app.pagesDir)),
158
+ prefix: app.prefix,
159
+ routes: app.routes.map((route) => toBuildRouteManifestEntry(route, rootDir)),
160
+ })),
161
+ targets: {},
162
+ };
163
+
164
+ for (const target of requestedTargets) {
165
+ switch (target) {
166
+ case "bun":
167
+ manifest.targets.bun = await buildBunTarget(apps, rootDir, buildRoot, serverEntry, options);
168
+ break;
169
+ case "static":
170
+ if (apps.length > 1) {
171
+ console.warn(
172
+ "[furin] `--target static` exports the root-mounted app only — prefixed apps are skipped."
173
+ );
174
+ }
175
+ // The static adapter only knows about staticConfig.basePath — the mount
176
+ // prefix is a server-side concept it never sees. Exporting a prefixed
177
+ // app without a matching basePath produces root-relative asset URLs.
178
+ if (primaryApp.prefix !== "") {
179
+ console.warn(
180
+ `[furin] \`--target static\` exports an app mounted at "${primaryApp.prefix}", but the ` +
181
+ `static export ignores the mount prefix — set \`static.basePath\` to "${primaryApp.prefix}" ` +
182
+ "(or the sub-path the site is served from) so asset URLs resolve correctly."
183
+ );
184
+ }
185
+ manifest.targets.static = await buildStaticTarget(
186
+ primaryApp.routes,
187
+ rootDir,
188
+ buildRoot,
189
+ primaryApp.root,
190
+ options
191
+ );
192
+ break;
193
+ case "package":
194
+ if (apps.length > 1) {
195
+ throw new Error(
196
+ "[furin] `--target package` builds exactly one app — configure a single pagesDir/prefix."
197
+ );
198
+ }
199
+ manifest.targets.package = await buildPackageTarget(
200
+ primaryApp,
201
+ rootDir,
202
+ buildRoot,
203
+ options
204
+ );
205
+ break;
206
+ case "node":
207
+ case "vercel":
208
+ case "cloudflare":
209
+ throw new Error(
210
+ `[furin] \`--target ${target}\` is planned but not implemented yet in this branch.`
211
+ );
212
+ default:
213
+ throw new Error(`[furin] Unsupported build target "${target}"`);
214
+ }
215
+ }
216
+
217
+ writeFileSync(join(buildRoot, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`);
218
+
219
+ return {
220
+ manifest,
221
+ targets: manifest.targets,
222
+ };
223
+ }