-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
52 lines (44 loc) · 1.12 KB
/
build.js
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
const chokidar = require("chokidar");
const esbuild = require("esbuild");
const glob = require("fast-glob");
const { Project } = require("ts-morph");
async function build(filePattern) {
const outDir = "lib";
const files = await glob(filePattern);
const tsProject = new Project({
compilerOptions: { outDir, emitDeclarationOnly: true },
tsConfigFilePath: "./tsconfig.json",
skipAddingFilesFromTsConfig: true,
});
tsProject.addSourceFilesAtPaths(files);
await tsProject.emit();
await esbuild.build({
entryPoints: files,
outdir: outDir,
format: "esm",
sourcemap: true,
});
}
function watch(filePattern) {
chokidar.watch(filePattern).on("change", async (file) => {
try {
console.log("file changed:", file);
await build(filePattern);
} catch {
// nevermind, errors are logged by esbuild
}
});
}
async function run() {
const watchMode = process.argv.includes("--watch");
const filePattern = "src/**/*.ts";
await build(filePattern).catch(() => {
if (!watchMode) {
process.exit(1);
}
});
if (watchMode) {
watch(filePattern);
}
}
run();