@teyik0/furin 0.1.0-alpha.9 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/adapter/bun.d.ts +2 -2
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/client.d.ts +2 -2
- package/dist/build/compile-entry.d.ts +2 -16
- package/dist/build/compile-entry.ts +109 -0
- package/dist/build/entry-template.d.ts +24 -0
- package/dist/build/entry-template.ts +156 -0
- package/dist/build/hydrate.d.ts +3 -3
- package/dist/build/index.d.ts +1 -0
- package/dist/build/index.js +4027 -685
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/server-routes-entry.d.ts +2 -12
- package/dist/build/server-routes-entry.ts +36 -0
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +4 -0
- package/dist/build/types.d.ts +5 -1
- package/dist/cli/index.js +4007 -687
- package/dist/client/boundaries.d.ts +117 -0
- package/dist/client/default-screens.d.ts +17 -0
- package/dist/client/link.d.ts +22 -0
- package/dist/client/link.js +3450 -0
- package/dist/client/router/boundary-tree.d.ts +26 -0
- package/dist/client/router/context.d.ts +8 -0
- package/dist/client/router/history.d.ts +9 -0
- package/dist/client/router/index.d.ts +7 -0
- package/dist/client/router/link-utils.d.ts +51 -0
- package/dist/client/router/provider.d.ts +4 -0
- package/dist/client/router/spa-response.d.ts +58 -0
- package/dist/{link.d.ts → client/router/types.d.ts} +114 -80
- package/dist/client/server-error.d.ts +12 -0
- package/dist/client.d.ts +51 -4
- package/dist/client.js +77 -1
- package/dist/config.d.ts +12 -2
- package/dist/config.js +1 -0
- package/dist/env.d.ts +50 -0
- package/dist/furin.d.ts +37 -2
- package/dist/furin.js +5291 -662
- package/dist/plugin/index.js +140 -48
- package/dist/plugin/transform-client.d.ts +2 -1
- package/dist/server/auto-invalidate/index.d.ts +4 -0
- package/dist/server/auto-invalidate/plugin.d.ts +38 -0
- package/dist/server/auto-invalidate/registry.d.ts +9 -0
- package/dist/server/auto-invalidate/runtime.d.ts +9 -0
- package/dist/server/auto-invalidate/types.d.ts +12 -0
- package/dist/server/cache/dev-loader.d.ts +28 -0
- package/dist/server/cache/index.d.ts +10 -0
- package/dist/server/cache/invalidation.d.ts +12 -0
- package/dist/server/cache/isr-ssg.d.ts +26 -0
- package/dist/server/cache/isr.d.ts +6 -0
- package/dist/server/cache/registry.d.ts +5 -0
- package/dist/server/cache/route-cache.d.ts +26 -0
- package/dist/server/cache/ssg.d.ts +5 -0
- package/dist/{context-logger.d.ts → server/context-logger.d.ts} +2 -0
- package/dist/server/dev-inspector.d.ts +89 -0
- package/dist/{internal.d.ts → server/internal.d.ts} +16 -0
- package/dist/server/internal.js +17 -0
- package/dist/server/lang-detect.d.ts +5 -0
- package/dist/server/render/assemble.d.ts +47 -0
- package/dist/server/render/element.d.ts +24 -0
- package/dist/server/render/index.d.ts +7 -0
- package/dist/server/render/index.ts +22 -0
- package/dist/server/render/isr.d.ts +3 -0
- package/dist/server/render/loaders.d.ts +64 -0
- package/dist/server/render/not-found.d.ts +6 -0
- package/dist/{render → server/render}/shell.d.ts +17 -7
- package/{src → dist/server}/render/shell.ts +28 -13
- package/dist/server/render/ssg.d.ts +8 -0
- package/dist/server/render/ssr.d.ts +107 -0
- package/dist/server/router/discovery.d.ts +16 -0
- package/dist/server/router/hmr.d.ts +64 -0
- package/dist/server/router/index.d.ts +6 -0
- package/dist/server/router/index.js +28 -0
- package/dist/server/router/index.ts +29 -0
- package/dist/server/router/patterns.d.ts +40 -0
- package/dist/server/router/plugin.d.ts +20 -0
- package/dist/server/router/schemas.d.ts +51 -0
- package/dist/server/router/types.d.ts +60 -0
- package/dist/server/runtime-env.js +10 -0
- package/dist/shared/await.d.ts +67 -0
- package/dist/shared/deferred-ndjson.d.ts +32 -0
- package/dist/shared/digest.d.ts +16 -0
- package/dist/shared/error.d.ts +37 -0
- package/dist/shared/not-found.d.ts +19 -0
- package/dist/shared/utils/ast-walk.d.ts +18 -0
- package/dist/shared/utils/index.d.ts +13 -0
- package/package.json +56 -25
- package/src/adapter/bun.ts +90 -41
- package/src/adapter/static.ts +79 -36
- package/src/build/client.ts +8 -12
- package/src/build/compile-entry.ts +20 -14
- package/src/build/entry-template.ts +90 -8
- package/src/build/hydrate.ts +213 -36
- package/src/build/index.ts +10 -9
- package/src/build/route-types.ts +30 -4
- package/src/build/scan-server.ts +52 -67
- package/src/build/server-routes-entry.ts +7 -11
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +31 -0
- package/src/build/types.ts +5 -1
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +259 -0
- package/src/client/default-screens.tsx +304 -0
- package/src/client/link.tsx +338 -0
- package/src/client/router/boundary-tree.tsx +116 -0
- package/src/client/router/context.ts +38 -0
- package/src/client/router/history.ts +41 -0
- package/src/client/router/index.ts +47 -0
- package/src/client/router/link-utils.ts +180 -0
- package/src/client/router/provider.tsx +723 -0
- package/src/client/router/spa-response.ts +143 -0
- package/src/client/router/types.ts +263 -0
- package/src/client/server-error.ts +47 -0
- package/src/client.ts +80 -3
- package/src/config.ts +11 -3
- package/src/env.d.ts +1 -0
- package/src/furin.ts +104 -26
- package/src/plugin/index.ts +7 -8
- package/src/plugin/transform-client.ts +146 -120
- package/src/server/auto-invalidate/index.ts +10 -0
- package/src/server/auto-invalidate/plugin.ts +25 -0
- package/src/server/auto-invalidate/registry.ts +60 -0
- package/src/server/auto-invalidate/runtime.ts +133 -0
- package/src/server/auto-invalidate/types.ts +16 -0
- package/src/server/cache/dev-loader.ts +211 -0
- package/src/server/cache/index.ts +34 -0
- package/src/server/cache/invalidation.ts +111 -0
- package/src/server/cache/isr-ssg.ts +41 -0
- package/src/server/cache/isr.ts +17 -0
- package/src/server/cache/registry.ts +35 -0
- package/src/server/cache/route-cache.ts +124 -0
- package/src/server/cache/ssg.ts +15 -0
- package/src/{context-logger.ts → server/context-logger.ts} +5 -0
- package/src/server/dev-inspector.ts +60 -0
- package/src/{dev-page-plugin.ts → server/dev-page-plugin.ts} +15 -0
- package/src/{internal.ts → server/internal.ts} +17 -0
- package/src/server/lang-detect.ts +46 -0
- package/src/server/render/assemble.ts +133 -0
- package/src/server/render/element.tsx +113 -0
- package/src/server/render/index.ts +22 -0
- package/src/server/render/isr.ts +295 -0
- package/src/server/render/loaders.ts +328 -0
- package/src/server/render/not-found.ts +96 -0
- package/src/server/render/shell.ts +202 -0
- package/src/server/render/ssg.ts +137 -0
- package/src/server/render/ssr.ts +545 -0
- package/src/server/router/discovery.ts +474 -0
- package/src/server/router/hmr.ts +372 -0
- package/src/server/router/index.ts +29 -0
- package/src/server/router/patterns.ts +214 -0
- package/src/server/router/plugin.ts +401 -0
- package/src/server/router/schemas.ts +163 -0
- package/src/server/router/types.ts +59 -0
- package/src/shared/await.tsx +192 -0
- package/src/shared/deferred-ndjson.ts +244 -0
- package/src/shared/digest.ts +71 -0
- package/src/shared/error.ts +49 -0
- package/src/shared/not-found.ts +40 -0
- package/src/shared/utils/ast-walk.ts +51 -0
- package/src/{utils.ts → shared/utils/index.ts} +32 -1
- package/dist/link.js +0 -1232
- package/dist/render/assemble.d.ts +0 -15
- package/dist/render/cache.d.ts +0 -95
- package/dist/render/element.d.ts +0 -4
- package/dist/render/index.d.ts +0 -27
- package/dist/render/loaders.d.ts +0 -12
- package/dist/router.d.ts +0 -45
- package/dist/router.js +0 -1671
- package/dist/utils.d.ts +0 -6
- package/src/link.tsx +0 -1120
- package/src/render/assemble.ts +0 -66
- package/src/render/cache.ts +0 -241
- package/src/render/element.tsx +0 -28
- package/src/render/index.ts +0 -483
- package/src/render/loaders.ts +0 -117
- package/src/router.ts +0 -439
- /package/dist/{dev-page-plugin.d.ts → server/dev-page-plugin.d.ts} +0 -0
- /package/dist/{render → server/render}/template.d.ts +0 -0
- /package/dist/{runtime-env.d.ts → server/runtime-env.d.ts} +0 -0
- /package/src/{render → server/render}/template.ts +0 -0
- /package/src/{runtime-env.ts → server/runtime-env.ts} +0 -0
package/src/adapter/bun.ts
CHANGED
|
@@ -5,34 +5,63 @@ import { buildClient } from "../build/client.ts";
|
|
|
5
5
|
import { generateCompileEntry } from "../build/compile-entry.ts";
|
|
6
6
|
import { generateServerRoutesEntry } from "../build/server-routes-entry.ts";
|
|
7
7
|
import { buildTargetManifest, copyDirRecursive, ensureDir, toPosixPath } from "../build/shared.ts";
|
|
8
|
+
import { buildSSGCacheSnapshot } from "../build/ssg-cache.ts";
|
|
8
9
|
import type { BuildAppOptions, TargetBuildManifest } from "../build/types.ts";
|
|
9
10
|
import type { BuildTarget } from "../config.ts";
|
|
10
|
-
import { generateProdIndexHtml } from "../render/shell.ts";
|
|
11
|
-
import
|
|
11
|
+
import { generateProdIndexHtml } from "../server/render/shell.ts";
|
|
12
|
+
import { setProductionTemplateContent } from "../server/render/template.ts";
|
|
13
|
+
import type { ResolvedRoute, RootLayout } from "../server/router/index.ts";
|
|
12
14
|
|
|
13
15
|
// import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
|
|
14
|
-
// through package exports, and is the Web-standard API.
|
|
15
|
-
|
|
16
|
+
// through package exports, and is the Web-standard API. The main entry is
|
|
17
|
+
// `src/furin.ts` (or `dist/furin.js`), so we strip two path segments to reach
|
|
18
|
+
// the package root.
|
|
19
|
+
const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
|
|
20
|
+
const _pkgSrcDir = existsSync(join(_pkgRoot, "src", "furin.ts"))
|
|
21
|
+
? join(_pkgRoot, "src")
|
|
22
|
+
: join(_pkgRoot, "dist");
|
|
23
|
+
// The published package always ships `src/` (see package.json "files"), and the
|
|
24
|
+
// "bun" export condition resolves to the TypeScript sources, so the fingerprint
|
|
25
|
+
// inputs are always the `.ts` files. Some of these inputs (e.g. entry-template)
|
|
26
|
+
// are never emitted to `dist/` as `.js`, so detecting the extension off the dist
|
|
27
|
+
// copy would point at files that don't exist and silently weaken the build ID.
|
|
28
|
+
const _ext = ".ts";
|
|
16
29
|
const BUILD_ID_INPUT_PATHS = [
|
|
17
|
-
`${_pkgSrcDir}/build/compile-entry
|
|
18
|
-
`${_pkgSrcDir}/build/entry-template
|
|
19
|
-
`${_pkgSrcDir}/build/server-routes-entry
|
|
20
|
-
`${_pkgSrcDir}/render/index
|
|
21
|
-
`${_pkgSrcDir}/render/shell
|
|
22
|
-
`${_pkgSrcDir}/router
|
|
30
|
+
`${_pkgSrcDir}/build/compile-entry${_ext}`,
|
|
31
|
+
`${_pkgSrcDir}/build/entry-template${_ext}`,
|
|
32
|
+
`${_pkgSrcDir}/build/server-routes-entry${_ext}`,
|
|
33
|
+
`${_pkgSrcDir}/server/render/index${_ext}`,
|
|
34
|
+
`${_pkgSrcDir}/server/render/shell${_ext}`,
|
|
35
|
+
`${_pkgSrcDir}/server/router/index${_ext}`,
|
|
23
36
|
];
|
|
24
37
|
|
|
25
38
|
async function createBuildFingerprint(
|
|
26
39
|
entryChunk: string,
|
|
27
40
|
cssChunks: string[],
|
|
28
41
|
routes: ResolvedRoute[],
|
|
29
|
-
|
|
42
|
+
root: RootLayout,
|
|
30
43
|
serverEntry: string | null
|
|
31
44
|
): Promise<string> {
|
|
32
|
-
const fingerprintPaths = new Set<string>([
|
|
45
|
+
const fingerprintPaths = new Set<string>([root.path, ...routes.map((route) => route.path)]);
|
|
33
46
|
if (serverEntry) {
|
|
34
47
|
fingerprintPaths.add(serverEntry);
|
|
35
48
|
}
|
|
49
|
+
if (root.errorPath) {
|
|
50
|
+
fingerprintPaths.add(root.errorPath);
|
|
51
|
+
}
|
|
52
|
+
if (root.notFoundPath) {
|
|
53
|
+
fingerprintPaths.add(root.notFoundPath);
|
|
54
|
+
}
|
|
55
|
+
for (const route of routes) {
|
|
56
|
+
for (const segment of route.segmentBoundaries) {
|
|
57
|
+
if (segment.errorPath) {
|
|
58
|
+
fingerprintPaths.add(segment.errorPath);
|
|
59
|
+
}
|
|
60
|
+
if (segment.notFoundPath) {
|
|
61
|
+
fingerprintPaths.add(segment.notFoundPath);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
36
65
|
for (const path of BUILD_ID_INPUT_PATHS) {
|
|
37
66
|
if (!existsSync(path)) {
|
|
38
67
|
// A missing framework source file would silently produce an empty-string
|
|
@@ -46,7 +75,7 @@ async function createBuildFingerprint(
|
|
|
46
75
|
}
|
|
47
76
|
|
|
48
77
|
const fileParts = await Promise.all(
|
|
49
|
-
[...fingerprintPaths].
|
|
78
|
+
[...fingerprintPaths].toSorted().map(async (path) => {
|
|
50
79
|
const content = existsSync(path) ? await Bun.file(path).text() : "";
|
|
51
80
|
return `${toPosixPath(path)}:${content}`;
|
|
52
81
|
})
|
|
@@ -58,14 +87,39 @@ async function createBuildFingerprint(
|
|
|
58
87
|
)
|
|
59
88
|
.sort();
|
|
60
89
|
|
|
61
|
-
return [entryChunk, ...[...cssChunks].
|
|
90
|
+
return [entryChunk, ...[...cssChunks].toSorted(), ...routeParts, ...fileParts].join("\n");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function buildCompileMetadata(root: RootLayout, routes: ResolvedRoute[]) {
|
|
94
|
+
const rootConventions =
|
|
95
|
+
root.errorPath || root.notFoundPath
|
|
96
|
+
? {
|
|
97
|
+
errorPath: root.errorPath ? toPosixPath(root.errorPath) : undefined,
|
|
98
|
+
notFoundPath: root.notFoundPath ? toPosixPath(root.notFoundPath) : undefined,
|
|
99
|
+
}
|
|
100
|
+
: undefined;
|
|
101
|
+
|
|
102
|
+
const routeMetadata: NonNullable<Parameters<typeof generateCompileEntry>[0]["routeMetadata"]> =
|
|
103
|
+
{};
|
|
104
|
+
for (const route of routes) {
|
|
105
|
+
routeMetadata[toPosixPath(route.path)] = {
|
|
106
|
+
segmentBoundaries: route.segmentBoundaries.map((b) => ({
|
|
107
|
+
depth: b.depth,
|
|
108
|
+
path: toPosixPath(b.path),
|
|
109
|
+
errorPath: b.errorPath ? toPosixPath(b.errorPath) : undefined,
|
|
110
|
+
notFoundPath: b.notFoundPath ? toPosixPath(b.notFoundPath) : undefined,
|
|
111
|
+
})),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { rootConventions, routeMetadata };
|
|
62
116
|
}
|
|
63
117
|
|
|
64
118
|
export async function buildBunTarget(
|
|
65
119
|
routes: ResolvedRoute[],
|
|
66
120
|
rootDir: string,
|
|
67
121
|
buildRoot: string,
|
|
68
|
-
|
|
122
|
+
root: RootLayout,
|
|
69
123
|
serverEntry: string | null,
|
|
70
124
|
options: BuildAppOptions
|
|
71
125
|
): Promise<TargetBuildManifest> {
|
|
@@ -85,17 +139,18 @@ export async function buildBunTarget(
|
|
|
85
139
|
|
|
86
140
|
const { entryChunk, cssChunks } = await buildClient(routes, {
|
|
87
141
|
outDir: targetDir,
|
|
88
|
-
rootLayout:
|
|
142
|
+
rootLayout: root.path,
|
|
89
143
|
plugins: options.plugins,
|
|
90
144
|
publicPath: "/_client/",
|
|
91
145
|
basePath: "",
|
|
146
|
+
clientLogging: options.clientLogging ?? false,
|
|
92
147
|
});
|
|
93
148
|
|
|
94
149
|
const buildFingerprint = await createBuildFingerprint(
|
|
95
150
|
entryChunk,
|
|
96
151
|
cssChunks,
|
|
97
152
|
routes,
|
|
98
|
-
|
|
153
|
+
root,
|
|
99
154
|
serverEntry
|
|
100
155
|
);
|
|
101
156
|
const buildId = Bun.hash(buildFingerprint).toString(16).slice(0, 12);
|
|
@@ -104,12 +159,14 @@ export async function buildBunTarget(
|
|
|
104
159
|
// Write index.html with the buildId meta tag injected so the client can
|
|
105
160
|
// detect stale deploys via X-Furin-Build-ID header comparison.
|
|
106
161
|
const clientDir = join(targetDir, "client");
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
);
|
|
162
|
+
const indexHtml = generateProdIndexHtml(entryChunk, cssChunks, buildId, undefined, false);
|
|
163
|
+
writeFileSync(join(clientDir, "index.html"), indexHtml);
|
|
164
|
+
setProductionTemplateContent(indexHtml);
|
|
111
165
|
|
|
112
166
|
const routeManifest = routes.map((r) => ({ pattern: r.pattern, path: r.path, mode: r.mode }));
|
|
167
|
+
const ssgCache = serverEntry
|
|
168
|
+
? await buildSSGCacheSnapshot(routes, root, "http://localhost")
|
|
169
|
+
: undefined;
|
|
113
170
|
const publicDir = existsSync(join(rootDir, "public")) ? join(rootDir, "public") : undefined;
|
|
114
171
|
const targetPublicDir = publicDir ? join(targetDir, "public") : undefined;
|
|
115
172
|
|
|
@@ -117,45 +174,34 @@ export async function buildBunTarget(
|
|
|
117
174
|
copyDirRecursive(publicDir, targetPublicDir);
|
|
118
175
|
}
|
|
119
176
|
|
|
177
|
+
const { rootConventions, routeMetadata } = buildCompileMetadata(root, routes);
|
|
178
|
+
|
|
120
179
|
if (options.compile && serverEntry) {
|
|
121
180
|
const clientDir = join(targetDir, "client");
|
|
122
181
|
const outfile = join(targetDir, "server");
|
|
123
182
|
|
|
124
183
|
const entryPath = generateCompileEntry({
|
|
125
184
|
buildId,
|
|
126
|
-
rootPath,
|
|
185
|
+
rootPath: root.path,
|
|
127
186
|
routes: routeManifest,
|
|
128
187
|
serverEntry,
|
|
129
188
|
outDir: targetDir,
|
|
130
189
|
embed: options.compile === "embed" ? { clientDir } : undefined,
|
|
131
190
|
publicDir,
|
|
191
|
+
rootConventions,
|
|
192
|
+
routeMetadata,
|
|
193
|
+
ssgCache,
|
|
132
194
|
});
|
|
133
195
|
|
|
134
196
|
await Bun.build({
|
|
135
197
|
entrypoints: [entryPath],
|
|
136
198
|
compile: { outfile },
|
|
137
199
|
minify: true,
|
|
138
|
-
sourcemap: "
|
|
200
|
+
sourcemap: "none",
|
|
139
201
|
define: { "process.env.NODE_ENV": JSON.stringify("production") },
|
|
140
202
|
plugins: options.plugins,
|
|
141
203
|
});
|
|
142
204
|
|
|
143
|
-
// macOS: Gatekeeper kills Bun-compiled binaries because Bun appends JS code to its own
|
|
144
|
-
// signed runtime, corrupting the original Oven signature. We must remove the corrupt
|
|
145
|
-
// signature first, then re-sign ad-hoc (-s -) so macOS accepts the binary.
|
|
146
|
-
if (process.platform === "darwin") {
|
|
147
|
-
Bun.spawnSync(["codesign", "--remove-signature", outfile]);
|
|
148
|
-
const sign = Bun.spawnSync(["codesign", "-f", "-s", "-", outfile]);
|
|
149
|
-
if (sign.exitCode !== 0) {
|
|
150
|
-
console.warn(
|
|
151
|
-
`[furin] Warning: codesign failed (exit ${sign.exitCode}). ` +
|
|
152
|
-
"The binary may be killed by Gatekeeper on macOS. Run manually:\n" +
|
|
153
|
-
` codesign --remove-signature ${outfile}\n` +
|
|
154
|
-
` codesign -f -s - ${outfile}`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
205
|
console.log(`[furin] Server binary: ${outfile}`);
|
|
160
206
|
|
|
161
207
|
targetManifest.serverPath = toPosixPath(join(targetManifest.targetDir, "server"));
|
|
@@ -170,10 +216,13 @@ export async function buildBunTarget(
|
|
|
170
216
|
// Disk mode: generate server.ts then bundle it into self-contained server.js
|
|
171
217
|
const entryPath = generateServerRoutesEntry({
|
|
172
218
|
buildId,
|
|
173
|
-
rootPath,
|
|
219
|
+
rootPath: root.path,
|
|
174
220
|
routes: routeManifest,
|
|
175
221
|
serverEntry,
|
|
176
222
|
outDir: targetDir,
|
|
223
|
+
rootConventions,
|
|
224
|
+
routeMetadata,
|
|
225
|
+
ssgCache,
|
|
177
226
|
});
|
|
178
227
|
|
|
179
228
|
await Bun.build({
|
|
@@ -181,7 +230,7 @@ export async function buildBunTarget(
|
|
|
181
230
|
outdir: targetDir,
|
|
182
231
|
target: "bun",
|
|
183
232
|
minify: true,
|
|
184
|
-
sourcemap: "
|
|
233
|
+
sourcemap: "none",
|
|
185
234
|
plugins: options.plugins,
|
|
186
235
|
});
|
|
187
236
|
console.log(
|
package/src/adapter/static.ts
CHANGED
|
@@ -4,11 +4,12 @@ import { buildClient } from "../build/client.ts";
|
|
|
4
4
|
import { ensureDir, toPosixPath } from "../build/shared.ts";
|
|
5
5
|
import type { BuildAppOptions, StaticTargetBuildManifest } from "../build/types.ts";
|
|
6
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";
|
|
7
|
+
import { resolvePath } from "../server/render/assemble.ts";
|
|
8
|
+
import { prerenderSSG } from "../server/render/index.ts";
|
|
9
|
+
import { generateProdIndexHtml } from "../server/render/shell.ts";
|
|
10
|
+
import { setProductionTemplateContent } from "../server/render/template.ts";
|
|
11
|
+
import type { ResolvedRoute, RootLayout } from "../server/router/index.ts";
|
|
12
|
+
import { mapWithConcurrency } from "../shared/utils/index.ts";
|
|
12
13
|
|
|
13
14
|
/** Maximum concurrent pre-render calls (mirrors warmSSGCache). */
|
|
14
15
|
const STATIC_CONCURRENCY = 4;
|
|
@@ -22,20 +23,21 @@ const TRAILING_SLASHES_RE = /\/+$/;
|
|
|
22
23
|
// ── Path helpers ──────────────────────────────────────────────────────────────
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
|
-
* Converts a URL path to a filesystem output path.
|
|
26
|
-
* "/" → "outDir/index.html"
|
|
27
|
-
* "/docs/routing" → "outDir/docs/routing/index.html"
|
|
26
|
+
* Converts a URL path to a filesystem output path for the given file name.
|
|
27
|
+
* "/" + "index.html" → "outDir/index.html"
|
|
28
|
+
* "/docs/routing" + "index.html" → "outDir/docs/routing/index.html"
|
|
29
|
+
* "/docs/routing" + "__furin_data.ndjson" → "outDir/docs/routing/__furin_data.ndjson"
|
|
28
30
|
*
|
|
29
31
|
* The resolved path is validated to stay within `outDir` so that `..`
|
|
30
32
|
* segments injected via dynamic `staticParams()` values cannot escape
|
|
31
33
|
* the output directory.
|
|
32
34
|
*/
|
|
33
|
-
function pathToOutputFile(urlPath: string, outDir: string): string {
|
|
35
|
+
function pathToOutputFile(urlPath: string, outDir: string, fileName: string): string {
|
|
34
36
|
const normalizedOutDir = resolve(outDir);
|
|
35
37
|
if (urlPath === "/" || urlPath === "") {
|
|
36
|
-
return join(normalizedOutDir,
|
|
38
|
+
return join(normalizedOutDir, fileName);
|
|
37
39
|
}
|
|
38
|
-
const resolved = resolve(normalizedOutDir, urlPath.slice(1),
|
|
40
|
+
const resolved = resolve(normalizedOutDir, urlPath.slice(1), fileName);
|
|
39
41
|
if (!resolved.startsWith(`${normalizedOutDir}${sep}`)) {
|
|
40
42
|
throw new Error(
|
|
41
43
|
`[furin] static: unsafe output path detected for URL "${urlPath}" — path traversal via ".." is not allowed.`
|
|
@@ -56,7 +58,8 @@ async function prerenderAndWrite(
|
|
|
56
58
|
basePath: string
|
|
57
59
|
): Promise<void> {
|
|
58
60
|
const urlPath = resolvePath(route.pattern, params);
|
|
59
|
-
const
|
|
61
|
+
const htmlOutputFile = pathToOutputFile(urlPath, outDir, "index.html");
|
|
62
|
+
const dataOutputFile = pathToOutputFile(urlPath, outDir, "__furin_data.ndjson");
|
|
60
63
|
|
|
61
64
|
try {
|
|
62
65
|
const entry = await prerenderSSG(route, params, root, "http://localhost", basePath);
|
|
@@ -68,10 +71,14 @@ async function prerenderAndWrite(
|
|
|
68
71
|
return;
|
|
69
72
|
}
|
|
70
73
|
|
|
71
|
-
ensureDir(dirname(
|
|
72
|
-
writeFileSync(
|
|
74
|
+
ensureDir(dirname(htmlOutputFile));
|
|
75
|
+
writeFileSync(htmlOutputFile, entry.html);
|
|
76
|
+
// Persist the loader payload at a static-friendly URL: SPA navigation on
|
|
77
|
+
// file-server deployments (GitHub Pages, S3, …) fetches this file instead
|
|
78
|
+
// of the runtime `/_furin/data?path=…` endpoint, which doesn't exist there.
|
|
79
|
+
writeFileSync(dataOutputFile, entry.ndjson);
|
|
73
80
|
renderedRoutes.push(urlPath);
|
|
74
|
-
console.log(`[furin] static: ${urlPath} → ${toPosixPath(
|
|
81
|
+
console.log(`[furin] static: ${urlPath} → ${toPosixPath(htmlOutputFile)}`);
|
|
75
82
|
} catch (err) {
|
|
76
83
|
console.error(
|
|
77
84
|
`[furin] static: prerender failed for "${route.pattern}" (params: ${JSON.stringify(params)}):`,
|
|
@@ -135,32 +142,67 @@ async function buildTaskQueue(
|
|
|
135
142
|
): Promise<Array<() => Promise<void>>> {
|
|
136
143
|
const tasks: Array<() => Promise<void>> = [];
|
|
137
144
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
145
|
+
type StaticParamsResult =
|
|
146
|
+
| { paramSets: Record<string, string>[] | null; route: ResolvedRoute }
|
|
147
|
+
| { error: unknown; paramSets: null; route: ResolvedRoute };
|
|
148
|
+
|
|
149
|
+
const staticParamsResults: StaticParamsResult[] = await mapWithConcurrency(
|
|
150
|
+
ssgRoutes,
|
|
151
|
+
STATIC_CONCURRENCY,
|
|
152
|
+
async (route) => {
|
|
153
|
+
if (!DYNAMIC_SEGMENT_RE.test(route.pattern)) {
|
|
154
|
+
return { route, paramSets: [{}] };
|
|
155
|
+
}
|
|
156
|
+
if (!route.page.staticParams) {
|
|
157
|
+
console.warn(
|
|
158
|
+
`[furin] static: skipping dynamic route "${route.pattern}" — no staticParams() defined.`
|
|
159
|
+
);
|
|
160
|
+
skippedRoutes.push(route.pattern);
|
|
161
|
+
return { route, paramSets: null };
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const paramSets = (await route.page.staticParams()) ?? [];
|
|
165
|
+
return { route, paramSets };
|
|
166
|
+
} catch (err) {
|
|
167
|
+
return { error: err, paramSets: null, route };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
for (const result of staticParamsResults) {
|
|
173
|
+
if ("error" in result) {
|
|
174
|
+
console.error(
|
|
175
|
+
`[furin] static: staticParams() failed for "${result.route.pattern}":`,
|
|
176
|
+
result.error
|
|
142
177
|
);
|
|
178
|
+
skippedRoutes.push(result.route.pattern);
|
|
143
179
|
continue;
|
|
144
180
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
181
|
+
if (result.paramSets === null) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// Defend against staticParams() returning a non-array (e.g. a bare object
|
|
185
|
+
// or `null`-after-coercion). Without this guard, a single malformed route
|
|
186
|
+
// would throw out of `for...of` and abort the entire build.
|
|
187
|
+
if (!Array.isArray(result.paramSets)) {
|
|
188
|
+
console.error(
|
|
189
|
+
`[furin] static: staticParams() for "${result.route.pattern}" returned a non-array value; skipping route.`
|
|
149
190
|
);
|
|
150
|
-
skippedRoutes.push(route.pattern);
|
|
191
|
+
skippedRoutes.push(result.route.pattern);
|
|
151
192
|
continue;
|
|
152
193
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
194
|
+
for (const params of result.paramSets) {
|
|
195
|
+
tasks.push(() =>
|
|
196
|
+
prerenderAndWrite(
|
|
197
|
+
result.route,
|
|
198
|
+
params,
|
|
199
|
+
root,
|
|
200
|
+
outDir,
|
|
201
|
+
renderedRoutes,
|
|
202
|
+
skippedRoutes,
|
|
203
|
+
basePath
|
|
204
|
+
)
|
|
205
|
+
);
|
|
164
206
|
}
|
|
165
207
|
}
|
|
166
208
|
|
|
@@ -268,6 +310,7 @@ export async function buildStaticTarget(
|
|
|
268
310
|
plugins: options.plugins,
|
|
269
311
|
publicPath,
|
|
270
312
|
basePath,
|
|
313
|
+
clientLogging: Boolean(options.clientLogging),
|
|
271
314
|
});
|
|
272
315
|
|
|
273
316
|
// ── 4. Generate HTML shell template ──────────────────────────────────────
|
|
@@ -275,7 +318,7 @@ export async function buildStaticTarget(
|
|
|
275
318
|
// This ensures the browser finds it even when the site is served from a sub-path.
|
|
276
319
|
const publicFavicon = join(rootDir, "public", "favicon.ico");
|
|
277
320
|
const faviconHref = existsSync(publicFavicon) ? `${basePath}/favicon.ico` : undefined;
|
|
278
|
-
const shellHtml = generateProdIndexHtml(entryChunk, cssChunks, undefined, faviconHref);
|
|
321
|
+
const shellHtml = generateProdIndexHtml(entryChunk, cssChunks, undefined, faviconHref, true);
|
|
279
322
|
|
|
280
323
|
// ── 5. Prime the renderer with the production shell ──────────────────────
|
|
281
324
|
// Must be called before any prerenderSSG invocation.
|
package/src/build/client.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { basename, join } from "node:path";
|
|
3
3
|
import { transformForClient } from "../plugin/transform-client";
|
|
4
|
-
import type { ResolvedRoute } from "../router";
|
|
4
|
+
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
5
5
|
import { generateHydrateEntry } from "./hydrate";
|
|
6
6
|
import { CLIENT_MODULE_PATH, LINK_MODULE_PATH } from "./shared";
|
|
7
7
|
import type { BuildClientOptions, BunBuildAliasConfig } from "./types";
|
|
8
8
|
|
|
9
9
|
const TS_FILE_FILTER = /\.(tsx|ts)$/;
|
|
10
|
-
const REACT_IMPORT_RE = /import\s+React\b/;
|
|
11
10
|
|
|
12
11
|
export interface BuildClientResult {
|
|
13
12
|
/** Public paths of all CSS chunks, e.g. `["/_client/chunk-abc.css"]` */
|
|
@@ -34,7 +33,7 @@ export interface BuildClientResult {
|
|
|
34
33
|
*/
|
|
35
34
|
export async function buildClient(
|
|
36
35
|
routes: ResolvedRoute[],
|
|
37
|
-
{ outDir, rootLayout, plugins, publicPath, basePath }: BuildClientOptions
|
|
36
|
+
{ outDir, rootLayout, plugins, publicPath, basePath, clientLogging }: BuildClientOptions
|
|
38
37
|
): Promise<BuildClientResult> {
|
|
39
38
|
const clientDir = join(outDir, "client");
|
|
40
39
|
|
|
@@ -45,7 +44,7 @@ export async function buildClient(
|
|
|
45
44
|
mkdirSync(clientDir, { recursive: true });
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
const hydrateCode = generateHydrateEntry(routes, rootLayout, basePath);
|
|
47
|
+
const hydrateCode = generateHydrateEntry(routes, rootLayout, basePath, clientLogging);
|
|
49
48
|
const hydratePath = join(outDir, "_hydrate.tsx");
|
|
50
49
|
writeFileSync(hydratePath, hydrateCode);
|
|
51
50
|
|
|
@@ -63,13 +62,10 @@ export async function buildClient(
|
|
|
63
62
|
const code = await Bun.file(path).text();
|
|
64
63
|
try {
|
|
65
64
|
const result = transformForClient(code, path);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
transformed = transformed
|
|
65
|
+
// transformForClient now emits TS/TSX directly (no pre-transpile),
|
|
66
|
+
// so JSX → React handling is delegated to Bun.build's loader, which
|
|
67
|
+
// applies the project tsconfig's automatic runtime by default.
|
|
68
|
+
const transformed = result.code
|
|
73
69
|
.replaceAll(`"@teyik0/furin/client"`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
74
70
|
.replaceAll(`'furin/client'`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
75
71
|
.replaceAll(`"@teyik0/furin/link"`, JSON.stringify(LINK_MODULE_PATH))
|
|
@@ -98,7 +94,7 @@ export async function buildClient(
|
|
|
98
94
|
format: "esm",
|
|
99
95
|
splitting: true,
|
|
100
96
|
minify: true,
|
|
101
|
-
sourcemap: "
|
|
97
|
+
sourcemap: "none",
|
|
102
98
|
// Hash the entry point name so it gets immutable caching like chunks.
|
|
103
99
|
// Without this, _hydrate.js keeps the same name across builds and browsers
|
|
104
100
|
// serve stale versions that reference old chunk hashes → dynamic import 404.
|
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
import { existsSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join, relative } from "node:path";
|
|
3
|
-
import { buildEntrySource } from "./entry-template";
|
|
3
|
+
import { buildEntrySource, type BuildEntryOptions } from "./entry-template";
|
|
4
4
|
import { collectFilesRecursive, ensureDir, toPosixPath } from "./shared";
|
|
5
5
|
|
|
6
|
-
export interface CompileEntryOptions {
|
|
7
|
-
buildId?: string;
|
|
8
|
-
embed?: { clientDir: string };
|
|
9
|
-
outDir: string;
|
|
10
|
-
publicDir?: string;
|
|
11
|
-
rootPath: string;
|
|
12
|
-
routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
|
|
13
|
-
serverEntry: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
6
|
/**
|
|
17
7
|
* Generates a single `_compile-entry.ts` that:
|
|
18
8
|
* 1. Statically imports every page module so Bun bundles them into the binary
|
|
@@ -20,8 +10,19 @@ export interface CompileEntryOptions {
|
|
|
20
10
|
* 3. Sets production mode and registers everything in a single CompileContext
|
|
21
11
|
* 4. Dynamically imports server.ts to boot the app
|
|
22
12
|
*/
|
|
23
|
-
export function generateCompileEntry(options:
|
|
24
|
-
const {
|
|
13
|
+
export function generateCompileEntry(options: BuildEntryOptions): string {
|
|
14
|
+
const {
|
|
15
|
+
buildId,
|
|
16
|
+
outDir,
|
|
17
|
+
rootPath,
|
|
18
|
+
routes,
|
|
19
|
+
serverEntry,
|
|
20
|
+
embed,
|
|
21
|
+
publicDir,
|
|
22
|
+
rootConventions,
|
|
23
|
+
routeMetadata,
|
|
24
|
+
ssgCache,
|
|
25
|
+
} = options;
|
|
25
26
|
ensureDir(outDir);
|
|
26
27
|
|
|
27
28
|
// Build embedded asset imports if embed mode
|
|
@@ -33,7 +34,9 @@ export function generateCompileEntry(options: CompileEntryOptions): string {
|
|
|
33
34
|
`[furin] Client directory not found: ${embed.clientDir}. Run the client build first.`
|
|
34
35
|
);
|
|
35
36
|
}
|
|
36
|
-
const clientFiles = collectFilesRecursive(embed.clientDir)
|
|
37
|
+
const clientFiles = collectFilesRecursive(embed.clientDir).filter(
|
|
38
|
+
(file) => !file.endsWith(".map")
|
|
39
|
+
);
|
|
37
40
|
const assetEntries: string[] = [];
|
|
38
41
|
let templateVarName: string | null = null;
|
|
39
42
|
|
|
@@ -95,6 +98,9 @@ export function generateCompileEntry(options: CompileEntryOptions): string {
|
|
|
95
98
|
serverEntry,
|
|
96
99
|
extraImports: assetImports,
|
|
97
100
|
extraContext: embeddedBlock,
|
|
101
|
+
rootConventions,
|
|
102
|
+
routeMetadata,
|
|
103
|
+
ssgCache,
|
|
98
104
|
});
|
|
99
105
|
|
|
100
106
|
const entryPath = join(outDir, "_compile-entry.ts");
|