-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.ts
64 lines (56 loc) · 1.52 KB
/
run.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
import { config } from "./src/config";
import { BuildOptions, build } from "esbuild";
import { clean } from "esbuild-plugin-clean";
import { createServer } from "esbuild-server";
import { writeFile } from "fs/promises";
import { basename } from "path";
const isDev = process.argv.includes("--dev");
function getFileName(filename: `${string}.js`): string {
return (
(isDev ? `.${config.files.directory}` : "") + basename(filename, ".js")
);
}
const baseConfig: BuildOptions = {
bundle: true,
format: "iife",
legalComments: "eof",
plugins: [
clean({
patterns: ["./dist/*"]
})
],
entryPoints: {
[getFileName(config.files.config)]: "./src/config.ts",
[getFileName(config.files.client)]: "./src/client/index.ts",
[getFileName(config.files.worker)]: "./src/worker.ts",
[getFileName(config.files.bundle)]: "./src/bundle.ts"
},
outdir: "./dist/",
logLevel: "info",
metafile: true
};
if (isDev) {
// start dev server
const server = createServer(
{ ...baseConfig, sourcemap: true },
{
static: "./public/",
port: process.env.PORT ? parseInt(process.env.PORT) : 3000
}
);
await server.start();
console.log("Dev server started!");
console.log(server.url);
} else {
// bundle for production
console.log("Building...");
// minify and treeshake output for production
const result = await build({
...baseConfig,
treeShaking: true,
minify: true
});
if (result.metafile) {
await writeFile("./metafile.json", JSON.stringify(result.metafile));
}
}