-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuild.ts
80 lines (75 loc) · 1.75 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
import * as esbuild from "esbuild";
import cssModulesPlugin from "esbuild-css-modules-plugin";
import { copy } from "esbuild-plugin-copy";
import * as process from "process";
const buildMode = "--build";
const serveMode = "--serve";
const helpString = `Mode must be provided as one of ${buildMode} or ${serveMode}`;
const args = process.argv.splice(2);
if (args.length !== 1) {
console.error(helpString);
process.exit(1);
}
const mode = args[0];
const buildOptions: esbuild.BuildOptions = {
entryPoints: {
index: "src/index.ts",
},
bundle: true,
external: ["node:crypto"],
write: true,
metafile: true,
publicPath: "",
sourcemap: true,
assetNames: "[name]-[hash]",
preserveSymlinks: true,
outdir: "./build/",
splitting: true,
platform: "browser",
format: "esm",
loader: {
".svg": "file",
".png": "file",
".jpg": "file",
".css": "css",
},
plugins: [
cssModulesPlugin({}),
copy({
resolveFrom: "cwd",
assets: {
from: ["./src/static/**/*"],
to: ["./build/"],
},
}),
],
};
switch (mode) {
case buildMode:
esbuild.build(buildOptions).catch(() => process.exit(1));
break;
case serveMode:
// eslint-disable-next-line no-case-declarations
const portArg = process.env.PORT;
if (!portArg) {
console.error("PORT environment variable is not set for server");
process.exit(1);
}
esbuild
.context({ ...buildOptions })
.then((context) =>
context.serve({
host: "127.0.0.1",
port: parseInt(portArg, 10),
servedir: "./build/",
}),
)
.catch((e) => {
console.error(e);
process.exit(1);
});
break;
default:
console.error(helpString);
process.exit(1);
}