|
2 | 2 |
|
3 | 3 | import { assert } from "@std/assert/assert"; |
4 | 4 | import { exists } from "@std/fs/exists"; |
5 | | -import { BuildContext, BuildOptions, context } from "esbuild"; |
| 5 | +import { BuildOptions, context } from "esbuild"; |
6 | 6 | import { OPTIONS } from "./config.ts"; |
7 | 7 |
|
8 | 8 | const BUILD_OPTIONS: BuildOptions = { |
9 | 9 | ...OPTIONS, |
10 | 10 | minify: false, |
11 | 11 | define: { LIVE_RELOAD: "true" }, |
12 | 12 | }; |
13 | | -async function watchMain(): Promise<BuildContext<BuildOptions>> { |
| 13 | +async function watchMain(): Promise<AsyncDisposable> { |
| 14 | + await using stack = new AsyncDisposableStack(); |
14 | 15 | const buildContext = await context(BUILD_OPTIONS); |
15 | | - try { |
16 | | - await buildContext.watch(); |
17 | | - await buildContext.serve({ servedir: "./dist/" }); |
18 | | - } catch (error) { |
19 | | - await buildContext.dispose(); |
20 | | - throw error; |
21 | | - } |
22 | | - return buildContext; |
| 16 | + stack.defer(async () => await buildContext.dispose()); |
| 17 | + buildContext.watch(); |
| 18 | + buildContext.serve({ servedir: "./dist/" }); |
| 19 | + return stack.move(); |
23 | 20 | } |
24 | 21 | async function watchDictionary(): Promise<number> { |
25 | 22 | const command = new Deno.Command(Deno.execPath(), { |
@@ -47,16 +44,14 @@ async function watchDictionary(): Promise<number> { |
47 | 44 | if (import.meta.main) { |
48 | 45 | let statusCode: number; |
49 | 46 | { |
50 | | - await using stack = new AsyncDisposableStack(); |
51 | 47 | if ( |
52 | 48 | !await exists(new URL("../dictionary/dictionary.ts", import.meta.url)) |
53 | 49 | ) { |
54 | 50 | const Dictionary = await import("../dictionary/build.ts"); |
55 | 51 | await Dictionary.build(); |
56 | 52 | } |
57 | 53 | const statusCodePromise = watchDictionary(); |
58 | | - const context = await watchMain(); |
59 | | - stack.defer(async () => await context.dispose()); |
| 54 | + await using _ = await watchMain(); |
60 | 55 | statusCode = await statusCodePromise; |
61 | 56 | } |
62 | 57 | Deno.exit(statusCode); |
|
0 commit comments