-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.ts
71 lines (69 loc) · 1.89 KB
/
bundle.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { bundle, BundleOptions } from "@deno/emit";
import { buildTeloMisikeke } from "telo-misikeke/build.ts";
import { fs } from "./src/misc.ts";
const SOURCE = new URL("./src/main.ts", import.meta.url);
const DESTINATION = new URL("./dist/main.js", import.meta.url);
const IMPORT_MAP = new URL("./bundle-imports.json", import.meta.url);
const buildOption: BundleOptions = {
compilerOptions: { inlineSourceMap: true },
type: "classic",
importMap: IMPORT_MAP,
};
async function build() {
console.log("Building main.js...");
const bundled = await bundle(SOURCE, buildOption);
const withUseStrict = bundled.code
.replace(/\(\s*function\s*\(\s*\)\s*\{/, '$&"use strict";');
await Deno.writeTextFile(DESTINATION, withUseStrict);
console.log("Building done!");
}
switch (Deno.args[0]) {
case "build": {
console.log("Building telo misikeke...");
await buildTeloMisikeke();
await build();
break;
}
case "watch": {
const builder = debounce(async () => {
try {
await build();
} catch (error) {
console.error(error);
}
}, 500);
const watcher = Deno.watchFs([
"./src/",
"./telo-misikeke/",
]);
try {
builder();
for await (const _ of watcher) {
builder();
}
} finally {
watcher.close();
}
throw new Error("unreachable");
}
default:
throw new Error(fs`Unrecognized build option, ${Deno.args[0]}`);
}
function debounce(callback: () => Promise<void>, delay: number): () => void {
let previous = { aborted: true };
let current = Promise.resolve();
return () => {
previous.aborted = true;
const newPrevious = { aborted: false };
setTimeout(() => {
if (!newPrevious.aborted) {
current = current
.then(() => callback())
.catch((error) => {
throw error;
});
}
}, delay);
previous = newPrevious;
};
}