-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ts
86 lines (77 loc) · 2.32 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
import { build as esbuild } from "esbuild";
import { nodeExternalsPlugin } from "esbuild-node-externals";
import util from "util";
import { exec } from "child_process";
const execPromise = util.promisify(exec);
/** Basic typescript build script
* commands:
* --tasks list tasks
* dev build dev bundle continually
* prod build production bundle
*
* source directories:
* src/ .ts/.tsx source files
* output directories:
* web/ debug bundle (includes source map)
* dist/ production bundle
*/
const productionOutDir = "dist";
const devOutDir = "web";
const defaultCmd = "dev";
const entryPoints = ["./src/Api.ts"];
export async function prod(): Promise<any> {
return Promise.all([compileDefinitions(), bundle(true)]);
//return Promise.all([compileDefinitions(), bundle(true)]);
}
export async function compileDefinitions(): Promise<any> {
return execPromise("tsc")
.then(() => console.log("compiled module definitions"));
}
export async function dev(): Promise<any> {
return bundle(false);
}
function bundle(production: boolean): Promise<void> {
const outdir = destDirectory(production);
const promisedResult = esbuild({
entryPoints,
outdir,
bundle: true,
// include src maps and no-minify dist for now
// sourcemap: !production,
// minify: production,
sourcemap: true,
minify: false,
splitting: true,
format: "esm",
target: "es2019",
plugins: [nodeExternalsPlugin()],
define: {
"process.env.NODE_ENV": production ? '"production"' : '"development"',
},
}).then(() => {
console.log("Built esbuild to " + outdir);
});
return promisedResult;
}
function destDirectory(production: boolean): string {
return production ? productionOutDir : devOutDir;
}
async function runCmd() {
const cmd = (process.argv[2] || defaultCmd).toLowerCase();
if (cmd === "--tasks") {
console.log(`tasks: ${Object.keys(exports).join("\n ")}`);
return 0;
} else {
const foundKey = Object.keys(exports).find(
(key: string) => key.toLowerCase() === cmd
);
if (foundKey) {
await exports[foundKey]();
} else {
console.error(`build command: "${cmd}" not found`);
return 1;
}
}
}
// must come after exports, so that the exported functions for commands are in module.exports
runCmd().then(() => 0);