|
| 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 |
0 commit comments