@teyik0/furin 0.2.0-alpha.4 → 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/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 +88 -83
- package/dist/build/entry-template.d.ts +35 -9
- package/dist/build/entry-template.ts +106 -56
- 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 +5 -11
- 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/config.d.ts +11 -2
- package/dist/config.js +5 -1
- package/dist/furin.d.ts +36 -18
- package/dist/furin.js +630 -310
- 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/config.d.ts +11 -0
- package/dist/server/sync/stream.d.ts +8 -1
- package/dist/shared/prefix.d.ts +15 -0
- package/package.json +6 -1
- package/src/adapter/bun.ts +113 -50
- package/src/adapter/package.ts +206 -0
- package/src/build/client.ts +9 -3
- package/src/build/compile-entry.ts +88 -83
- package/src/build/entry-template.ts +106 -56
- package/src/build/hydrate.ts +4 -2
- package/src/build/index.ts +99 -28
- package/src/build/scan-server.ts +23 -7
- package/src/build/server-routes-entry.ts +5 -11
- package/src/build/ssg-cache.ts +6 -2
- package/src/build/types.ts +37 -1
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +21 -3
- package/src/config.ts +14 -1
- package/src/furin.ts +297 -143
- package/src/server/auto-invalidate/registry.ts +23 -1
- package/src/server/auto-invalidate/runtime.ts +29 -4
- 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/instance.ts +252 -0
- package/src/server/internal.ts +100 -5
- package/src/server/render/not-found.ts +9 -3
- package/src/server/render/ssr.ts +16 -4
- package/src/server/render/template.ts +57 -24
- package/src/server/sync/config.ts +32 -1
- package/src/server/sync/plugin.ts +4 -5
- package/src/server/sync/stream.ts +19 -9
- package/src/shared/prefix.ts +38 -0
package/src/adapter/bun.ts
CHANGED
|
@@ -3,14 +3,17 @@ import { dirname, join, resolve } from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { buildClient } from "../build/client.ts";
|
|
5
5
|
import { generateCompileEntry } from "../build/compile-entry.ts";
|
|
6
|
+
import type { BuildEntryOptions, EntryAppContext } from "../build/entry-template.ts";
|
|
6
7
|
import { generateServerRoutesEntry } from "../build/server-routes-entry.ts";
|
|
7
8
|
import { buildTargetManifest, copyDirRecursive, ensureDir, toPosixPath } from "../build/shared.ts";
|
|
8
9
|
import { buildSSGCacheSnapshot } from "../build/ssg-cache.ts";
|
|
9
10
|
import type { BuildAppOptions, TargetBuildManifest } from "../build/types.ts";
|
|
10
11
|
import type { BuildTarget } from "../config.ts";
|
|
12
|
+
import { ssgRouteCache } from "../server/cache/ssg.ts";
|
|
11
13
|
import { generateProdIndexHtml } from "../server/render/shell.ts";
|
|
12
14
|
import { setProductionTemplateContent } from "../server/render/template.ts";
|
|
13
15
|
import type { ResolvedRoute, RootLayout } from "../server/router/index.ts";
|
|
16
|
+
import { clientDirNameForPrefix } from "../shared/prefix.ts";
|
|
14
17
|
|
|
15
18
|
// import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
|
|
16
19
|
// through package exports, and is the Web-standard API. The main entry is
|
|
@@ -35,7 +38,15 @@ const BUILD_ID_INPUT_PATHS = [
|
|
|
35
38
|
`${_pkgSrcDir}/server/router/index${_ext}`,
|
|
36
39
|
];
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Deterministic build-ID input covering everything that can change rendered
|
|
43
|
+
* output: client chunks, route shape, route/root/error/not-found source
|
|
44
|
+
* contents and the framework's own render pipeline sources. Shared with the
|
|
45
|
+
* package target so packaged apps get the same stale-deploy detection —
|
|
46
|
+
* SSR-only changes (loader/page code that never reaches the client bundle)
|
|
47
|
+
* must still produce a new build ID.
|
|
48
|
+
*/
|
|
49
|
+
export async function createBuildFingerprint(
|
|
39
50
|
entryChunk: string,
|
|
40
51
|
cssChunks: string[],
|
|
41
52
|
routes: ResolvedRoute[],
|
|
@@ -99,8 +110,7 @@ function buildCompileMetadata(root: RootLayout, routes: ResolvedRoute[]) {
|
|
|
99
110
|
}
|
|
100
111
|
: undefined;
|
|
101
112
|
|
|
102
|
-
const routeMetadata: NonNullable<
|
|
103
|
-
{};
|
|
113
|
+
const routeMetadata: NonNullable<EntryAppContext["routeMetadata"]> = {};
|
|
104
114
|
for (const route of routes) {
|
|
105
115
|
routeMetadata[toPosixPath(route.path)] = {
|
|
106
116
|
segmentBoundaries: route.segmentBoundaries.map((b) => ({
|
|
@@ -115,82 +125,135 @@ function buildCompileMetadata(root: RootLayout, routes: ResolvedRoute[]) {
|
|
|
115
125
|
return { rootConventions, routeMetadata };
|
|
116
126
|
}
|
|
117
127
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
root: RootLayout
|
|
128
|
+
/** One mounted app's build input (root + routes scanned from its pagesDir). */
|
|
129
|
+
export interface BunTargetApp {
|
|
130
|
+
pagesDir: string;
|
|
131
|
+
prefix: string;
|
|
132
|
+
root: RootLayout;
|
|
133
|
+
routes: ResolvedRoute[];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Builds one app's client bundle + compile context payload for the entry. */
|
|
137
|
+
async function buildOneApp(
|
|
138
|
+
app: BunTargetApp,
|
|
139
|
+
targetDir: string,
|
|
123
140
|
serverEntry: string | null,
|
|
124
141
|
options: BuildAppOptions
|
|
125
|
-
): Promise<
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const target = "bun" satisfies BuildTarget;
|
|
134
|
-
const targetManifest = buildTargetManifest(rootDir, buildRoot, target, serverEntry);
|
|
135
|
-
const targetDir = resolve(rootDir, targetManifest.targetDir);
|
|
136
|
-
|
|
137
|
-
rmSync(targetDir, { force: true, recursive: true });
|
|
138
|
-
ensureDir(targetDir);
|
|
142
|
+
): Promise<{
|
|
143
|
+
buildId: string;
|
|
144
|
+
entryApp: BuildEntryOptions["apps"][number];
|
|
145
|
+
hydrateIntermediate: string;
|
|
146
|
+
}> {
|
|
147
|
+
const { prefix, root, routes } = app;
|
|
148
|
+
const clientDirName = clientDirNameForPrefix(prefix);
|
|
149
|
+
const label = prefix === "" ? "root app" : `app "${prefix}"`;
|
|
139
150
|
|
|
140
151
|
const { entryChunk, cssChunks } = await buildClient(routes, {
|
|
141
152
|
outDir: targetDir,
|
|
142
153
|
rootLayout: root.path,
|
|
143
154
|
plugins: options.plugins,
|
|
144
|
-
publicPath:
|
|
145
|
-
basePath:
|
|
155
|
+
publicPath: `${prefix}/_client/`,
|
|
156
|
+
basePath: prefix,
|
|
146
157
|
clientLogging: options.clientLogging ?? false,
|
|
158
|
+
clientDirName,
|
|
147
159
|
});
|
|
148
160
|
|
|
149
161
|
const buildFingerprint = await createBuildFingerprint(
|
|
150
|
-
entryChunk
|
|
162
|
+
`${prefix}\n${entryChunk}`,
|
|
151
163
|
cssChunks,
|
|
152
164
|
routes,
|
|
153
165
|
root,
|
|
154
166
|
serverEntry
|
|
155
167
|
);
|
|
156
168
|
const buildId = Bun.hash(buildFingerprint).toString(16).slice(0, 12);
|
|
157
|
-
targetManifest.buildId = buildId;
|
|
158
169
|
|
|
159
170
|
// Write index.html with the buildId meta tag injected so the client can
|
|
160
171
|
// detect stale deploys via X-Furin-Build-ID header comparison.
|
|
161
|
-
const clientDir = join(targetDir,
|
|
172
|
+
const clientDir = join(targetDir, clientDirName);
|
|
162
173
|
const indexHtml = generateProdIndexHtml(entryChunk, cssChunks, buildId, undefined, false);
|
|
163
174
|
writeFileSync(join(clientDir, "index.html"), indexHtml);
|
|
164
|
-
setProductionTemplateContent(indexHtml);
|
|
165
175
|
|
|
166
|
-
|
|
176
|
+
// The SSG snapshot renders through the (build-time) default state bucket:
|
|
177
|
+
// install this app's template, then clear the bucket's html caches so the
|
|
178
|
+
// previous app's prerenders can never leak into this snapshot. The mount
|
|
179
|
+
// prefix is passed explicitly — no instance scope exists at build time.
|
|
180
|
+
setProductionTemplateContent(indexHtml);
|
|
181
|
+
ssgRouteCache().clear();
|
|
167
182
|
const ssgCache = serverEntry
|
|
168
|
-
? await buildSSGCacheSnapshot(routes, root, "http://localhost")
|
|
183
|
+
? await buildSSGCacheSnapshot(routes, root, "http://localhost", prefix)
|
|
169
184
|
: undefined;
|
|
185
|
+
|
|
186
|
+
const { rootConventions, routeMetadata } = buildCompileMetadata(root, routes);
|
|
187
|
+
console.log(`[furin] Built ${label} (buildId ${buildId})`);
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
buildId,
|
|
191
|
+
entryApp: {
|
|
192
|
+
buildId,
|
|
193
|
+
prefix,
|
|
194
|
+
rootPath: root.path,
|
|
195
|
+
routes: routes.map((r) => ({ pattern: r.pattern, path: r.path, mode: r.mode })),
|
|
196
|
+
rootConventions,
|
|
197
|
+
routeMetadata,
|
|
198
|
+
ssgCache,
|
|
199
|
+
embed: options.compile === "embed" ? { clientDir } : undefined,
|
|
200
|
+
},
|
|
201
|
+
hydrateIntermediate:
|
|
202
|
+
clientDirName === "client" ? "_hydrate.tsx" : `_hydrate-${clientDirName}.tsx`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function buildBunTarget(
|
|
207
|
+
apps: BunTargetApp[],
|
|
208
|
+
rootDir: string,
|
|
209
|
+
buildRoot: string,
|
|
210
|
+
serverEntry: string | null,
|
|
211
|
+
options: BuildAppOptions
|
|
212
|
+
): Promise<TargetBuildManifest> {
|
|
213
|
+
if (options.compile && !serverEntry) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`[furin] \`compile: "${options.compile}"\` requires a server entry point. ` +
|
|
216
|
+
"Create src/server.ts or set `serverEntry` in your furin.config.ts."
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
if (apps.length === 0) {
|
|
220
|
+
throw new Error("[furin] buildBunTarget requires at least one app.");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const target = "bun" satisfies BuildTarget;
|
|
224
|
+
const targetManifest = buildTargetManifest(rootDir, buildRoot, target, serverEntry);
|
|
225
|
+
const targetDir = resolve(rootDir, targetManifest.targetDir);
|
|
226
|
+
|
|
227
|
+
rmSync(targetDir, { force: true, recursive: true });
|
|
228
|
+
ensureDir(targetDir);
|
|
229
|
+
|
|
170
230
|
const publicDir = existsSync(join(rootDir, "public")) ? join(rootDir, "public") : undefined;
|
|
171
|
-
const
|
|
231
|
+
const entryApps: BuildEntryOptions["apps"] = [];
|
|
232
|
+
const hydrateIntermediates: string[] = [];
|
|
172
233
|
|
|
234
|
+
for (const app of apps) {
|
|
235
|
+
const built = await buildOneApp(app, targetDir, serverEntry, options);
|
|
236
|
+
entryApps.push(built.entryApp);
|
|
237
|
+
hydrateIntermediates.push(built.hydrateIntermediate);
|
|
238
|
+
// The ROOT app's buildId is the manifest's headline id (back-compat).
|
|
239
|
+
if (app.prefix === "" || targetManifest.buildId === "") {
|
|
240
|
+
targetManifest.buildId = built.buildId;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const targetPublicDir = publicDir ? join(targetDir, "public") : undefined;
|
|
173
245
|
if (publicDir && targetPublicDir && options.compile !== "embed") {
|
|
174
246
|
copyDirRecursive(publicDir, targetPublicDir);
|
|
175
247
|
}
|
|
176
248
|
|
|
177
|
-
const { rootConventions, routeMetadata } = buildCompileMetadata(root, routes);
|
|
178
|
-
|
|
179
249
|
if (options.compile && serverEntry) {
|
|
180
|
-
const clientDir = join(targetDir, "client");
|
|
181
250
|
const outfile = join(targetDir, "server");
|
|
182
251
|
|
|
183
252
|
const entryPath = generateCompileEntry({
|
|
184
|
-
|
|
185
|
-
rootPath: root.path,
|
|
186
|
-
routes: routeManifest,
|
|
253
|
+
apps: entryApps,
|
|
187
254
|
serverEntry,
|
|
188
255
|
outDir: targetDir,
|
|
189
|
-
embed: options.compile === "embed" ? { clientDir } : undefined,
|
|
190
256
|
publicDir,
|
|
191
|
-
rootConventions,
|
|
192
|
-
routeMetadata,
|
|
193
|
-
ssgCache,
|
|
194
257
|
});
|
|
195
258
|
|
|
196
259
|
await Bun.build({
|
|
@@ -206,23 +269,23 @@ export async function buildBunTarget(
|
|
|
206
269
|
|
|
207
270
|
targetManifest.serverPath = toPosixPath(join(targetManifest.targetDir, "server"));
|
|
208
271
|
|
|
209
|
-
// Embed mode: assets are in the binary — clean up client
|
|
272
|
+
// Embed mode: assets are in the binary — clean up client dirs too.
|
|
210
273
|
if (options.compile === "embed") {
|
|
211
|
-
|
|
274
|
+
for (const app of apps) {
|
|
275
|
+
rmSync(join(targetDir, clientDirNameForPrefix(app.prefix)), {
|
|
276
|
+
force: true,
|
|
277
|
+
recursive: true,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
212
280
|
targetManifest.clientDir = null;
|
|
213
281
|
targetManifest.templatePath = null;
|
|
214
282
|
}
|
|
215
283
|
} else if (serverEntry) {
|
|
216
284
|
// Disk mode: generate server.ts then bundle it into self-contained server.js
|
|
217
285
|
const entryPath = generateServerRoutesEntry({
|
|
218
|
-
|
|
219
|
-
rootPath: root.path,
|
|
220
|
-
routes: routeManifest,
|
|
286
|
+
apps: entryApps,
|
|
221
287
|
serverEntry,
|
|
222
288
|
outDir: targetDir,
|
|
223
|
-
rootConventions,
|
|
224
|
-
routeMetadata,
|
|
225
|
-
ssgCache,
|
|
226
289
|
});
|
|
227
290
|
|
|
228
291
|
await Bun.build({
|
|
@@ -248,7 +311,7 @@ export async function buildBunTarget(
|
|
|
248
311
|
"_compile-entry.ts",
|
|
249
312
|
"_compile-entry.js.map",
|
|
250
313
|
"server.ts", // disk mode intermediate
|
|
251
|
-
|
|
314
|
+
...hydrateIntermediates,
|
|
252
315
|
]) {
|
|
253
316
|
rmSync(join(targetDir, file), { force: true });
|
|
254
317
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { existsSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, relative, resolve } from "node:path";
|
|
3
|
+
import { type BunTargetApp, createBuildFingerprint } from "../adapter/bun.ts";
|
|
4
|
+
import { buildClient } from "../build/client.ts";
|
|
5
|
+
import { buildEntrySource } from "../build/entry-template.ts";
|
|
6
|
+
import { copyDirRecursive, ensureDir, toPosixPath } from "../build/shared.ts";
|
|
7
|
+
import { buildSSGCacheSnapshot } from "../build/ssg-cache.ts";
|
|
8
|
+
import type { BuildAppOptions, PackageTargetBuildManifest } from "../build/types.ts";
|
|
9
|
+
import { ssgRouteCache } from "../server/cache/ssg.ts";
|
|
10
|
+
import { generateProdIndexHtml } from "../server/render/shell.ts";
|
|
11
|
+
import { setProductionTemplateContent } from "../server/render/template.ts";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `--target package` — builds a furin app as a PUBLISHABLE Elysia plugin
|
|
15
|
+
* instead of a bootable server. Output (`.furin/build/package/`):
|
|
16
|
+
*
|
|
17
|
+
* - `client/` content-hashed chunks + index.html SSR template
|
|
18
|
+
* - `register.js` side-effect module registering the app's CompileContext;
|
|
19
|
+
* page modules are bundled in, but everything from
|
|
20
|
+
* node_modules (incl. @teyik0/furin itself) stays external so
|
|
21
|
+
* the host and the register module share ONE furin runtime
|
|
22
|
+
* - `index.js` `createFurinApp()` factory the host `.use()`es
|
|
23
|
+
* - `index.d.ts` minimal typings for the factory
|
|
24
|
+
*
|
|
25
|
+
* The host composes it like any Elysia plugin:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { createFurinApp as admin } from "@org/admin/furin";
|
|
29
|
+
* new Elysia().use(await furin({...})).use(await admin()).listen(3000)
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* In a monorepo the baked relative `pagesDir` still points at real sources,
|
|
33
|
+
* so the same factory works under `bun --hot` (dev scans live). A published
|
|
34
|
+
* package without sources is production-only; its context is found by prefix.
|
|
35
|
+
*/
|
|
36
|
+
export async function buildPackageTarget(
|
|
37
|
+
app: BunTargetApp,
|
|
38
|
+
rootDir: string,
|
|
39
|
+
buildRoot: string,
|
|
40
|
+
options: BuildAppOptions
|
|
41
|
+
): Promise<PackageTargetBuildManifest> {
|
|
42
|
+
const { prefix, root, routes, pagesDir } = app;
|
|
43
|
+
const targetDir = join(buildRoot, "package");
|
|
44
|
+
|
|
45
|
+
rmSync(targetDir, { force: true, recursive: true });
|
|
46
|
+
ensureDir(targetDir);
|
|
47
|
+
|
|
48
|
+
const { entryChunk, cssChunks } = await buildClient(routes, {
|
|
49
|
+
outDir: targetDir,
|
|
50
|
+
rootLayout: root.path,
|
|
51
|
+
plugins: options.plugins,
|
|
52
|
+
publicPath: `${prefix}/_client/`,
|
|
53
|
+
basePath: prefix,
|
|
54
|
+
clientLogging: options.clientLogging ?? false,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Same fingerprint as the Bun target: hashing only the client chunks would
|
|
58
|
+
// keep the old build ID across SSR-only changes (page/loader code that never
|
|
59
|
+
// reaches the client bundle), defeating stale-deploy detection. The package
|
|
60
|
+
// target has no server entry, hence `null`.
|
|
61
|
+
const buildFingerprint = await createBuildFingerprint(
|
|
62
|
+
`${prefix}\n${entryChunk}`,
|
|
63
|
+
cssChunks,
|
|
64
|
+
routes,
|
|
65
|
+
root,
|
|
66
|
+
null
|
|
67
|
+
);
|
|
68
|
+
const buildId = Bun.hash(buildFingerprint).toString(16).slice(0, 12);
|
|
69
|
+
|
|
70
|
+
const clientDir = join(targetDir, "client");
|
|
71
|
+
|
|
72
|
+
// At runtime furin() derives publicDir next to the baked clientDir
|
|
73
|
+
// (join(dirname(clientDir), "public")), so the project's public/ assets must
|
|
74
|
+
// ship inside the artifact or /public/* and /favicon.ico 404 in production.
|
|
75
|
+
const publicDir = join(rootDir, "public");
|
|
76
|
+
if (existsSync(publicDir)) {
|
|
77
|
+
copyDirRecursive(publicDir, join(targetDir, "public"));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const indexHtml = generateProdIndexHtml(entryChunk, cssChunks, buildId, undefined, false);
|
|
81
|
+
writeFileSync(join(clientDir, "index.html"), indexHtml);
|
|
82
|
+
|
|
83
|
+
// SSG snapshot renders through the build-time default state bucket. The
|
|
84
|
+
// mount prefix is passed explicitly — no instance scope exists at build time.
|
|
85
|
+
setProductionTemplateContent(indexHtml);
|
|
86
|
+
ssgRouteCache().clear();
|
|
87
|
+
const ssgCache = await buildSSGCacheSnapshot(routes, root, "http://localhost", prefix);
|
|
88
|
+
|
|
89
|
+
const rootConventions =
|
|
90
|
+
root.errorPath || root.notFoundPath
|
|
91
|
+
? {
|
|
92
|
+
errorPath: root.errorPath ? toPosixPath(root.errorPath) : undefined,
|
|
93
|
+
notFoundPath: root.notFoundPath ? toPosixPath(root.notFoundPath) : undefined,
|
|
94
|
+
}
|
|
95
|
+
: undefined;
|
|
96
|
+
const routeMetadata: Record<
|
|
97
|
+
string,
|
|
98
|
+
{
|
|
99
|
+
segmentBoundaries: Array<{
|
|
100
|
+
depth: number;
|
|
101
|
+
path: string;
|
|
102
|
+
errorPath?: string;
|
|
103
|
+
notFoundPath?: string;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
> = {};
|
|
107
|
+
for (const route of routes) {
|
|
108
|
+
routeMetadata[toPosixPath(route.path)] = {
|
|
109
|
+
segmentBoundaries: route.segmentBoundaries.map((b) => ({
|
|
110
|
+
depth: b.depth,
|
|
111
|
+
path: toPosixPath(b.path),
|
|
112
|
+
errorPath: b.errorPath ? toPosixPath(b.errorPath) : undefined,
|
|
113
|
+
notFoundPath: b.notFoundPath ? toPosixPath(b.notFoundPath) : undefined,
|
|
114
|
+
})),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 1. register.ts — context registration side-effect module.
|
|
119
|
+
const registerSource = buildEntrySource({
|
|
120
|
+
apps: [
|
|
121
|
+
{
|
|
122
|
+
buildId,
|
|
123
|
+
prefix,
|
|
124
|
+
rootPath: root.path,
|
|
125
|
+
routes: routes.map((r) => ({ pattern: r.pattern, path: r.path, mode: r.mode })),
|
|
126
|
+
rootConventions,
|
|
127
|
+
routeMetadata,
|
|
128
|
+
ssgCache,
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
headerComment: "// Auto-generated by `furin build --target package` — do not edit",
|
|
132
|
+
mode: "register",
|
|
133
|
+
});
|
|
134
|
+
const registerEntry = join(targetDir, "register.ts");
|
|
135
|
+
writeFileSync(registerEntry, registerSource);
|
|
136
|
+
|
|
137
|
+
const result = await Bun.build({
|
|
138
|
+
entrypoints: [registerEntry],
|
|
139
|
+
outdir: targetDir,
|
|
140
|
+
target: "bun",
|
|
141
|
+
// Keep EVERY dependency external (incl. @teyik0/furin): the host must
|
|
142
|
+
// share one copy of the furin runtime with this register module — only
|
|
143
|
+
// the package's own page sources get bundled in.
|
|
144
|
+
packages: "external",
|
|
145
|
+
naming: { entry: "[name].[ext]", chunk: "[name]-[hash].[ext]" },
|
|
146
|
+
sourcemap: "none",
|
|
147
|
+
minify: false,
|
|
148
|
+
plugins: options.plugins,
|
|
149
|
+
});
|
|
150
|
+
if (!result.success) {
|
|
151
|
+
throw new AggregateError(result.logs, "[furin] package register build failed");
|
|
152
|
+
}
|
|
153
|
+
rmSync(registerEntry, { force: true });
|
|
154
|
+
rmSync(join(targetDir, "_hydrate.tsx"), { force: true });
|
|
155
|
+
|
|
156
|
+
// 2. index.js — the factory the host mounts. The baked relative pagesDir
|
|
157
|
+
// keeps monorepo dev working; in a published package the compile context is
|
|
158
|
+
// resolved by prefix instead.
|
|
159
|
+
const relativePagesDir = toPosixPath(relative(targetDir, resolve(rootDir, pagesDir)));
|
|
160
|
+
const factorySource = `// Auto-generated by \`furin build --target package\` — do not edit
|
|
161
|
+
import { fileURLToPath } from "node:url";
|
|
162
|
+
import { furin } from "@teyik0/furin";
|
|
163
|
+
import "./register.js";
|
|
164
|
+
|
|
165
|
+
const PAGES_DIR = fileURLToPath(new URL(${JSON.stringify(`./${relativePagesDir}`)}, import.meta.url));
|
|
166
|
+
const CLIENT_DIR = fileURLToPath(new URL("./client", import.meta.url));
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Mounts this packaged furin app as an Elysia plugin.
|
|
170
|
+
*
|
|
171
|
+
* @param {object} [options] Extra furin() options (logger, sync, ...) —
|
|
172
|
+
* pagesDir/prefix/clientDir are baked into the package and cannot be overridden.
|
|
173
|
+
* @returns {Promise<import("elysia").Elysia>}
|
|
174
|
+
*/
|
|
175
|
+
export function createFurinApp(options = {}) {
|
|
176
|
+
return furin({
|
|
177
|
+
...options,
|
|
178
|
+
pagesDir: PAGES_DIR,
|
|
179
|
+
prefix: ${JSON.stringify(prefix)},
|
|
180
|
+
clientDir: CLIENT_DIR,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const prefix = ${JSON.stringify(prefix)};
|
|
185
|
+
`;
|
|
186
|
+
writeFileSync(join(targetDir, "index.js"), factorySource);
|
|
187
|
+
|
|
188
|
+
const dtsSource = `import type { Elysia } from "elysia";
|
|
189
|
+
import type { FurinOptions } from "@teyik0/furin";
|
|
190
|
+
|
|
191
|
+
/** Extra furin() options — pagesDir/prefix/clientDir are baked into the package and cannot be overridden. */
|
|
192
|
+
export type CreateFurinAppOptions = Omit<FurinOptions, "pagesDir" | "prefix" | "clientDir">;
|
|
193
|
+
export declare function createFurinApp(options?: CreateFurinAppOptions): Promise<Elysia>;
|
|
194
|
+
export declare const prefix: string;
|
|
195
|
+
`;
|
|
196
|
+
writeFileSync(join(targetDir, "index.d.ts"), dtsSource);
|
|
197
|
+
|
|
198
|
+
console.log(`[furin] Package artifact: ${toPosixPath(relative(rootDir, targetDir))}`);
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
buildId,
|
|
202
|
+
generatedAt: new Date().toISOString(),
|
|
203
|
+
prefix,
|
|
204
|
+
targetDir: toPosixPath(relative(rootDir, targetDir)),
|
|
205
|
+
};
|
|
206
|
+
}
|
package/src/build/client.ts
CHANGED
|
@@ -33,9 +33,12 @@ export interface BuildClientResult {
|
|
|
33
33
|
*/
|
|
34
34
|
export async function buildClient(
|
|
35
35
|
routes: ResolvedRoute[],
|
|
36
|
-
{ outDir, rootLayout, plugins, publicPath, basePath, clientLogging }: BuildClientOptions
|
|
36
|
+
{ outDir, rootLayout, plugins, publicPath, basePath, clientLogging, clientDirName }: BuildClientOptions
|
|
37
37
|
): Promise<BuildClientResult> {
|
|
38
|
-
|
|
38
|
+
// Per-app client dir so several mounted apps build side by side
|
|
39
|
+
// ("client", "client-admin", …) without clobbering each other.
|
|
40
|
+
const dirName = clientDirName ?? "client";
|
|
41
|
+
const clientDir = join(outDir, dirName);
|
|
39
42
|
|
|
40
43
|
if (!existsSync(outDir)) {
|
|
41
44
|
mkdirSync(outDir, { recursive: true });
|
|
@@ -45,7 +48,10 @@ export async function buildClient(
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
const hydrateCode = generateHydrateEntry(routes, rootLayout, basePath, clientLogging);
|
|
48
|
-
const hydratePath = join(
|
|
51
|
+
const hydratePath = join(
|
|
52
|
+
outDir,
|
|
53
|
+
dirName === "client" ? "_hydrate.tsx" : `_hydrate-${dirName}.tsx`
|
|
54
|
+
);
|
|
49
55
|
writeFileSync(hydratePath, hydrateCode);
|
|
50
56
|
|
|
51
57
|
console.log("[furin] Building production client bundle…");
|