Skip to content

Commit 975889e

Browse files
fix: don't expose internal build cache
1 parent 1e16891 commit 975889e

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

src/dev/builder.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,41 +173,67 @@ export class Builder<State = any> {
173173

174174
setBuildCache(devApp, buildCache);
175175

176+
// Boot in parallel to spin up the server quicker. We'll hold
177+
// requests until the required assets are processed.
176178
await Promise.all([
177179
devApp.listen(options),
178180
this.#build(buildCache, true),
179181
]);
180182
return;
181183
}
182184

183-
async build(): Promise<void> {
184-
this.config.mode = "production";
185-
186-
await this.#crawlFsItems();
187-
188-
const buildCache = new DiskBuildCache(
189-
this.config,
190-
this.#fsRoutes,
191-
this.#transformer,
192-
);
193-
194-
return await this.#build(buildCache, false);
195-
}
196-
197-
async buildForTests(): Promise<DevBuildCache<State>> {
198-
this.config.mode = "production";
185+
/**
186+
* Build optimized assets for your app. By default this will create
187+
* a production build.
188+
*
189+
* This can also be used for testing to apply a snapshot to a particular
190+
* {@linkcode App} instance.
191+
*
192+
* @example
193+
* ```ts
194+
* const builder = new Builder();
195+
* const applySnapshot = await builder.build({ snapshot: "memory" });
196+
*
197+
* Deno.test("My Test", () => {
198+
* const app = new App()
199+
* .get("/", () => new Response("hello"))
200+
*
201+
* applySnapshot(app)
202+
*
203+
* // ... your usual testing
204+
* })
205+
* ```
206+
* @param options
207+
* @returns Apply a snapshot to a particular {@linkcode App} instance.
208+
*/
209+
async build(
210+
options?: {
211+
mode?: ResolvedBuildConfig["mode"];
212+
snapshot?: "disk" | "memory";
213+
},
214+
): Promise<(app: App<State>) => void> {
215+
this.config.mode = options?.mode ?? "production";
199216

200217
await this.#crawlFsItems();
201218

202-
const buildCache = new MemoryBuildCache(
203-
this.config,
204-
this.#fsRoutes,
205-
this.#transformer,
206-
);
219+
const buildCache = options?.snapshot === "memory"
220+
? new MemoryBuildCache(
221+
this.config,
222+
this.#fsRoutes,
223+
this.#transformer,
224+
)
225+
: new DiskBuildCache(
226+
this.config,
227+
this.#fsRoutes,
228+
this.#transformer,
229+
);
207230

208-
await this.#build(buildCache, false);
231+
await this.#build(buildCache, this.config.mode === "development");
209232
await buildCache.prepare();
210-
return buildCache;
233+
234+
return (app) => {
235+
setBuildCache(app, buildCache);
236+
};
211237
}
212238

213239
async #crawlFsItems() {

tests/test_utils.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type App, setBuildCache } from "../src/app.ts";
1+
import type { App } from "../src/app.ts";
22
import { launch, type Page } from "@astral/astral";
33
import * as colors from "@std/fmt/colors";
44
import { DOMParser, HTMLElement } from "linkedom";
@@ -58,9 +58,7 @@ export async function buildProd(
5858
): Promise<<T>(app: App<T>) => void> {
5959
const outDir = await Deno.makeTempDir();
6060
const builder = new Builder({ outDir, ...options });
61-
const cache = await builder.buildForTests();
62-
63-
return (app) => setBuildCache(app, cache);
61+
return await builder.build({ mode: "production", snapshot: "memory" });
6462
}
6563

6664
export async function withBrowserApp(

0 commit comments

Comments
 (0)