summaryrefslogtreecommitdiff
path: root/client-web/watch-builder.ts
blob: 05871e1aa1b6426d5279bb3f40abf46dc7714891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// `emit` uses fetch to *download* was, that is just stupid
// import { bundle } from "https://deno.land/x/emit@0.1.1/mod.ts";

// instead, lets run `deno bundle` manually

async function bundle(entry: string, options: { compilerOptions: { checkJs: boolean } }) {
    const proc = Deno.run({ cmd: ["deno", "bundle", options.compilerOptions.checkJs ? "--check" : "--no-check", "--unstable", entry], stdout: "piped" })
    const out = await proc.output()
    const code = new TextDecoder().decode(out)
    return { code }
}

let refresh_needed = false
let refresh_pending = false
async function refresh() {
    refresh_needed = true
    if (refresh_pending) return
    refresh_needed = false
    refresh_pending = true

    try {
        const { code } = await bundle("source/index.ts", { compilerOptions: { checkJs: false } })
        await Deno.writeTextFile("public/assets/bundle.js", code)
    } catch (e) { console.error(e) }

    refresh_pending = false
    if (refresh_needed) refresh()
}

refresh()
for await (const event of Deno.watchFs("source")) {
    if (event.kind == "modify" || event.kind == "create") {
        refresh()
    }
}