@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.
- package/LICENSE +21 -21
- package/README.md +37 -33
- package/dist/adapter/bun.d.ts +17 -1
- package/dist/adapter/package.d.ts +26 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +3 -3
- package/dist/build/compile-entry.ts +114 -109
- package/dist/build/entry-template.d.ts +35 -9
- package/dist/build/entry-template.ts +206 -156
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +643 -247
- package/dist/build/scan-server.d.ts +10 -4
- package/dist/build/server-routes-entry.d.ts +3 -3
- package/dist/build/server-routes-entry.ts +30 -36
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +36 -1
- package/dist/cli/config.d.ts +6 -1
- package/dist/cli/index.js +662 -260
- package/dist/client/link.js +163 -16
- package/dist/client/router/abort.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +29 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.js +5 -1
- package/dist/furin.d.ts +37 -19
- package/dist/furin.js +982 -378
- package/dist/server/auto-invalidate/registry.d.ts +8 -1
- package/dist/server/cache/dev-loader.d.ts +3 -2
- package/dist/server/cache/invalidation.d.ts +19 -0
- package/dist/server/cache/isr.d.ts +6 -2
- package/dist/server/cache/registry.d.ts +3 -3
- package/dist/server/cache/ssg.d.ts +6 -2
- package/dist/server/cache/store-view.d.ts +11 -0
- package/dist/server/instance.d.ts +86 -0
- package/dist/server/internal.d.ts +16 -1
- package/dist/server/internal.js +72 -5
- package/dist/server/render/template.d.ts +3 -2
- package/dist/server/sync/adapter.d.ts +62 -0
- package/dist/server/sync/config.d.ts +11 -0
- package/dist/server/sync/fingerprint.d.ts +6 -0
- package/dist/server/sync/index.d.ts +2 -2
- package/dist/server/sync/memory-adapter.d.ts +17 -0
- package/dist/server/sync/plugin.d.ts +12 -195
- package/dist/server/sync/response.d.ts +4 -0
- package/dist/server/sync/stream.d.ts +24 -5
- package/dist/shared/prefix.d.ts +15 -0
- package/package.json +6 -1
- package/src/adapter/README.md +13 -13
- package/src/adapter/bun.ts +113 -50
- package/src/adapter/package.ts +206 -0
- package/src/build/client.ts +158 -152
- package/src/build/compile-entry.ts +114 -109
- package/src/build/entry-template.ts +206 -156
- package/src/build/hydrate.ts +342 -340
- package/src/build/index.ts +223 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +111 -95
- package/src/build/server-routes-entry.ts +30 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +36 -32
- package/src/build/types.ts +125 -89
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +21 -3
- package/src/client/router/abort.ts +5 -0
- package/src/client/router/link-utils.ts +184 -184
- package/src/client/router/provider.tsx +883 -804
- package/src/client/router/sync-catch-up.ts +113 -0
- package/src/client/router/types.ts +276 -276
- package/src/client/sync.ts +98 -98
- package/src/client.ts +369 -369
- package/src/config.ts +14 -1
- package/src/furin.ts +619 -465
- package/src/server/auto-invalidate/registry.ts +23 -1
- package/src/server/auto-invalidate/runtime.ts +159 -134
- package/src/server/cache/dev-loader.ts +77 -38
- package/src/server/cache/invalidation.ts +71 -30
- package/src/server/cache/isr.ts +20 -5
- package/src/server/cache/registry.ts +17 -20
- package/src/server/cache/ssg.ts +19 -4
- package/src/server/cache/store-view.ts +30 -0
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/instance.ts +252 -0
- package/src/server/internal.ts +100 -5
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/not-found.ts +9 -3
- package/src/server/render/ssr.ts +603 -585
- package/src/server/render/template.ts +57 -24
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +60 -29
- package/src/server/sync/fingerprint.ts +44 -0
- package/src/server/sync/index.ts +15 -14
- package/src/server/sync/memory-adapter.ts +196 -0
- package/src/server/sync/plugin.ts +199 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +162 -144
- package/src/shared/deferred-ndjson.ts +7 -0
- package/src/shared/prefix.ts +38 -0
package/src/build/index.ts
CHANGED
|
@@ -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 {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
+
}
|