-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
63 lines (51 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
62
63
import { rm } from "node:fs/promises";
import process from "node:process";
import autoprefixer from "autoprefixer";
import { build, context } from "esbuild";
import { htmlPlugin } from "esbuild-html-plugin";
import stylePlugin from "esbuild-style-plugin";
import tailwindcss from "tailwindcss";
const outdir = "dist";
const nodeEnv = process.env["NODE_ENV"] ?? "production";
const dev = nodeEnv !== "production";
const style = stylePlugin({ postcss: { plugins: [tailwindcss, autoprefixer] } });
const html = htmlPlugin({
outfile: "index.html",
language: "en",
createHeadElements: (outputUrls) => [
'<meta charset="utf-8" />',
'<meta name="viewport" content="width=device-width, initial-scale=1" />',
'<meta name="description" content="A conversational rabbit hole!">',
"<title>DownTheHole.wtf</title>",
...outputUrls
.filter((url) => url.endsWith(".css"))
.map((url) => `<link href="${url}" rel="stylesheet">`),
...outputUrls
.filter((url) => url.endsWith(".js"))
.map((url) => `<script src="${url}" defer></script>`),
],
createBodyElements: () => [`<main id="app"></main>`],
});
console.log(`Building in ${dev ? "DEV" : "PROD"} mode`);
/** @type {import('esbuild').BuildOptions} */
const buildOptions = {
bundle: true,
entryNames: "[dir]/[name]-[hash]",
entryPoints: [{ out: "client", in: "src/client.tsx" }],
metafile: true,
minify: !dev,
outdir,
plugins: [style, html],
publicPath: "/",
sourcemap: dev,
target: "es2022",
treeShaking: true,
tsconfig: "tsconfig.json",
};
if (process.argv.includes("--watch")) {
const ctx = await context(buildOptions);
await ctx.watch();
} else {
await rm(outdir, { recursive: true, force: true });
await build(buildOptions);
}