@teyik0/furin 0.1.0-alpha.10
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/dist/adapter/bun.d.ts +3 -0
- package/dist/adapter/static.d.ts +3 -0
- package/dist/build/client.d.ts +25 -0
- package/dist/build/compile-entry.d.ts +35 -0
- package/dist/build/entry-template.d.ts +26 -0
- package/dist/build/hydrate.d.ts +24 -0
- package/dist/build/index.d.ts +7 -0
- package/dist/build/index.js +4716 -0
- package/dist/build/route-types.d.ts +21 -0
- package/dist/build/scan-server.d.ts +8 -0
- package/dist/build/server-routes-entry.d.ts +35 -0
- package/dist/build/shared.d.ts +11 -0
- package/dist/build/types.d.ts +76 -0
- package/dist/cli/config.d.ts +9 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +4751 -0
- package/dist/client.d.ts +195 -0
- package/dist/client.js +20 -0
- package/dist/config.d.ts +50 -0
- package/dist/config.js +28 -0
- package/dist/context-logger.d.ts +28 -0
- package/dist/dev-page-plugin.d.ts +109 -0
- package/dist/error.d.ts +23 -0
- package/dist/furin.d.ts +52 -0
- package/dist/furin.js +3948 -0
- package/dist/internal.d.ts +33 -0
- package/dist/link.d.ts +454 -0
- package/dist/link.js +1790 -0
- package/dist/not-found.d.ts +19 -0
- package/dist/plugin/index.d.ts +20 -0
- package/dist/plugin/index.js +1408 -0
- package/dist/plugin/transform-client.d.ts +9 -0
- package/dist/render/assemble.d.ts +15 -0
- package/dist/render/boundaries.d.ts +79 -0
- package/dist/render/cache.d.ts +96 -0
- package/dist/render/default-screens.d.ts +11 -0
- package/dist/render/digest.d.ts +16 -0
- package/dist/render/element.d.ts +8 -0
- package/dist/render/index.d.ts +53 -0
- package/dist/render/loaders.d.ts +21 -0
- package/dist/render/shell.d.ts +29 -0
- package/dist/render/template.d.ts +6 -0
- package/dist/router.d.ts +96 -0
- package/dist/router.js +2693 -0
- package/dist/runtime-env.d.ts +3 -0
- package/dist/utils.d.ts +6 -0
- package/package.json +77 -0
- package/src/adapter/README.md +13 -0
- package/src/adapter/bun.ts +239 -0
- package/src/adapter/static.ts +341 -0
- package/src/build/client.ts +153 -0
- package/src/build/compile-entry.ts +129 -0
- package/src/build/entry-template.ts +126 -0
- package/src/build/hydrate.ts +250 -0
- package/src/build/index.ts +151 -0
- package/src/build/route-types.ts +89 -0
- package/src/build/scan-server.ts +110 -0
- package/src/build/server-routes-entry.ts +55 -0
- package/src/build/shared.ts +78 -0
- package/src/build/types.ts +85 -0
- package/src/cli/config.ts +68 -0
- package/src/cli/index.ts +115 -0
- package/src/client.ts +278 -0
- package/src/config.ts +66 -0
- package/src/context-logger.ts +74 -0
- package/src/dev-page-plugin.ts +434 -0
- package/src/env.d.ts +49 -0
- package/src/error.ts +32 -0
- package/src/furin.ts +383 -0
- package/src/internal.ts +51 -0
- package/src/link.tsx +1578 -0
- package/src/not-found.ts +40 -0
- package/src/plugin/index.ts +80 -0
- package/src/plugin/transform-client.ts +372 -0
- package/src/render/assemble.ts +66 -0
- package/src/render/boundaries.tsx +176 -0
- package/src/render/cache.ts +251 -0
- package/src/render/default-screens.tsx +299 -0
- package/src/render/digest.ts +71 -0
- package/src/render/element.tsx +119 -0
- package/src/render/index.ts +867 -0
- package/src/render/loaders.ts +127 -0
- package/src/render/shell.ts +187 -0
- package/src/render/template.ts +55 -0
- package/src/router.ts +779 -0
- package/src/runtime-env.ts +6 -0
- package/src/utils.ts +68 -0
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { RuntimePage, RuntimeRoute } from "./client";
|
|
2
|
+
export declare function isFurinPage(value: unknown): value is RuntimePage;
|
|
3
|
+
export declare function isFurinRoute(value: unknown): value is RuntimeRoute;
|
|
4
|
+
export declare function collectRouteChainFromRoute(route: RuntimeRoute): RuntimeRoute[];
|
|
5
|
+
export declare function hasCycle(route: RuntimeRoute): boolean;
|
|
6
|
+
export declare function validateRouteChain(chain: RuntimeRoute[], root: RuntimeRoute, pagePath?: string): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teyik0/furin",
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/furin.js",
|
|
10
|
+
"module": "./dist/furin.js",
|
|
11
|
+
"types": "./src/furin.ts",
|
|
12
|
+
"bin": {
|
|
13
|
+
"furin": "./src/cli/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./src/furin.ts",
|
|
18
|
+
"bun": "./src/furin.ts",
|
|
19
|
+
"import": "./dist/furin.js"
|
|
20
|
+
},
|
|
21
|
+
"./client": {
|
|
22
|
+
"types": "./src/client.ts",
|
|
23
|
+
"bun": "./src/client.ts",
|
|
24
|
+
"import": "./dist/client.js"
|
|
25
|
+
},
|
|
26
|
+
"./build": {
|
|
27
|
+
"types": "./src/build/index.ts",
|
|
28
|
+
"bun": "./src/build/index.ts",
|
|
29
|
+
"import": "./dist/build/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./config": {
|
|
32
|
+
"types": "./src/config.ts",
|
|
33
|
+
"bun": "./src/config.ts",
|
|
34
|
+
"import": "./dist/config.js"
|
|
35
|
+
},
|
|
36
|
+
"./router": {
|
|
37
|
+
"types": "./src/router.ts",
|
|
38
|
+
"bun": "./src/router.ts",
|
|
39
|
+
"import": "./dist/router.js"
|
|
40
|
+
},
|
|
41
|
+
"./strip-plugin": {
|
|
42
|
+
"types": "./src/plugin/index.ts",
|
|
43
|
+
"bun": "./src/plugin/index.ts",
|
|
44
|
+
"import": "./dist/plugin/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./link": {
|
|
47
|
+
"types": "./src/link.tsx",
|
|
48
|
+
"bun": "./src/link.tsx",
|
|
49
|
+
"import": "./dist/link.js"
|
|
50
|
+
},
|
|
51
|
+
"./env": {
|
|
52
|
+
"types": "./src/env.d.ts"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"test": "bun test",
|
|
57
|
+
"test:types": "tsc --noEmit",
|
|
58
|
+
"build": "bun run build.ts",
|
|
59
|
+
"fix": "ultracite fix"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@elysiajs/static": "^1.4.9",
|
|
63
|
+
"magic-string": "^0.30.21",
|
|
64
|
+
"oxc-parser": "^0.127.0"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/react": "^19.2.14",
|
|
68
|
+
"@types/react-dom": "^19.2.3",
|
|
69
|
+
"expect-type": "^1.3.0"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"elysia": "^1.4.28",
|
|
73
|
+
"evlog": "^2.13.0",
|
|
74
|
+
"react": "^19.2.5",
|
|
75
|
+
"react-dom": "^19.2.5"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
## Bun adapter
|
|
2
|
+
|
|
3
|
+
Goal is having 3 modes:
|
|
4
|
+
|
|
5
|
+
- generate server.js + client/
|
|
6
|
+
- generate server (compiled to binary) + client/
|
|
7
|
+
- generate server (compiled to binary with client/ [embeded](https://bun.com/docs/bundler/executables.md))
|
|
8
|
+
|
|
9
|
+
All should be portable.
|
|
10
|
+
|
|
11
|
+
## Cloudflare adapter
|
|
12
|
+
|
|
13
|
+
## Vercel adapter
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { existsSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { buildClient } from "../build/client.ts";
|
|
5
|
+
import { generateCompileEntry } from "../build/compile-entry.ts";
|
|
6
|
+
import { generateServerRoutesEntry } from "../build/server-routes-entry.ts";
|
|
7
|
+
import { buildTargetManifest, copyDirRecursive, ensureDir, toPosixPath } from "../build/shared.ts";
|
|
8
|
+
import type { BuildAppOptions, TargetBuildManifest } from "../build/types.ts";
|
|
9
|
+
import type { BuildTarget } from "../config.ts";
|
|
10
|
+
import { generateProdIndexHtml } from "../render/shell.ts";
|
|
11
|
+
import type { ResolvedRoute, RootLayout } from "../router.ts";
|
|
12
|
+
|
|
13
|
+
// import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
|
|
14
|
+
// through package exports, and is the Web-standard API.
|
|
15
|
+
const _pkgSrcDir = dirname(fileURLToPath(import.meta.resolve("@teyik0/furin")));
|
|
16
|
+
const BUILD_ID_INPUT_PATHS = [
|
|
17
|
+
`${_pkgSrcDir}/build/compile-entry.ts`,
|
|
18
|
+
`${_pkgSrcDir}/build/entry-template.ts`,
|
|
19
|
+
`${_pkgSrcDir}/build/server-routes-entry.ts`,
|
|
20
|
+
`${_pkgSrcDir}/render/index.ts`,
|
|
21
|
+
`${_pkgSrcDir}/render/shell.ts`,
|
|
22
|
+
`${_pkgSrcDir}/router.ts`,
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
async function createBuildFingerprint(
|
|
26
|
+
entryChunk: string,
|
|
27
|
+
cssChunks: string[],
|
|
28
|
+
routes: ResolvedRoute[],
|
|
29
|
+
root: RootLayout,
|
|
30
|
+
serverEntry: string | null
|
|
31
|
+
): Promise<string> {
|
|
32
|
+
const fingerprintPaths = new Set<string>([root.path, ...routes.map((route) => route.path)]);
|
|
33
|
+
if (serverEntry) {
|
|
34
|
+
fingerprintPaths.add(serverEntry);
|
|
35
|
+
}
|
|
36
|
+
if (root.errorPath) {
|
|
37
|
+
fingerprintPaths.add(root.errorPath);
|
|
38
|
+
}
|
|
39
|
+
if (root.notFoundPath) {
|
|
40
|
+
fingerprintPaths.add(root.notFoundPath);
|
|
41
|
+
}
|
|
42
|
+
for (const route of routes) {
|
|
43
|
+
for (const segment of route.segmentBoundaries) {
|
|
44
|
+
if (segment.errorPath) {
|
|
45
|
+
fingerprintPaths.add(segment.errorPath);
|
|
46
|
+
}
|
|
47
|
+
if (segment.notFoundPath) {
|
|
48
|
+
fingerprintPaths.add(segment.notFoundPath);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const path of BUILD_ID_INPUT_PATHS) {
|
|
53
|
+
if (!existsSync(path)) {
|
|
54
|
+
// A missing framework source file would silently produce an empty-string
|
|
55
|
+
// contribution to the fingerprint, making the build ID unreliable.
|
|
56
|
+
console.warn(
|
|
57
|
+
`[furin] Warning: build fingerprint input "${toPosixPath(path)}" is missing — ` +
|
|
58
|
+
"the generated build ID may not reflect all framework changes."
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
fingerprintPaths.add(path);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const fileParts = await Promise.all(
|
|
65
|
+
[...fingerprintPaths].sort().map(async (path) => {
|
|
66
|
+
const content = existsSync(path) ? await Bun.file(path).text() : "";
|
|
67
|
+
return `${toPosixPath(path)}:${content}`;
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const routeParts = routes
|
|
72
|
+
.map((route) =>
|
|
73
|
+
JSON.stringify({ mode: route.mode, path: toPosixPath(route.path), pattern: route.pattern })
|
|
74
|
+
)
|
|
75
|
+
.sort();
|
|
76
|
+
|
|
77
|
+
return [entryChunk, ...[...cssChunks].sort(), ...routeParts, ...fileParts].join("\n");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function buildCompileMetadata(root: RootLayout, routes: ResolvedRoute[]) {
|
|
81
|
+
const rootConventions =
|
|
82
|
+
root.errorPath || root.notFoundPath
|
|
83
|
+
? {
|
|
84
|
+
errorPath: root.errorPath ? toPosixPath(root.errorPath) : undefined,
|
|
85
|
+
notFoundPath: root.notFoundPath ? toPosixPath(root.notFoundPath) : undefined,
|
|
86
|
+
}
|
|
87
|
+
: undefined;
|
|
88
|
+
|
|
89
|
+
const routeMetadata: NonNullable<Parameters<typeof generateCompileEntry>[0]["routeMetadata"]> =
|
|
90
|
+
{};
|
|
91
|
+
for (const route of routes) {
|
|
92
|
+
routeMetadata[toPosixPath(route.path)] = {
|
|
93
|
+
segmentBoundaries: route.segmentBoundaries.map((b) => ({
|
|
94
|
+
depth: b.depth,
|
|
95
|
+
path: toPosixPath(b.path),
|
|
96
|
+
errorPath: b.errorPath ? toPosixPath(b.errorPath) : undefined,
|
|
97
|
+
notFoundPath: b.notFoundPath ? toPosixPath(b.notFoundPath) : undefined,
|
|
98
|
+
})),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { rootConventions, routeMetadata };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function buildBunTarget(
|
|
106
|
+
routes: ResolvedRoute[],
|
|
107
|
+
rootDir: string,
|
|
108
|
+
buildRoot: string,
|
|
109
|
+
root: RootLayout,
|
|
110
|
+
serverEntry: string | null,
|
|
111
|
+
options: BuildAppOptions
|
|
112
|
+
): Promise<TargetBuildManifest> {
|
|
113
|
+
if (options.compile && !serverEntry) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`[furin] \`compile: "${options.compile}"\` requires a server entry point. ` +
|
|
116
|
+
"Create src/server.ts or set `serverEntry` in your furin.config.ts."
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const target = "bun" satisfies BuildTarget;
|
|
121
|
+
const targetManifest = buildTargetManifest(rootDir, buildRoot, target, serverEntry);
|
|
122
|
+
const targetDir = resolve(rootDir, targetManifest.targetDir);
|
|
123
|
+
|
|
124
|
+
rmSync(targetDir, { force: true, recursive: true });
|
|
125
|
+
ensureDir(targetDir);
|
|
126
|
+
|
|
127
|
+
const { entryChunk, cssChunks } = await buildClient(routes, {
|
|
128
|
+
outDir: targetDir,
|
|
129
|
+
rootLayout: root.path,
|
|
130
|
+
plugins: options.plugins,
|
|
131
|
+
publicPath: "/_client/",
|
|
132
|
+
basePath: "",
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const buildFingerprint = await createBuildFingerprint(
|
|
136
|
+
entryChunk,
|
|
137
|
+
cssChunks,
|
|
138
|
+
routes,
|
|
139
|
+
root,
|
|
140
|
+
serverEntry
|
|
141
|
+
);
|
|
142
|
+
const buildId = Bun.hash(buildFingerprint).toString(16).slice(0, 12);
|
|
143
|
+
targetManifest.buildId = buildId;
|
|
144
|
+
|
|
145
|
+
// Write index.html with the buildId meta tag injected so the client can
|
|
146
|
+
// detect stale deploys via X-Furin-Build-ID header comparison.
|
|
147
|
+
const clientDir = join(targetDir, "client");
|
|
148
|
+
writeFileSync(
|
|
149
|
+
join(clientDir, "index.html"),
|
|
150
|
+
generateProdIndexHtml(entryChunk, cssChunks, buildId)
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const routeManifest = routes.map((r) => ({ pattern: r.pattern, path: r.path, mode: r.mode }));
|
|
154
|
+
const publicDir = existsSync(join(rootDir, "public")) ? join(rootDir, "public") : undefined;
|
|
155
|
+
const targetPublicDir = publicDir ? join(targetDir, "public") : undefined;
|
|
156
|
+
|
|
157
|
+
if (publicDir && targetPublicDir && options.compile !== "embed") {
|
|
158
|
+
copyDirRecursive(publicDir, targetPublicDir);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const { rootConventions, routeMetadata } = buildCompileMetadata(root, routes);
|
|
162
|
+
|
|
163
|
+
if (options.compile && serverEntry) {
|
|
164
|
+
const clientDir = join(targetDir, "client");
|
|
165
|
+
const outfile = join(targetDir, "server");
|
|
166
|
+
|
|
167
|
+
const entryPath = generateCompileEntry({
|
|
168
|
+
buildId,
|
|
169
|
+
rootPath: root.path,
|
|
170
|
+
routes: routeManifest,
|
|
171
|
+
serverEntry,
|
|
172
|
+
outDir: targetDir,
|
|
173
|
+
embed: options.compile === "embed" ? { clientDir } : undefined,
|
|
174
|
+
publicDir,
|
|
175
|
+
rootConventions,
|
|
176
|
+
routeMetadata,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
await Bun.build({
|
|
180
|
+
entrypoints: [entryPath],
|
|
181
|
+
compile: { outfile },
|
|
182
|
+
minify: true,
|
|
183
|
+
sourcemap: "linked",
|
|
184
|
+
define: { "process.env.NODE_ENV": JSON.stringify("production") },
|
|
185
|
+
plugins: options.plugins,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
console.log(`[furin] Server binary: ${outfile}`);
|
|
189
|
+
|
|
190
|
+
targetManifest.serverPath = toPosixPath(join(targetManifest.targetDir, "server"));
|
|
191
|
+
|
|
192
|
+
// Embed mode: assets are in the binary — clean up client dir too.
|
|
193
|
+
if (options.compile === "embed") {
|
|
194
|
+
rmSync(clientDir, { force: true, recursive: true });
|
|
195
|
+
targetManifest.clientDir = null;
|
|
196
|
+
targetManifest.templatePath = null;
|
|
197
|
+
}
|
|
198
|
+
} else if (serverEntry) {
|
|
199
|
+
// Disk mode: generate server.ts then bundle it into self-contained server.js
|
|
200
|
+
const entryPath = generateServerRoutesEntry({
|
|
201
|
+
buildId,
|
|
202
|
+
rootPath: root.path,
|
|
203
|
+
routes: routeManifest,
|
|
204
|
+
serverEntry,
|
|
205
|
+
outDir: targetDir,
|
|
206
|
+
rootConventions,
|
|
207
|
+
routeMetadata,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
await Bun.build({
|
|
211
|
+
entrypoints: [entryPath],
|
|
212
|
+
outdir: targetDir,
|
|
213
|
+
target: "bun",
|
|
214
|
+
minify: true,
|
|
215
|
+
sourcemap: "linked",
|
|
216
|
+
plugins: options.plugins,
|
|
217
|
+
});
|
|
218
|
+
console.log(
|
|
219
|
+
`[furin] Server bundle: ${toPosixPath(join(targetManifest.targetDir, "server.js"))}`
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
targetManifest.serverPath = toPosixPath(join(targetManifest.targetDir, "server.js"));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Clean up build intermediates — no longer needed once the bundle/binary is built.
|
|
226
|
+
// NOTE: "index.html" is intentionally absent — generateProdIndexHtml writes
|
|
227
|
+
// the final artifact to client/index.html (inside targetDir/client/), not to
|
|
228
|
+
// targetDir directly, so there is nothing to remove here.
|
|
229
|
+
for (const file of [
|
|
230
|
+
"_compile-entry.ts",
|
|
231
|
+
"_compile-entry.js.map",
|
|
232
|
+
"server.ts", // disk mode intermediate
|
|
233
|
+
"_hydrate.tsx",
|
|
234
|
+
]) {
|
|
235
|
+
rmSync(join(targetDir, file), { force: true });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return targetManifest;
|
|
239
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { cpSync, existsSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { buildClient } from "../build/client.ts";
|
|
4
|
+
import { ensureDir, toPosixPath } from "../build/shared.ts";
|
|
5
|
+
import type { BuildAppOptions, StaticTargetBuildManifest } from "../build/types.ts";
|
|
6
|
+
import type { StaticExportConfig } from "../config.ts";
|
|
7
|
+
import { resolvePath } from "../render/assemble.ts";
|
|
8
|
+
import { prerenderSSG } from "../render/index.ts";
|
|
9
|
+
import { generateProdIndexHtml } from "../render/shell.ts";
|
|
10
|
+
import { setProductionTemplateContent } from "../render/template.ts";
|
|
11
|
+
import type { ResolvedRoute, RootLayout } from "../router.ts";
|
|
12
|
+
|
|
13
|
+
/** Maximum concurrent pre-render calls (mirrors warmSSGCache). */
|
|
14
|
+
const STATIC_CONCURRENCY = 4;
|
|
15
|
+
|
|
16
|
+
/** Pattern that identifies a dynamic route segment. */
|
|
17
|
+
const DYNAMIC_SEGMENT_RE = /\/:[^/]+|\/\*/;
|
|
18
|
+
|
|
19
|
+
/** Strips all trailing slashes from a string. */
|
|
20
|
+
const TRAILING_SLASHES_RE = /\/+$/;
|
|
21
|
+
|
|
22
|
+
// ── Path helpers ──────────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Converts a URL path to a filesystem output path.
|
|
26
|
+
* "/" → "outDir/index.html"
|
|
27
|
+
* "/docs/routing" → "outDir/docs/routing/index.html"
|
|
28
|
+
*
|
|
29
|
+
* The resolved path is validated to stay within `outDir` so that `..`
|
|
30
|
+
* segments injected via dynamic `staticParams()` values cannot escape
|
|
31
|
+
* the output directory.
|
|
32
|
+
*/
|
|
33
|
+
function pathToOutputFile(urlPath: string, outDir: string): string {
|
|
34
|
+
const normalizedOutDir = resolve(outDir);
|
|
35
|
+
if (urlPath === "/" || urlPath === "") {
|
|
36
|
+
return join(normalizedOutDir, "index.html");
|
|
37
|
+
}
|
|
38
|
+
const resolved = resolve(normalizedOutDir, urlPath.slice(1), "index.html");
|
|
39
|
+
if (!resolved.startsWith(`${normalizedOutDir}${sep}`)) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`[furin] static: unsafe output path detected for URL "${urlPath}" — path traversal via ".." is not allowed.`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return resolved;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ── Pre-render worker ─────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
async function prerenderAndWrite(
|
|
50
|
+
route: ResolvedRoute,
|
|
51
|
+
params: Record<string, string>,
|
|
52
|
+
root: RootLayout,
|
|
53
|
+
outDir: string,
|
|
54
|
+
renderedRoutes: string[],
|
|
55
|
+
skippedRoutes: string[],
|
|
56
|
+
basePath: string
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
const urlPath = resolvePath(route.pattern, params);
|
|
59
|
+
const outputFile = pathToOutputFile(urlPath, outDir);
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const entry = await prerenderSSG(route, params, root, "http://localhost", basePath);
|
|
63
|
+
|
|
64
|
+
if (entry instanceof Response) {
|
|
65
|
+
console.warn(
|
|
66
|
+
`[furin] static: route "${route.pattern}" loader returned a redirect — skipping.`
|
|
67
|
+
);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ensureDir(dirname(outputFile));
|
|
72
|
+
writeFileSync(outputFile, entry.html);
|
|
73
|
+
renderedRoutes.push(urlPath);
|
|
74
|
+
console.log(`[furin] static: ${urlPath} → ${toPosixPath(outputFile)}`);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error(
|
|
77
|
+
`[furin] static: prerender failed for "${route.pattern}" (params: ${JSON.stringify(params)}):`,
|
|
78
|
+
err
|
|
79
|
+
);
|
|
80
|
+
skippedRoutes.push(urlPath);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ── SSR validation ────────────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns only the SSG-eligible routes.
|
|
88
|
+
* Throws when onSSR="error" and non-SSG routes exist; warns+skips otherwise.
|
|
89
|
+
* Pushes skipped patterns into `skippedRoutes`.
|
|
90
|
+
*/
|
|
91
|
+
function collectSsgRoutes(
|
|
92
|
+
routes: ResolvedRoute[],
|
|
93
|
+
onSSR: "error" | "skip",
|
|
94
|
+
skippedRoutes: string[]
|
|
95
|
+
): ResolvedRoute[] {
|
|
96
|
+
const nonSsg = routes.filter((r) => r.mode !== "ssg");
|
|
97
|
+
|
|
98
|
+
if (nonSsg.length === 0) {
|
|
99
|
+
return routes;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (onSSR === "error") {
|
|
103
|
+
const list = nonSsg.map((r) => ` • ${r.pattern} (mode: ${r.mode})`).join("\n");
|
|
104
|
+
throw new Error(
|
|
105
|
+
"[furin] Cannot produce a static export: the following routes are not SSG and cannot be pre-rendered.\n" +
|
|
106
|
+
`Either change their mode to "ssg", remove them, or set \`onSSR: "skip"\` in your static config.\n\n` +
|
|
107
|
+
`${list}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// onSSR === "skip"
|
|
112
|
+
for (const r of nonSsg) {
|
|
113
|
+
console.warn(
|
|
114
|
+
`[furin] static: skipping route "${r.pattern}" (mode=${r.mode}) — not statically exportable.`
|
|
115
|
+
);
|
|
116
|
+
skippedRoutes.push(r.pattern);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return routes.filter((r) => r.mode === "ssg");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── Task queue builder ────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Builds the flat list of pre-render tasks for all SSG routes.
|
|
126
|
+
* Dynamic routes without staticParams() are warned about and pushed to skippedRoutes.
|
|
127
|
+
*/
|
|
128
|
+
async function buildTaskQueue(
|
|
129
|
+
ssgRoutes: ResolvedRoute[],
|
|
130
|
+
root: RootLayout,
|
|
131
|
+
outDir: string,
|
|
132
|
+
renderedRoutes: string[],
|
|
133
|
+
skippedRoutes: string[],
|
|
134
|
+
basePath: string
|
|
135
|
+
): Promise<Array<() => Promise<void>>> {
|
|
136
|
+
const tasks: Array<() => Promise<void>> = [];
|
|
137
|
+
|
|
138
|
+
for (const route of ssgRoutes) {
|
|
139
|
+
if (!DYNAMIC_SEGMENT_RE.test(route.pattern)) {
|
|
140
|
+
tasks.push(() =>
|
|
141
|
+
prerenderAndWrite(route, {}, root, outDir, renderedRoutes, skippedRoutes, basePath)
|
|
142
|
+
);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!route.page.staticParams) {
|
|
147
|
+
console.warn(
|
|
148
|
+
`[furin] static: skipping dynamic route "${route.pattern}" — no staticParams() defined.`
|
|
149
|
+
);
|
|
150
|
+
skippedRoutes.push(route.pattern);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
const paramSets = (await route.page.staticParams()) ?? [];
|
|
156
|
+
for (const params of paramSets) {
|
|
157
|
+
tasks.push(() =>
|
|
158
|
+
prerenderAndWrite(route, params, root, outDir, renderedRoutes, skippedRoutes, basePath)
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
} catch (err) {
|
|
162
|
+
console.error(`[furin] static: staticParams() failed for "${route.pattern}":`, err);
|
|
163
|
+
skippedRoutes.push(route.pattern);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return tasks;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ── Concurrency runner ────────────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
async function runWithConcurrency(
|
|
173
|
+
tasks: Array<() => Promise<void>>,
|
|
174
|
+
concurrency: number
|
|
175
|
+
): Promise<void> {
|
|
176
|
+
const queue = [...tasks];
|
|
177
|
+
const workerCount = Math.min(concurrency, Math.max(queue.length, 1));
|
|
178
|
+
await Promise.all(
|
|
179
|
+
Array.from({ length: workerCount }, async () => {
|
|
180
|
+
while (queue.length > 0) {
|
|
181
|
+
await queue.shift()?.();
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ── Main adapter ──────────────────────────────────────────────────────────────
|
|
188
|
+
|
|
189
|
+
export async function buildStaticTarget(
|
|
190
|
+
routes: ResolvedRoute[],
|
|
191
|
+
rootDir: string,
|
|
192
|
+
buildRoot: string,
|
|
193
|
+
root: RootLayout,
|
|
194
|
+
options: BuildAppOptions
|
|
195
|
+
): Promise<StaticTargetBuildManifest> {
|
|
196
|
+
const staticConfig: StaticExportConfig = options.staticConfig ?? {};
|
|
197
|
+
|
|
198
|
+
// Normalize basePath: treat "" and "/" identically as no prefix; strip any
|
|
199
|
+
// trailing slash so `${basePath}/_client/` never produces double slashes.
|
|
200
|
+
const rawBasePath = staticConfig.basePath ?? "";
|
|
201
|
+
let basePath: string;
|
|
202
|
+
if (rawBasePath === "/" || rawBasePath === "") {
|
|
203
|
+
basePath = "";
|
|
204
|
+
} else if (rawBasePath.endsWith("/")) {
|
|
205
|
+
basePath = rawBasePath.replace(TRAILING_SLASHES_RE, "");
|
|
206
|
+
} else {
|
|
207
|
+
basePath = rawBasePath;
|
|
208
|
+
}
|
|
209
|
+
if (basePath && !basePath.startsWith("/")) {
|
|
210
|
+
throw new Error(
|
|
211
|
+
`[furin] static: basePath must start with "/" (received "${rawBasePath}"). ` +
|
|
212
|
+
`Use "" for root deployments or "/sub-path" for sub-path deployments.`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const outDir = resolve(rootDir, staticConfig.outDir ?? "dist");
|
|
217
|
+
|
|
218
|
+
// Guard against destructive rmSync on obviously wrong output directories.
|
|
219
|
+
const normalizedRoot = resolve(rootDir);
|
|
220
|
+
const normalizedBuildRoot = resolve(buildRoot);
|
|
221
|
+
if (
|
|
222
|
+
outDir === normalizedRoot ||
|
|
223
|
+
outDir === normalizedBuildRoot ||
|
|
224
|
+
outDir === "/" ||
|
|
225
|
+
outDir === "."
|
|
226
|
+
) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
`[furin] static: outDir resolves to "${outDir}" which is unsafe to delete. ` +
|
|
229
|
+
`Use a dedicated output directory such as "dist".`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
// Also reject ancestor paths: if rootDir or buildRoot is inside outDir, deleting
|
|
233
|
+
// outDir would destroy them. path.relative(outDir, x) not starting with ".." means x
|
|
234
|
+
// is inside (or equal to) outDir. On Windows, paths on different drives produce an
|
|
235
|
+
// absolute result from relative(), which also indicates they are outside outDir.
|
|
236
|
+
function isOutsideOutDir(target: string): boolean {
|
|
237
|
+
const rel = relative(outDir, target);
|
|
238
|
+
return rel.startsWith("..") || isAbsolute(rel);
|
|
239
|
+
}
|
|
240
|
+
if (!(isOutsideOutDir(normalizedRoot) && isOutsideOutDir(normalizedBuildRoot))) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
`[furin] static: outDir resolves to "${outDir}" which is unsafe to delete. ` +
|
|
243
|
+
`Use a dedicated output directory such as "dist".`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const onSSR = staticConfig.onSSR ?? "error";
|
|
248
|
+
const publicPath = basePath ? `${basePath}/_client/` : "/_client/";
|
|
249
|
+
|
|
250
|
+
const renderedRoutes: string[] = [];
|
|
251
|
+
const skippedRoutes: string[] = [];
|
|
252
|
+
|
|
253
|
+
// ── 1. Validate SSR/ISR routes before doing any work ─────────────────────
|
|
254
|
+
const ssgRoutes = collectSsgRoutes(routes, onSSR, skippedRoutes);
|
|
255
|
+
|
|
256
|
+
// ── 2. Clean & create output directories ─────────────────────────────────
|
|
257
|
+
rmSync(outDir, { force: true, recursive: true });
|
|
258
|
+
ensureDir(outDir);
|
|
259
|
+
|
|
260
|
+
const targetDir = join(buildRoot, "static");
|
|
261
|
+
rmSync(targetDir, { force: true, recursive: true });
|
|
262
|
+
ensureDir(targetDir);
|
|
263
|
+
|
|
264
|
+
// ── 3. Build client bundle ────────────────────────────────────────────────
|
|
265
|
+
const { entryChunk, cssChunks } = await buildClient(ssgRoutes, {
|
|
266
|
+
outDir: targetDir,
|
|
267
|
+
rootLayout: root.path,
|
|
268
|
+
plugins: options.plugins,
|
|
269
|
+
publicPath,
|
|
270
|
+
basePath,
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// ── 4. Generate HTML shell template ──────────────────────────────────────
|
|
274
|
+
// Emit a <link rel="icon"> only when a favicon.ico exists in the public dir.
|
|
275
|
+
// This ensures the browser finds it even when the site is served from a sub-path.
|
|
276
|
+
const publicFavicon = join(rootDir, "public", "favicon.ico");
|
|
277
|
+
const faviconHref = existsSync(publicFavicon) ? `${basePath}/favicon.ico` : undefined;
|
|
278
|
+
const shellHtml = generateProdIndexHtml(entryChunk, cssChunks, undefined, faviconHref);
|
|
279
|
+
|
|
280
|
+
// ── 5. Prime the renderer with the production shell ──────────────────────
|
|
281
|
+
// Must be called before any prerenderSSG invocation.
|
|
282
|
+
// prepareRender() checks getProductionTemplate() first (before IS_DEV), so
|
|
283
|
+
// setting the content here is sufficient — no need to flip IS_DEV globally.
|
|
284
|
+
setProductionTemplateContent(shellHtml);
|
|
285
|
+
|
|
286
|
+
// ── 6. Copy public/ → outDir/ ─────────────────────────────────────────────
|
|
287
|
+
const publicSrcDir = join(rootDir, "public");
|
|
288
|
+
if (existsSync(publicSrcDir)) {
|
|
289
|
+
cpSync(publicSrcDir, outDir, { recursive: true });
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── 7. Copy _client/ chunks → outDir/_client/ ────────────────────────────
|
|
293
|
+
const clientSrcDir = join(targetDir, "client");
|
|
294
|
+
if (existsSync(clientSrcDir)) {
|
|
295
|
+
cpSync(clientSrcDir, join(outDir, "_client"), { recursive: true });
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ── 8. Pre-render SSG routes ──────────────────────────────────────────────
|
|
299
|
+
const tasks = await buildTaskQueue(
|
|
300
|
+
ssgRoutes,
|
|
301
|
+
root,
|
|
302
|
+
outDir,
|
|
303
|
+
renderedRoutes,
|
|
304
|
+
skippedRoutes,
|
|
305
|
+
basePath
|
|
306
|
+
);
|
|
307
|
+
// Snapshot after queue-building: routes skipped due to missing staticParams() are
|
|
308
|
+
// already recorded; only routes added during task execution are prerender failures.
|
|
309
|
+
const afterQueueCount = skippedRoutes.length;
|
|
310
|
+
await runWithConcurrency(tasks, STATIC_CONCURRENCY);
|
|
311
|
+
|
|
312
|
+
// Fail the build when onSSR="error" and any prerender task was recorded as skipped.
|
|
313
|
+
if (onSSR === "error" && skippedRoutes.length > afterQueueCount) {
|
|
314
|
+
const failed = skippedRoutes.slice(afterQueueCount);
|
|
315
|
+
const list = failed.map((r) => ` • ${r}`).join("\n");
|
|
316
|
+
throw new Error(
|
|
317
|
+
`[furin] static: ${failed.length} route(s) failed to pre-render:\n${list}\n` +
|
|
318
|
+
`Set \`onSSR: "skip"\` in your static config to suppress this error.`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// ── 9. Write 404.html (SPA fallback for GitHub Pages) ───────────────────
|
|
323
|
+
writeFileSync(join(outDir, "404.html"), shellHtml);
|
|
324
|
+
console.log("[furin] static: wrote 404.html (SPA shell fallback)");
|
|
325
|
+
|
|
326
|
+
// ── 10. Clean up intermediate build artifacts ────────────────────────────
|
|
327
|
+
rmSync(targetDir, { force: true, recursive: true });
|
|
328
|
+
|
|
329
|
+
console.log(
|
|
330
|
+
`[furin] static: export complete → ${toPosixPath(outDir)}` +
|
|
331
|
+
` (${renderedRoutes.length} rendered, ${skippedRoutes.length} skipped)`
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
basePath,
|
|
336
|
+
generatedAt: new Date().toISOString(),
|
|
337
|
+
outDir: toPosixPath(outDir),
|
|
338
|
+
renderedRoutes,
|
|
339
|
+
skippedRoutes,
|
|
340
|
+
};
|
|
341
|
+
}
|