-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.ts
89 lines (83 loc) · 2.61 KB
/
build.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { build, stop } from "esbuild";
import { denoPlugins } from "@luca/esbuild-deno-loader";
import { basename } from "@std/path";
import { extname } from "@std/path";
import { normalize } from "@std/path";
async function esbuild(inPath: string, outPath: string): Promise<void> {
const { errors, warnings } = await build({
plugins: denoPlugins(),
entryPoints: [inPath],
outfile: outPath,
format: "esm",
bundle: true,
minify: true,
sourcemap: "inline",
});
errors.forEach((error) => console.error(error));
warnings.forEach((warning) => console.warn(warning));
}
async function createScript(path: string, outDir: string): Promise<void> {
const startTime = performance.now();
if (extname(path) !== ".ts") {
throw new TypeError(
`Expected a ".ts" type extension. Got ${extname(path)} in ${path}`,
);
}
const name = basename(path).slice(0, -3);
const minPath = normalize(outDir + "/" + name + "min.js");
const promise = esbuild(path, minPath);
const file = await Deno.create(normalize(outDir + "/" + name + ".user.js"));
await file.write(new TextEncoder().encode(
await async function (): Promise<string> {
const text = await Deno.readTextFile(path);
const a = text.indexOf("// ==UserScript==");
if (a === -1) {
throw new SyntaxError(
`Failed to locate "// ==UserScript==" in ${path}`,
);
}
const b = text.indexOf("// ==/UserScript==", a) +
"// ==/UserScript==".length;
if (b === -1) {
throw new SyntaxError(
`Failed to locate "// ==/UserScript==" in ${path}`,
);
}
return text.slice(a, b) + "\n'use strict';\n";
}(),
));
await promise;
await (await Deno.open(minPath))
.readable
.pipeTo(file.writable);
await Deno.remove(minPath);
console.log(
`(${
(performance.now() - startTime).toLocaleString("en-US", {
maximumFractionDigits: 2,
})
}ms)\t${path}`,
);
}
await Promise.allSettled([
Deno.mkdir("static/js/", { recursive: true })
.then(() => esbuild("ts/main.ts", "static/js/main.js")),
Deno.mkdir("static/scripts/", { recursive: true })
.then(async () => {
const promises: Promise<void>[] = [];
for await (const dirEntry of Deno.readDir("src/")) {
if (dirEntry.isFile && extname(dirEntry.name) === ".ts") {
promises.push(
createScript("src/" + dirEntry.name, "static/scripts/"),
);
}
}
await Promise.allSettled(promises);
}),
]);
stop();
console.log(
`${
performance.now().toLocaleString("en-US", { maximumFractionDigits: 2 })
}ms`,
);