@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.3
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 +33 -33
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/index.js +1724 -687
- package/dist/build/route-types.d.ts +3 -1
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/build/shared.d.ts +1 -0
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/cli/index.js +1673 -658
- package/dist/client/link.js +883 -89
- package/dist/client/router/index.d.ts +4 -0
- package/dist/client/router/link-utils.d.ts +3 -1
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/types.d.ts +14 -1
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +44 -0
- package/dist/furin.d.ts +9 -1
- package/dist/furin.js +1578 -258
- package/dist/server/auto-invalidate/runtime.d.ts +1 -1
- package/dist/server/dev-page-plugin.d.ts +3 -0
- package/dist/server/render/assemble.d.ts +2 -0
- package/dist/server/render/isr.d.ts +2 -1
- package/dist/server/render/ssg.d.ts +3 -2
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/router/hmr.d.ts +4 -3
- package/dist/server/router/index.d.ts +1 -1
- package/dist/server/router/index.js +1 -2
- package/dist/server/router/index.ts +1 -2
- package/dist/server/router/plugin.d.ts +2 -1
- package/dist/server/router/schemas.d.ts +25 -17
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +369 -0
- package/dist/server/sync/stream.d.ts +47 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/package.json +10 -5
- package/src/adapter/README.md +13 -13
- package/src/adapter/static.ts +23 -6
- package/src/build/client.ts +152 -149
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -331
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -115
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -78
- package/src/build/ssg-cache.ts +32 -31
- package/src/build/types.ts +89 -89
- package/src/client/link.tsx +13 -4
- package/src/client/router/context.ts +2 -0
- package/src/client/router/index.ts +9 -0
- package/src/client/router/link-utils.ts +184 -180
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +804 -723
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/types.ts +276 -263
- package/src/client/sync.ts +98 -0
- package/src/client.ts +369 -355
- package/src/furin.ts +465 -437
- package/src/server/auto-invalidate/runtime.ts +134 -133
- package/src/server/dev-page-plugin.ts +454 -449
- package/src/server/render/assemble.ts +160 -133
- package/src/server/render/isr.ts +323 -295
- package/src/server/render/not-found.ts +2 -0
- package/src/server/render/ssg.ts +15 -4
- package/src/server/render/ssr.ts +585 -545
- package/src/server/router/hmr.ts +14 -10
- package/src/server/router/index.ts +1 -2
- package/src/server/router/patterns.ts +5 -0
- package/src/server/router/plugin.ts +384 -401
- package/src/server/router/schemas.ts +172 -57
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/index.ts +14 -0
- package/src/server/sync/plugin.ts +108 -0
- package/src/server/sync/stream.ts +144 -0
- package/src/shared/search-params.ts +182 -0
|
@@ -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
|
+
}
|
|
@@ -1,156 +1,156 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import type { SsgCacheEntry } from "../server/cache/index.ts";
|
|
5
|
-
|
|
6
|
-
// import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
|
|
7
|
-
// through package exports, and is the Web-standard API. The main entry is
|
|
8
|
-
// `src/furin.ts` (or `dist/furin.js`), so we strip two path segments to reach
|
|
9
|
-
// the package root.
|
|
10
|
-
const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
|
|
11
|
-
const _pkgSrcDirRaw = existsSync(join(_pkgRoot, "src", "furin.ts"))
|
|
12
|
-
? join(_pkgRoot, "src")
|
|
13
|
-
: join(_pkgRoot, "dist");
|
|
14
|
-
// Normalize to forward slashes so endsWith checks and template paths work on Windows.
|
|
15
|
-
const _pkgSrcDir = _pkgSrcDirRaw.replace(/\\/g, "/");
|
|
16
|
-
const _ext = _pkgSrcDir.endsWith("/src") ? ".ts" : ".js";
|
|
17
|
-
const INTERNAL_MODULE_PATH = `${_pkgSrcDir}/server/internal${_ext}`;
|
|
18
|
-
const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/server/runtime-env${_ext}`;
|
|
19
|
-
|
|
20
|
-
export interface EntryTemplateOptions {
|
|
21
|
-
buildId?: string;
|
|
22
|
-
extraContext?: string[];
|
|
23
|
-
extraImports?: string[];
|
|
24
|
-
headerComment: string;
|
|
25
|
-
rootConventions?: { errorPath?: string; notFoundPath?: string };
|
|
26
|
-
rootPath: string;
|
|
27
|
-
routeMetadata?: Record<
|
|
28
|
-
string,
|
|
29
|
-
{
|
|
30
|
-
segmentBoundaries: Array<{
|
|
31
|
-
depth: number;
|
|
32
|
-
path: string;
|
|
33
|
-
errorPath?: string;
|
|
34
|
-
notFoundPath?: string;
|
|
35
|
-
}>;
|
|
36
|
-
}
|
|
37
|
-
>;
|
|
38
|
-
routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
|
|
39
|
-
serverEntry: string;
|
|
40
|
-
ssgCache?: Record<string, SsgCacheEntry>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Unified options for generating a build entry (compile or disk-based). */
|
|
44
|
-
export interface BuildEntryOptions extends Omit<EntryTemplateOptions, "headerComment"> {
|
|
45
|
-
outDir: string;
|
|
46
|
-
headerComment?: string;
|
|
47
|
-
/** Embed mode: bundles client assets into the binary via `with { type: "file" }`. */
|
|
48
|
-
embed?: { clientDir: string };
|
|
49
|
-
publicDir?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function collectConventionPaths(
|
|
53
|
-
rootConventions: EntryTemplateOptions["rootConventions"],
|
|
54
|
-
routeMetadata: EntryTemplateOptions["routeMetadata"]
|
|
55
|
-
): string[] {
|
|
56
|
-
const paths: string[] = [];
|
|
57
|
-
if (rootConventions?.errorPath) {
|
|
58
|
-
paths.push(rootConventions.errorPath);
|
|
59
|
-
}
|
|
60
|
-
if (rootConventions?.notFoundPath) {
|
|
61
|
-
paths.push(rootConventions.notFoundPath);
|
|
62
|
-
}
|
|
63
|
-
if (routeMetadata) {
|
|
64
|
-
for (const meta of Object.values(routeMetadata)) {
|
|
65
|
-
for (const seg of meta.segmentBoundaries) {
|
|
66
|
-
if (seg.errorPath) {
|
|
67
|
-
paths.push(seg.errorPath);
|
|
68
|
-
}
|
|
69
|
-
if (seg.notFoundPath) {
|
|
70
|
-
paths.push(seg.notFoundPath);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return [...new Set(paths)];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function buildEntrySource(options: EntryTemplateOptions): string {
|
|
79
|
-
const {
|
|
80
|
-
buildId,
|
|
81
|
-
headerComment,
|
|
82
|
-
rootPath,
|
|
83
|
-
routes,
|
|
84
|
-
serverEntry,
|
|
85
|
-
rootConventions,
|
|
86
|
-
routeMetadata,
|
|
87
|
-
ssgCache,
|
|
88
|
-
} = options;
|
|
89
|
-
let { extraImports, extraContext } = options;
|
|
90
|
-
if (extraImports === undefined) {
|
|
91
|
-
extraImports = [];
|
|
92
|
-
}
|
|
93
|
-
if (extraContext === undefined) {
|
|
94
|
-
extraContext = [];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const allModulePaths = [
|
|
98
|
-
rootPath,
|
|
99
|
-
...routes.map((r) => r.path),
|
|
100
|
-
...collectConventionPaths(rootConventions, routeMetadata),
|
|
101
|
-
];
|
|
102
|
-
const moduleImports: string[] = [];
|
|
103
|
-
const moduleEntries: string[] = [];
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < allModulePaths.length; i++) {
|
|
106
|
-
const absPath = (allModulePaths[i] as string).replace(/\\/g, "/");
|
|
107
|
-
const varName = `_mod${i}`;
|
|
108
|
-
moduleImports.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
|
|
109
|
-
moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const routeEntries = routes.map(
|
|
113
|
-
(r) =>
|
|
114
|
-
` { pattern: ${JSON.stringify(r.pattern)}, path: ${JSON.stringify(r.path.replace(/\\/g, "/"))}, mode: ${JSON.stringify(r.mode)} },`
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
const rootConventionsLine = rootConventions
|
|
118
|
-
? ` rootConventions: ${JSON.stringify(rootConventions)},`
|
|
119
|
-
: "";
|
|
120
|
-
const routeMetadataLine = routeMetadata
|
|
121
|
-
? ` routeMetadata: ${JSON.stringify(routeMetadata)},`
|
|
122
|
-
: "";
|
|
123
|
-
const ssgCacheLine = ssgCache ? ` ssgCache: ${JSON.stringify(ssgCache)},` : "";
|
|
124
|
-
|
|
125
|
-
const lines = [
|
|
126
|
-
headerComment,
|
|
127
|
-
`import { __setCompileContext } from ${JSON.stringify(INTERNAL_MODULE_PATH)};`,
|
|
128
|
-
`import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`,
|
|
129
|
-
...moduleImports,
|
|
130
|
-
...(extraImports.length > 0 ? ["", ...extraImports] : []),
|
|
131
|
-
"",
|
|
132
|
-
"// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
|
|
133
|
-
"__setDevMode(false);",
|
|
134
|
-
'process.env.NODE_ENV = "production";',
|
|
135
|
-
"",
|
|
136
|
-
"__setCompileContext({",
|
|
137
|
-
` buildId: ${JSON.stringify(buildId ?? "")},`,
|
|
138
|
-
` rootPath: ${JSON.stringify(rootPath.replace(/\\/g, "/"))},`,
|
|
139
|
-
rootConventionsLine,
|
|
140
|
-
" modules: {",
|
|
141
|
-
...moduleEntries,
|
|
142
|
-
" },",
|
|
143
|
-
" routes: [",
|
|
144
|
-
...routeEntries,
|
|
145
|
-
" ],",
|
|
146
|
-
routeMetadataLine,
|
|
147
|
-
ssgCacheLine,
|
|
148
|
-
...extraContext,
|
|
149
|
-
"});",
|
|
150
|
-
"",
|
|
151
|
-
`await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
|
|
152
|
-
"",
|
|
153
|
-
];
|
|
154
|
-
|
|
155
|
-
return lines.join("\n");
|
|
156
|
-
}
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import type { SsgCacheEntry } from "../server/cache/index.ts";
|
|
5
|
+
|
|
6
|
+
// import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
|
|
7
|
+
// through package exports, and is the Web-standard API. The main entry is
|
|
8
|
+
// `src/furin.ts` (or `dist/furin.js`), so we strip two path segments to reach
|
|
9
|
+
// the package root.
|
|
10
|
+
const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
|
|
11
|
+
const _pkgSrcDirRaw = existsSync(join(_pkgRoot, "src", "furin.ts"))
|
|
12
|
+
? join(_pkgRoot, "src")
|
|
13
|
+
: join(_pkgRoot, "dist");
|
|
14
|
+
// Normalize to forward slashes so endsWith checks and template paths work on Windows.
|
|
15
|
+
const _pkgSrcDir = _pkgSrcDirRaw.replace(/\\/g, "/");
|
|
16
|
+
const _ext = _pkgSrcDir.endsWith("/src") ? ".ts" : ".js";
|
|
17
|
+
const INTERNAL_MODULE_PATH = `${_pkgSrcDir}/server/internal${_ext}`;
|
|
18
|
+
const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/server/runtime-env${_ext}`;
|
|
19
|
+
|
|
20
|
+
export interface EntryTemplateOptions {
|
|
21
|
+
buildId?: string;
|
|
22
|
+
extraContext?: string[];
|
|
23
|
+
extraImports?: string[];
|
|
24
|
+
headerComment: string;
|
|
25
|
+
rootConventions?: { errorPath?: string; notFoundPath?: string };
|
|
26
|
+
rootPath: string;
|
|
27
|
+
routeMetadata?: Record<
|
|
28
|
+
string,
|
|
29
|
+
{
|
|
30
|
+
segmentBoundaries: Array<{
|
|
31
|
+
depth: number;
|
|
32
|
+
path: string;
|
|
33
|
+
errorPath?: string;
|
|
34
|
+
notFoundPath?: string;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
>;
|
|
38
|
+
routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
|
|
39
|
+
serverEntry: string;
|
|
40
|
+
ssgCache?: Record<string, SsgCacheEntry>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Unified options for generating a build entry (compile or disk-based). */
|
|
44
|
+
export interface BuildEntryOptions extends Omit<EntryTemplateOptions, "headerComment"> {
|
|
45
|
+
outDir: string;
|
|
46
|
+
headerComment?: string;
|
|
47
|
+
/** Embed mode: bundles client assets into the binary via `with { type: "file" }`. */
|
|
48
|
+
embed?: { clientDir: string };
|
|
49
|
+
publicDir?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function collectConventionPaths(
|
|
53
|
+
rootConventions: EntryTemplateOptions["rootConventions"],
|
|
54
|
+
routeMetadata: EntryTemplateOptions["routeMetadata"]
|
|
55
|
+
): string[] {
|
|
56
|
+
const paths: string[] = [];
|
|
57
|
+
if (rootConventions?.errorPath) {
|
|
58
|
+
paths.push(rootConventions.errorPath);
|
|
59
|
+
}
|
|
60
|
+
if (rootConventions?.notFoundPath) {
|
|
61
|
+
paths.push(rootConventions.notFoundPath);
|
|
62
|
+
}
|
|
63
|
+
if (routeMetadata) {
|
|
64
|
+
for (const meta of Object.values(routeMetadata)) {
|
|
65
|
+
for (const seg of meta.segmentBoundaries) {
|
|
66
|
+
if (seg.errorPath) {
|
|
67
|
+
paths.push(seg.errorPath);
|
|
68
|
+
}
|
|
69
|
+
if (seg.notFoundPath) {
|
|
70
|
+
paths.push(seg.notFoundPath);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return [...new Set(paths)];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function buildEntrySource(options: EntryTemplateOptions): string {
|
|
79
|
+
const {
|
|
80
|
+
buildId,
|
|
81
|
+
headerComment,
|
|
82
|
+
rootPath,
|
|
83
|
+
routes,
|
|
84
|
+
serverEntry,
|
|
85
|
+
rootConventions,
|
|
86
|
+
routeMetadata,
|
|
87
|
+
ssgCache,
|
|
88
|
+
} = options;
|
|
89
|
+
let { extraImports, extraContext } = options;
|
|
90
|
+
if (extraImports === undefined) {
|
|
91
|
+
extraImports = [];
|
|
92
|
+
}
|
|
93
|
+
if (extraContext === undefined) {
|
|
94
|
+
extraContext = [];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const allModulePaths = [
|
|
98
|
+
rootPath,
|
|
99
|
+
...routes.map((r) => r.path),
|
|
100
|
+
...collectConventionPaths(rootConventions, routeMetadata),
|
|
101
|
+
];
|
|
102
|
+
const moduleImports: string[] = [];
|
|
103
|
+
const moduleEntries: string[] = [];
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < allModulePaths.length; i++) {
|
|
106
|
+
const absPath = (allModulePaths[i] as string).replace(/\\/g, "/");
|
|
107
|
+
const varName = `_mod${i}`;
|
|
108
|
+
moduleImports.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
|
|
109
|
+
moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const routeEntries = routes.map(
|
|
113
|
+
(r) =>
|
|
114
|
+
` { pattern: ${JSON.stringify(r.pattern)}, path: ${JSON.stringify(r.path.replace(/\\/g, "/"))}, mode: ${JSON.stringify(r.mode)} },`
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const rootConventionsLine = rootConventions
|
|
118
|
+
? ` rootConventions: ${JSON.stringify(rootConventions)},`
|
|
119
|
+
: "";
|
|
120
|
+
const routeMetadataLine = routeMetadata
|
|
121
|
+
? ` routeMetadata: ${JSON.stringify(routeMetadata)},`
|
|
122
|
+
: "";
|
|
123
|
+
const ssgCacheLine = ssgCache ? ` ssgCache: ${JSON.stringify(ssgCache)},` : "";
|
|
124
|
+
|
|
125
|
+
const lines = [
|
|
126
|
+
headerComment,
|
|
127
|
+
`import { __setCompileContext } from ${JSON.stringify(INTERNAL_MODULE_PATH)};`,
|
|
128
|
+
`import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`,
|
|
129
|
+
...moduleImports,
|
|
130
|
+
...(extraImports.length > 0 ? ["", ...extraImports] : []),
|
|
131
|
+
"",
|
|
132
|
+
"// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
|
|
133
|
+
"__setDevMode(false);",
|
|
134
|
+
'process.env.NODE_ENV = "production";',
|
|
135
|
+
"",
|
|
136
|
+
"__setCompileContext({",
|
|
137
|
+
` buildId: ${JSON.stringify(buildId ?? "")},`,
|
|
138
|
+
` rootPath: ${JSON.stringify(rootPath.replace(/\\/g, "/"))},`,
|
|
139
|
+
rootConventionsLine,
|
|
140
|
+
" modules: {",
|
|
141
|
+
...moduleEntries,
|
|
142
|
+
" },",
|
|
143
|
+
" routes: [",
|
|
144
|
+
...routeEntries,
|
|
145
|
+
" ],",
|
|
146
|
+
routeMetadataLine,
|
|
147
|
+
ssgCacheLine,
|
|
148
|
+
...extraContext,
|
|
149
|
+
"});",
|
|
150
|
+
"",
|
|
151
|
+
`await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
|
|
152
|
+
"",
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
return lines.join("\n");
|
|
156
|
+
}
|