@teyik0/furin 0.2.0-alpha.3 → 0.2.0-alpha.4
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/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/cli/index.js +0 -0
- 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/furin.d.ts +1 -1
- package/dist/furin.js +499 -215
- package/dist/server/sync/adapter.d.ts +62 -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 +16 -4
- package/package.json +1 -1
- package/src/adapter/README.md +13 -13
- package/src/build/client.ts +152 -152
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -340
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +32 -32
- package/src/build/types.ts +89 -89
- 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/furin.ts +465 -465
- package/src/server/auto-invalidate/runtime.ts +134 -134
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/ssr.ts +591 -585
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +29 -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 +200 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +152 -144
- package/src/shared/deferred-ndjson.ts +7 -0
package/src/build/client.ts
CHANGED
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { basename, join } from "node:path";
|
|
3
|
-
import { transformForClient } from "../plugin/transform-client";
|
|
4
|
-
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
5
|
-
import { generateHydrateEntry } from "./hydrate";
|
|
6
|
-
import { CLIENT_MODULE_PATH, LINK_MODULE_PATH, SEARCH_MODULE_PATH } from "./shared";
|
|
7
|
-
import type { BuildClientOptions, BunBuildAliasConfig } from "./types";
|
|
8
|
-
|
|
9
|
-
const TS_FILE_FILTER = /\.(tsx|ts)$/;
|
|
10
|
-
|
|
11
|
-
export interface BuildClientResult {
|
|
12
|
-
/** Public paths of all CSS chunks, e.g. `["/_client/chunk-abc.css"]` */
|
|
13
|
-
cssChunks: string[];
|
|
14
|
-
/** Public path of the JS entry chunk, e.g. `/_client/chunk-abc.js` */
|
|
15
|
-
entryChunk: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Builds the production client bundle via Bun.build() using _hydrate.tsx as
|
|
20
|
-
* the JS entrypoint (NOT an HTML entrypoint). Bun produces:
|
|
21
|
-
* <outDir>/client/chunk-*.js — code-split bundles
|
|
22
|
-
* <outDir>/client/chunk-*.css — extracted CSS (if imported)
|
|
23
|
-
*
|
|
24
|
-
* Returns the chunk paths so the caller can compute a `buildId` and write
|
|
25
|
-
* `index.html` with the correct meta tag. Using an HTML entrypoint with
|
|
26
|
-
* code-splitting causes a Bun bug where the output index.html references a
|
|
27
|
-
* leaf chunk instead of the actual entry chunk, preventing React from mounting
|
|
28
|
-
* in production.
|
|
29
|
-
*
|
|
30
|
-
* The output index.html is NOT served to browsers directly. The server reads
|
|
31
|
-
* it as an SSR template, injects the pre-rendered React HTML into
|
|
32
|
-
* <!--ssr-outlet-->, and sends the complete page.
|
|
33
|
-
*/
|
|
34
|
-
export async function buildClient(
|
|
35
|
-
routes: ResolvedRoute[],
|
|
36
|
-
{ outDir, rootLayout, plugins, publicPath, basePath, clientLogging }: BuildClientOptions
|
|
37
|
-
): Promise<BuildClientResult> {
|
|
38
|
-
const clientDir = join(outDir, "client");
|
|
39
|
-
|
|
40
|
-
if (!existsSync(outDir)) {
|
|
41
|
-
mkdirSync(outDir, { recursive: true });
|
|
42
|
-
}
|
|
43
|
-
if (!existsSync(clientDir)) {
|
|
44
|
-
mkdirSync(clientDir, { recursive: true });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const hydrateCode = generateHydrateEntry(routes, rootLayout, basePath, clientLogging);
|
|
48
|
-
const hydratePath = join(outDir, "_hydrate.tsx");
|
|
49
|
-
writeFileSync(hydratePath, hydrateCode);
|
|
50
|
-
|
|
51
|
-
console.log("[furin] Building production client bundle…");
|
|
52
|
-
|
|
53
|
-
const transformPlugin: Bun.BunPlugin = {
|
|
54
|
-
name: "furin-transform-client",
|
|
55
|
-
setup(build) {
|
|
56
|
-
build.onLoad({ filter: TS_FILE_FILTER }, async (args) => {
|
|
57
|
-
const { path } = args;
|
|
58
|
-
if (path.includes("node_modules")) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const code = await Bun.file(path).text();
|
|
63
|
-
try {
|
|
64
|
-
const result = transformForClient(code, path);
|
|
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
|
|
69
|
-
.replaceAll(`"@teyik0/furin/client"`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
70
|
-
.replaceAll(`'furin/client'`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
71
|
-
.replaceAll(`"@teyik0/furin/link"`, JSON.stringify(LINK_MODULE_PATH))
|
|
72
|
-
.replaceAll(`'furin/link'`, JSON.stringify(LINK_MODULE_PATH))
|
|
73
|
-
.replaceAll(`"@teyik0/furin/search"`, JSON.stringify(SEARCH_MODULE_PATH))
|
|
74
|
-
.replaceAll(`'furin/search'`, JSON.stringify(SEARCH_MODULE_PATH));
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
contents: transformed,
|
|
78
|
-
loader: path.endsWith(".tsx") ? "tsx" : "ts",
|
|
79
|
-
};
|
|
80
|
-
} catch (error) {
|
|
81
|
-
console.error(`[furin] Transform error for ${path}:`, error);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const clientBuildConfig: BunBuildAliasConfig = {
|
|
89
|
-
// Use the JS file as entrypoint — NOT an HTML file. Bun's HTML bundler
|
|
90
|
-
// with code-splitting incorrectly references a leaf chunk in the output
|
|
91
|
-
// index.html instead of the actual entry chunk, preventing React from
|
|
92
|
-
// mounting. We write index.html ourselves after the build.
|
|
93
|
-
entrypoints: [hydratePath],
|
|
94
|
-
outdir: clientDir,
|
|
95
|
-
target: "browser",
|
|
96
|
-
format: "esm",
|
|
97
|
-
splitting: true,
|
|
98
|
-
minify: true,
|
|
99
|
-
sourcemap: "none",
|
|
100
|
-
// Hash the entry point name so it gets immutable caching like chunks.
|
|
101
|
-
// Without this, _hydrate.js keeps the same name across builds and browsers
|
|
102
|
-
// serve stale versions that reference old chunk hashes → dynamic import 404.
|
|
103
|
-
naming: {
|
|
104
|
-
entry: "[dir]/[name]-[hash].[ext]",
|
|
105
|
-
chunk: "[name]-[hash].[ext]",
|
|
106
|
-
},
|
|
107
|
-
// Absolute public path so SSR template asset URLs resolve on any route.
|
|
108
|
-
// Overridable via the `publicPath` option (e.g. "/furin/_client/" for basePath builds).
|
|
109
|
-
publicPath,
|
|
110
|
-
// User plugins run before the internal transform so they pre-process files first
|
|
111
|
-
plugins: plugins ? [...plugins, transformPlugin] : [transformPlugin],
|
|
112
|
-
alias: {
|
|
113
|
-
"@teyik0/furin/client": CLIENT_MODULE_PATH,
|
|
114
|
-
"@teyik0/furin/link": LINK_MODULE_PATH,
|
|
115
|
-
"@teyik0/furin/search": SEARCH_MODULE_PATH,
|
|
116
|
-
},
|
|
117
|
-
define: {
|
|
118
|
-
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const result = await Bun.build(clientBuildConfig);
|
|
123
|
-
for (const output of result.outputs) {
|
|
124
|
-
console.log(`[furin] ${output.path} (${(output.size / 1024).toFixed(1)} KB)`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Build index.html (SSR template) with correct chunk references.
|
|
128
|
-
// We derive these from result.outputs rather than letting Bun write the HTML,
|
|
129
|
-
// because Bun's HTML entrypoint + code-splitting produces the wrong entry chunk.
|
|
130
|
-
const entryOutput = result.outputs.find((o) => o.kind === "entry-point");
|
|
131
|
-
const cssOutputs = result.outputs.filter(
|
|
132
|
-
(o) => o.path.endsWith(".css") && !o.path.endsWith(".css.map")
|
|
133
|
-
);
|
|
134
|
-
if (!entryOutput) {
|
|
135
|
-
throw new Error("[furin] client build did not emit entry chunk");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Normalise: ensure publicPath ends with exactly one "/".
|
|
139
|
-
// Preserve an empty publicPath ("") as-is so callers that want
|
|
140
|
-
// relative chunk URLs don't get an unintended root-absolute "/".
|
|
141
|
-
let publicPrefix: string;
|
|
142
|
-
if (publicPath === "") {
|
|
143
|
-
publicPrefix = "";
|
|
144
|
-
} else {
|
|
145
|
-
publicPrefix = publicPath.endsWith("/") ? publicPath : `${publicPath}/`;
|
|
146
|
-
}
|
|
147
|
-
const entryChunk = `${publicPrefix}${basename(entryOutput.path)}`;
|
|
148
|
-
const cssChunks = cssOutputs.map((o) => `${publicPrefix}${basename(o.path)}`);
|
|
149
|
-
|
|
150
|
-
console.log("[furin] Production client build complete");
|
|
151
|
-
return { entryChunk, cssChunks };
|
|
152
|
-
}
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { transformForClient } from "../plugin/transform-client";
|
|
4
|
+
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
5
|
+
import { generateHydrateEntry } from "./hydrate";
|
|
6
|
+
import { CLIENT_MODULE_PATH, LINK_MODULE_PATH, SEARCH_MODULE_PATH } from "./shared";
|
|
7
|
+
import type { BuildClientOptions, BunBuildAliasConfig } from "./types";
|
|
8
|
+
|
|
9
|
+
const TS_FILE_FILTER = /\.(tsx|ts)$/;
|
|
10
|
+
|
|
11
|
+
export interface BuildClientResult {
|
|
12
|
+
/** Public paths of all CSS chunks, e.g. `["/_client/chunk-abc.css"]` */
|
|
13
|
+
cssChunks: string[];
|
|
14
|
+
/** Public path of the JS entry chunk, e.g. `/_client/chunk-abc.js` */
|
|
15
|
+
entryChunk: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Builds the production client bundle via Bun.build() using _hydrate.tsx as
|
|
20
|
+
* the JS entrypoint (NOT an HTML entrypoint). Bun produces:
|
|
21
|
+
* <outDir>/client/chunk-*.js — code-split bundles
|
|
22
|
+
* <outDir>/client/chunk-*.css — extracted CSS (if imported)
|
|
23
|
+
*
|
|
24
|
+
* Returns the chunk paths so the caller can compute a `buildId` and write
|
|
25
|
+
* `index.html` with the correct meta tag. Using an HTML entrypoint with
|
|
26
|
+
* code-splitting causes a Bun bug where the output index.html references a
|
|
27
|
+
* leaf chunk instead of the actual entry chunk, preventing React from mounting
|
|
28
|
+
* in production.
|
|
29
|
+
*
|
|
30
|
+
* The output index.html is NOT served to browsers directly. The server reads
|
|
31
|
+
* it as an SSR template, injects the pre-rendered React HTML into
|
|
32
|
+
* <!--ssr-outlet-->, and sends the complete page.
|
|
33
|
+
*/
|
|
34
|
+
export async function buildClient(
|
|
35
|
+
routes: ResolvedRoute[],
|
|
36
|
+
{ outDir, rootLayout, plugins, publicPath, basePath, clientLogging }: BuildClientOptions
|
|
37
|
+
): Promise<BuildClientResult> {
|
|
38
|
+
const clientDir = join(outDir, "client");
|
|
39
|
+
|
|
40
|
+
if (!existsSync(outDir)) {
|
|
41
|
+
mkdirSync(outDir, { recursive: true });
|
|
42
|
+
}
|
|
43
|
+
if (!existsSync(clientDir)) {
|
|
44
|
+
mkdirSync(clientDir, { recursive: true });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const hydrateCode = generateHydrateEntry(routes, rootLayout, basePath, clientLogging);
|
|
48
|
+
const hydratePath = join(outDir, "_hydrate.tsx");
|
|
49
|
+
writeFileSync(hydratePath, hydrateCode);
|
|
50
|
+
|
|
51
|
+
console.log("[furin] Building production client bundle…");
|
|
52
|
+
|
|
53
|
+
const transformPlugin: Bun.BunPlugin = {
|
|
54
|
+
name: "furin-transform-client",
|
|
55
|
+
setup(build) {
|
|
56
|
+
build.onLoad({ filter: TS_FILE_FILTER }, async (args) => {
|
|
57
|
+
const { path } = args;
|
|
58
|
+
if (path.includes("node_modules")) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const code = await Bun.file(path).text();
|
|
63
|
+
try {
|
|
64
|
+
const result = transformForClient(code, path);
|
|
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
|
|
69
|
+
.replaceAll(`"@teyik0/furin/client"`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
70
|
+
.replaceAll(`'furin/client'`, JSON.stringify(CLIENT_MODULE_PATH))
|
|
71
|
+
.replaceAll(`"@teyik0/furin/link"`, JSON.stringify(LINK_MODULE_PATH))
|
|
72
|
+
.replaceAll(`'furin/link'`, JSON.stringify(LINK_MODULE_PATH))
|
|
73
|
+
.replaceAll(`"@teyik0/furin/search"`, JSON.stringify(SEARCH_MODULE_PATH))
|
|
74
|
+
.replaceAll(`'furin/search'`, JSON.stringify(SEARCH_MODULE_PATH));
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
contents: transformed,
|
|
78
|
+
loader: path.endsWith(".tsx") ? "tsx" : "ts",
|
|
79
|
+
};
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(`[furin] Transform error for ${path}:`, error);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const clientBuildConfig: BunBuildAliasConfig = {
|
|
89
|
+
// Use the JS file as entrypoint — NOT an HTML file. Bun's HTML bundler
|
|
90
|
+
// with code-splitting incorrectly references a leaf chunk in the output
|
|
91
|
+
// index.html instead of the actual entry chunk, preventing React from
|
|
92
|
+
// mounting. We write index.html ourselves after the build.
|
|
93
|
+
entrypoints: [hydratePath],
|
|
94
|
+
outdir: clientDir,
|
|
95
|
+
target: "browser",
|
|
96
|
+
format: "esm",
|
|
97
|
+
splitting: true,
|
|
98
|
+
minify: true,
|
|
99
|
+
sourcemap: "none",
|
|
100
|
+
// Hash the entry point name so it gets immutable caching like chunks.
|
|
101
|
+
// Without this, _hydrate.js keeps the same name across builds and browsers
|
|
102
|
+
// serve stale versions that reference old chunk hashes → dynamic import 404.
|
|
103
|
+
naming: {
|
|
104
|
+
entry: "[dir]/[name]-[hash].[ext]",
|
|
105
|
+
chunk: "[name]-[hash].[ext]",
|
|
106
|
+
},
|
|
107
|
+
// Absolute public path so SSR template asset URLs resolve on any route.
|
|
108
|
+
// Overridable via the `publicPath` option (e.g. "/furin/_client/" for basePath builds).
|
|
109
|
+
publicPath,
|
|
110
|
+
// User plugins run before the internal transform so they pre-process files first
|
|
111
|
+
plugins: plugins ? [...plugins, transformPlugin] : [transformPlugin],
|
|
112
|
+
alias: {
|
|
113
|
+
"@teyik0/furin/client": CLIENT_MODULE_PATH,
|
|
114
|
+
"@teyik0/furin/link": LINK_MODULE_PATH,
|
|
115
|
+
"@teyik0/furin/search": SEARCH_MODULE_PATH,
|
|
116
|
+
},
|
|
117
|
+
define: {
|
|
118
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const result = await Bun.build(clientBuildConfig);
|
|
123
|
+
for (const output of result.outputs) {
|
|
124
|
+
console.log(`[furin] ${output.path} (${(output.size / 1024).toFixed(1)} KB)`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Build index.html (SSR template) with correct chunk references.
|
|
128
|
+
// We derive these from result.outputs rather than letting Bun write the HTML,
|
|
129
|
+
// because Bun's HTML entrypoint + code-splitting produces the wrong entry chunk.
|
|
130
|
+
const entryOutput = result.outputs.find((o) => o.kind === "entry-point");
|
|
131
|
+
const cssOutputs = result.outputs.filter(
|
|
132
|
+
(o) => o.path.endsWith(".css") && !o.path.endsWith(".css.map")
|
|
133
|
+
);
|
|
134
|
+
if (!entryOutput) {
|
|
135
|
+
throw new Error("[furin] client build did not emit entry chunk");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Normalise: ensure publicPath ends with exactly one "/".
|
|
139
|
+
// Preserve an empty publicPath ("") as-is so callers that want
|
|
140
|
+
// relative chunk URLs don't get an unintended root-absolute "/".
|
|
141
|
+
let publicPrefix: string;
|
|
142
|
+
if (publicPath === "") {
|
|
143
|
+
publicPrefix = "";
|
|
144
|
+
} else {
|
|
145
|
+
publicPrefix = publicPath.endsWith("/") ? publicPath : `${publicPath}/`;
|
|
146
|
+
}
|
|
147
|
+
const entryChunk = `${publicPrefix}${basename(entryOutput.path)}`;
|
|
148
|
+
const cssChunks = cssOutputs.map((o) => `${publicPrefix}${basename(o.path)}`);
|
|
149
|
+
|
|
150
|
+
console.log("[furin] Production client build complete");
|
|
151
|
+
return { entryChunk, cssChunks };
|
|
152
|
+
}
|
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import { existsSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
3
|
-
import { buildEntrySource, type BuildEntryOptions } from "./entry-template";
|
|
4
|
-
import { collectFilesRecursive, ensureDir, toPosixPath } from "./shared";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Generates a single `_compile-entry.ts` that:
|
|
8
|
-
* 1. Statically imports every page module so Bun bundles them into the binary
|
|
9
|
-
* 2. Optionally embeds client assets via `with { type: "file" }` (embed mode)
|
|
10
|
-
* 3. Sets production mode and registers everything in a single CompileContext
|
|
11
|
-
* 4. Dynamically imports server.ts to boot the app
|
|
12
|
-
*/
|
|
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;
|
|
26
|
-
ensureDir(outDir);
|
|
27
|
-
|
|
28
|
-
// Build embedded asset imports if embed mode
|
|
29
|
-
const assetImports: string[] = [];
|
|
30
|
-
let embeddedBlock: string[] = [];
|
|
31
|
-
if (embed) {
|
|
32
|
-
if (!existsSync(embed.clientDir)) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`[furin] Client directory not found: ${embed.clientDir}. Run the client build first.`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
const clientFiles = collectFilesRecursive(embed.clientDir).filter(
|
|
38
|
-
(file) => !file.endsWith(".map")
|
|
39
|
-
);
|
|
40
|
-
const assetEntries: string[] = [];
|
|
41
|
-
let templateVarName: string | null = null;
|
|
42
|
-
|
|
43
|
-
let assetIndex = 0;
|
|
44
|
-
for (const file of clientFiles) {
|
|
45
|
-
const varName = `_asset${assetIndex++}`;
|
|
46
|
-
const relPath = toPosixPath(relative(outDir, file));
|
|
47
|
-
const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
|
|
48
|
-
|
|
49
|
-
assetImports.push(
|
|
50
|
-
`import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const clientRelativePath = toPosixPath(relative(embed.clientDir, file));
|
|
54
|
-
if (clientRelativePath === "index.html") {
|
|
55
|
-
templateVarName = varName;
|
|
56
|
-
} else {
|
|
57
|
-
assetEntries.push(` ${JSON.stringify(`/_client/${clientRelativePath}`)}: ${varName},`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (templateVarName === null) {
|
|
62
|
-
throw new Error(
|
|
63
|
-
`[furin] Embed mode requires a client index.html at ${join(embed.clientDir, "index.html")}. Run the client build first.`
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (publicDir && existsSync(publicDir)) {
|
|
68
|
-
const publicFiles = collectFilesRecursive(publicDir);
|
|
69
|
-
for (const file of publicFiles) {
|
|
70
|
-
const varName = `_asset${assetIndex++}`;
|
|
71
|
-
const relPath = toPosixPath(relative(outDir, file));
|
|
72
|
-
const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
|
|
73
|
-
|
|
74
|
-
assetImports.push(
|
|
75
|
-
`import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
const publicRelativePath = toPosixPath(relative(publicDir, file));
|
|
79
|
-
assetEntries.push(` ${JSON.stringify(`/public/${publicRelativePath}`)}: ${varName},`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
embeddedBlock = [
|
|
84
|
-
" embedded: {",
|
|
85
|
-
` template: ${templateVarName},`,
|
|
86
|
-
" assets: {",
|
|
87
|
-
...assetEntries,
|
|
88
|
-
" },",
|
|
89
|
-
" },",
|
|
90
|
-
];
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const source = buildEntrySource({
|
|
94
|
-
buildId,
|
|
95
|
-
headerComment: "// Auto-generated by furin compile — do not edit",
|
|
96
|
-
rootPath,
|
|
97
|
-
routes,
|
|
98
|
-
serverEntry,
|
|
99
|
-
extraImports: assetImports,
|
|
100
|
-
extraContext: embeddedBlock,
|
|
101
|
-
rootConventions,
|
|
102
|
-
routeMetadata,
|
|
103
|
-
ssgCache,
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
const entryPath = join(outDir, "_compile-entry.ts");
|
|
107
|
-
writeFileSync(entryPath, source);
|
|
108
|
-
return entryPath;
|
|
109
|
-
}
|
|
1
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import { buildEntrySource, type BuildEntryOptions } from "./entry-template";
|
|
4
|
+
import { collectFilesRecursive, ensureDir, toPosixPath } from "./shared";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generates a single `_compile-entry.ts` that:
|
|
8
|
+
* 1. Statically imports every page module so Bun bundles them into the binary
|
|
9
|
+
* 2. Optionally embeds client assets via `with { type: "file" }` (embed mode)
|
|
10
|
+
* 3. Sets production mode and registers everything in a single CompileContext
|
|
11
|
+
* 4. Dynamically imports server.ts to boot the app
|
|
12
|
+
*/
|
|
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;
|
|
26
|
+
ensureDir(outDir);
|
|
27
|
+
|
|
28
|
+
// Build embedded asset imports if embed mode
|
|
29
|
+
const assetImports: string[] = [];
|
|
30
|
+
let embeddedBlock: string[] = [];
|
|
31
|
+
if (embed) {
|
|
32
|
+
if (!existsSync(embed.clientDir)) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`[furin] Client directory not found: ${embed.clientDir}. Run the client build first.`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const clientFiles = collectFilesRecursive(embed.clientDir).filter(
|
|
38
|
+
(file) => !file.endsWith(".map")
|
|
39
|
+
);
|
|
40
|
+
const assetEntries: string[] = [];
|
|
41
|
+
let templateVarName: string | null = null;
|
|
42
|
+
|
|
43
|
+
let assetIndex = 0;
|
|
44
|
+
for (const file of clientFiles) {
|
|
45
|
+
const varName = `_asset${assetIndex++}`;
|
|
46
|
+
const relPath = toPosixPath(relative(outDir, file));
|
|
47
|
+
const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
|
|
48
|
+
|
|
49
|
+
assetImports.push(
|
|
50
|
+
`import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const clientRelativePath = toPosixPath(relative(embed.clientDir, file));
|
|
54
|
+
if (clientRelativePath === "index.html") {
|
|
55
|
+
templateVarName = varName;
|
|
56
|
+
} else {
|
|
57
|
+
assetEntries.push(` ${JSON.stringify(`/_client/${clientRelativePath}`)}: ${varName},`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (templateVarName === null) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`[furin] Embed mode requires a client index.html at ${join(embed.clientDir, "index.html")}. Run the client build first.`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (publicDir && existsSync(publicDir)) {
|
|
68
|
+
const publicFiles = collectFilesRecursive(publicDir);
|
|
69
|
+
for (const file of publicFiles) {
|
|
70
|
+
const varName = `_asset${assetIndex++}`;
|
|
71
|
+
const relPath = toPosixPath(relative(outDir, file));
|
|
72
|
+
const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
|
|
73
|
+
|
|
74
|
+
assetImports.push(
|
|
75
|
+
`import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const publicRelativePath = toPosixPath(relative(publicDir, file));
|
|
79
|
+
assetEntries.push(` ${JSON.stringify(`/public/${publicRelativePath}`)}: ${varName},`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
embeddedBlock = [
|
|
84
|
+
" embedded: {",
|
|
85
|
+
` template: ${templateVarName},`,
|
|
86
|
+
" assets: {",
|
|
87
|
+
...assetEntries,
|
|
88
|
+
" },",
|
|
89
|
+
" },",
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const source = buildEntrySource({
|
|
94
|
+
buildId,
|
|
95
|
+
headerComment: "// Auto-generated by furin compile — do not edit",
|
|
96
|
+
rootPath,
|
|
97
|
+
routes,
|
|
98
|
+
serverEntry,
|
|
99
|
+
extraImports: assetImports,
|
|
100
|
+
extraContext: embeddedBlock,
|
|
101
|
+
rootConventions,
|
|
102
|
+
routeMetadata,
|
|
103
|
+
ssgCache,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const entryPath = join(outDir, "_compile-entry.ts");
|
|
107
|
+
writeFileSync(entryPath, source);
|
|
108
|
+
return entryPath;
|
|
109
|
+
}
|