@@ -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( ) {
0 commit comments