Skip to content

Commit 236ca98

Browse files
authored
docs: add BuildEntryFunction API docs and document build option (#105)
1 parent f71429a commit 236ca98

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

packages/docs/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DeferAndActivity from "./pages/learn/DeferAndActivity.mdx";
1212
import FileSystemRouting from "./pages/learn/FileSystemRouting.mdx";
1313
import MultipleEntrypoints from "./pages/advanced/MultipleEntrypoints.mdx";
1414
import SSR from "./pages/advanced/SSR.mdx";
15+
import BuildEntryApi from "./pages/api/BuildEntry.mdx";
1516
import EntryDefinitionApi from "./pages/api/EntryDefinition.mdx";
1617
import FAQ from "./pages/FAQ.mdx";
1718
import GettingStarted from "./pages/GettingStarted.mdx";
@@ -65,6 +66,12 @@ export const routes: RouteDefinition[] = [
6566
path: "/api/defer",
6667
component: <Layout>{defer(<DeferApi />, { name: "DeferApi" })}</Layout>,
6768
}),
69+
route({
70+
path: "/api/build-entry",
71+
component: (
72+
<Layout>{defer(<BuildEntryApi />, { name: "BuildEntryApi" })}</Layout>
73+
),
74+
}),
6875
route({
6976
path: "/api/entry-definition",
7077
component: (

packages/docs/src/components/Sidebar/Sidebar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export const navigation: NavSection[] = [
7171
href: "/api/funstack-static",
7272
},
7373
{ label: "defer()", href: "/api/defer" },
74+
{
75+
label: "BuildEntryFunction",
76+
href: "/api/build-entry",
77+
},
7478
{
7579
label: "EntryDefinition",
7680
href: "/api/entry-definition",
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# BuildEntryFunction
2+
3+
The `BuildEntryFunction` type defines a custom build entry point that lets you hook into the production build process. Use it to run additional work before, after, or in parallel with the default build.
4+
5+
## Import
6+
7+
```typescript
8+
import type { BuildEntryFunction } from "@funstack/static/server";
9+
```
10+
11+
## Usage
12+
13+
Create a module that default-exports a `BuildEntryFunction`, then reference it via the [`build`](/api/funstack-static#build-optional) option in your Vite config:
14+
15+
```typescript
16+
// vite.config.ts
17+
import funstackStatic from "@funstack/static";
18+
import { defineConfig } from "vite";
19+
20+
export default defineConfig({
21+
plugins: [
22+
funstackStatic({
23+
entries: "./src/entries.tsx",
24+
build: "./src/build.ts",
25+
}),
26+
],
27+
});
28+
```
29+
30+
```typescript
31+
// src/build.ts
32+
import type { BuildEntryFunction } from "@funstack/static/server";
33+
34+
export default (async ({ build, outDir }) => {
35+
// Run the default build
36+
await build();
37+
// ... do additional work after the build
38+
}) satisfies BuildEntryFunction;
39+
```
40+
41+
## Type Definition
42+
43+
```typescript
44+
type BuildEntryFunction = (context: BuildEntryContext) => Promise<void> | void;
45+
46+
interface BuildEntryContext {
47+
build: () => Promise<void>;
48+
outDir: string;
49+
}
50+
```
51+
52+
## Context Properties
53+
54+
### build
55+
56+
**Type:** `() => Promise<void>`
57+
58+
Performs the default build flow (rendering entries and writing output files). You must call this function to produce the standard build output. You can run additional work before, after, or in parallel with it.
59+
60+
### outDir
61+
62+
**Type:** `string`
63+
64+
Absolute path to the output directory where built files are written. Use this to write additional files alongside the build output.
65+
66+
## Examples
67+
68+
### Generate a sitemap alongside the build
69+
70+
```typescript
71+
import { writeFile } from "node:fs/promises";
72+
import path from "node:path";
73+
import type { BuildEntryFunction } from "@funstack/static/server";
74+
75+
export default (async ({ build, outDir }) => {
76+
const sitemap = generateSitemap(); // your sitemap logic
77+
78+
await Promise.all([
79+
build(),
80+
writeFile(path.join(outDir, "sitemap.xml"), sitemap),
81+
]);
82+
}) satisfies BuildEntryFunction;
83+
```
84+
85+
By running `build()` and `writeFile()` in parallel with `Promise.all`, the sitemap is generated without adding to the total build time.
86+
87+
### Run work before or after the build
88+
89+
```typescript
90+
import type { BuildEntryFunction } from "@funstack/static/server";
91+
92+
export default (async ({ build }) => {
93+
console.log("Build starting...");
94+
await build();
95+
console.log("Build complete!");
96+
}) satisfies BuildEntryFunction;
97+
```
98+
99+
## Notes
100+
101+
- The build entry module runs in the **RSC environment**, so you have access to Node.js APIs.
102+
- The `build` option is only used during **production builds** (`vite build`), not in dev mode.
103+
- If no `build` option is specified, the default build flow runs automatically.
104+
105+
## See Also
106+
107+
- [funstackStatic()](/api/funstack-static) - Plugin configuration (includes the `build` option)
108+
- [Multiple Entrypoints](/advanced/multiple-entrypoints) - Multi-page static site generation

packages/docs/src/pages/api/FunstackStatic.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ Sentry.init({
226226

227227
**Note:** Errors in the client init module will propagate normally and prevent the app from rendering.
228228

229+
### build (optional)
230+
231+
**Type:** `string`
232+
233+
Path to a module that customizes the production build process. The module should `export default` a function that receives a `BuildEntryContext` with a `build` function (which performs the default build flow) and an `outDir` string (the output directory path).
234+
235+
This allows you to run additional work before, after, or in parallel with the build — for example, generating a sitemap or other static assets.
236+
237+
Only called during production builds (`vite build`), not in dev mode. The module runs in the RSC environment.
238+
239+
```typescript
240+
funstackStatic({
241+
entries: "./src/entries.tsx",
242+
build: "./src/build.ts",
243+
});
244+
```
245+
246+
See [BuildEntryFunction](/api/build-entry) for the full type definition and examples.
247+
229248
### rscPayloadDir (optional)
230249

231250
**Type:** `string`
@@ -295,5 +314,6 @@ You can use the same Vite commands you would use in a normal Vite project:
295314

296315
- [Getting Started](/getting-started) - Quick start guide
297316
- [Multiple Entrypoints](/advanced/multiple-entrypoints) - Multi-page static site generation
317+
- [BuildEntryFunction](/api/build-entry) - Custom build entry point
298318
- [defer()](/api/defer) - Deferred rendering for streaming
299319
- [React Server Components](/learn/rsc) - Understanding RSC

0 commit comments

Comments
 (0)