-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
executable file
·85 lines (76 loc) · 1.98 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
#!/usr/bin/env ts-node
import { execSync } from "child_process";
import * as esbuild from "esbuild";
import { z } from "zod";
const TargetSchema = z.union([z.literal("chrome"), z.literal("firefox")]);
const TARGET = TargetSchema.parse(process.env.TARGET);
const NodeEnvSchema = z.union([
z.literal("development"),
z.literal("production"),
]);
const NODE_ENV = NodeEnvSchema.parse(process.env.NODE_ENV);
const cleanupPlugin = (): esbuild.Plugin => ({
name: "cleanup",
setup() {
execSync(`rm -rf dist/${TARGET}`);
execSync(`rm -f dist/${TARGET}.zip`);
},
});
const copyAssetsPlugin = (): esbuild.Plugin => ({
name: "copy-assets",
setup(build) {
build.onEnd(() => {
execSync(`cp -r src/public/${TARGET}/* dist/${TARGET}`);
});
},
});
const tailwindPlugin = (): esbuild.Plugin => ({
name: "tailwind",
setup(build) {
build.onStart(() => {
const minify = NODE_ENV === "production" ? "--minify" : "";
execSync(
`tailwindcss -i src/globals.css -o dist/${TARGET}/output.css ${minify}`,
);
});
},
});
const zipPlugin = (): esbuild.Plugin => ({
name: "zip",
setup(build) {
build.onEnd(() => {
execSync(`zip -r ../${TARGET}.zip *`, { cwd: `dist/${TARGET}` });
});
},
});
const baseOptions: esbuild.BuildOptions = {
entryPoints: ["src/public/index.tsx", "src/public/index.html"],
bundle: true,
loader: {
".html": "copy",
},
plugins: [
cleanupPlugin(),
copyAssetsPlugin(),
tailwindPlugin(),
zipPlugin(),
],
target: ["chrome58", "firefox57"],
outdir: `dist/${TARGET}`,
};
async function build() {
if (process.env.NODE_ENV === "production") {
esbuild.build({
...baseOptions,
minify: true,
});
} else {
const context = await esbuild.context({
...baseOptions,
sourcemap: true,
});
await context.watch();
console.log("Watching for changes...");
}
}
build();