-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.mjs
executable file
·81 lines (73 loc) · 1.73 KB
/
esbuild.mjs
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
#!/usr/bin/env node
import fs from "node:fs";
import esbuild from "esbuild";
// CONFIG
const DIST_DIR = "./dist";
const PORT = 3000;
const ESBUILD_CONFIG = {
platform: "browser",
target: "esnext",
bundle: true,
splitting: true,
format: "esm",
sourcemap: false,
entryPoints: ["./src/index.jsx"],
loader: { ".js": "jsx", ".html": "copy" },
outdir: `${DIST_DIR}`,
metafile: true,
entryNames: "[name]-[hash]",
chunkNames: "assets/[ext]/[name]-[hash]",
assetNames: "assets/[ext]/[name]-[hash]",
};
const serve = async () => {
// Create a context for incremental builds
const context = await esbuild.context({
...ESBUILD_CONFIG,
minify: false,
sourcemap: true,
write: false,
outdir: "./public",
banner: {
js: "new EventSource('/esbuild').addEventListener('change', () => location.reload());",
},
});
// Enable watch mode
await context.watch();
// Enable serve mode
await context.serve({ port: PORT, servedir: "./public", keyfile: "key.pem", certfile: "cert.pem" });
// Dispose of the context
// context.dispose();
};
/**
*
*/
const build = async () => {
// Check if dist directory exists, if not create it
if (!fs.existsSync(DIST_DIR)) {
fs.mkdir(DIST_DIR, (err) => {
if (err) throw err;
console.log(`${DIST_DIR} created.`);
});
} else {
console.log(`${DIST_DIR} already exists.`);
}
// Build our files
let buildResult = await esbuild.build({
...ESBUILD_CONFIG,
outdir: `${DIST_DIR}`,
minify: true,
write: true,
});
fs.writeFileSync('buildMeta.json', JSON.stringify(buildResult.metafile))
};
/**
*
*/
const init = () => {
if (process.argv.includes("--serve")) {
serve();
} else {
build();
}
};
init();