-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.mjs
72 lines (60 loc) · 1.77 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
// @ts-check
import * as esbuild from 'esbuild';
import url from "node:url";
import fs from 'fs';
import path from 'path';
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const nodeEnv = process.env.NODE_ENV || "production";
const isProd = nodeEnv === "production";
// Function to copy index.html to dist directory
const copyIndexHtml = () => {
const src = path.resolve(__dirname, 'index.html');
const distDir = path.resolve(__dirname, 'dist');
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
}
const dest = path.resolve(distDir, 'index.html');
fs.copyFileSync(src, dest);
};
/* In dev mode we run the esbuild.mjs directly from root, while in prod
we run it already from `_build/default` (see `dune` file) */
const entryPoint = nodeEnv === "production" ? 'output/src/App.mjs' : '_build/default/output/src/App.mjs';
// Build options
/** @type {import('esbuild').BuildOptions} */
const buildOptions = {
entryPoints: [entryPoint],
outfile: 'dist/App.mjs',
bundle: true,
minify: isProd,
sourcemap: isProd,
logLevel: "info",
loader: { '.jpg': 'dataurl' },
define: {
NODE_ENV: JSON.stringify(nodeEnv)
}
};
async function startBuild() {
copyIndexHtml();
if (nodeEnv === "production") {
try {
await esbuild.build(buildOptions);
} catch (error) {
console.error('Build error:', error);
process.exit(1);
}
} else {
// Create the esbuild context
let ctx = await esbuild.context(buildOptions);
// Watch for changes and rebuild
await ctx.watch();
// Start the dev server
let { host, port } = await ctx.serve({
servedir: 'dist',
port: 8080, // Change the port if needed
});
}
}
startBuild().catch((error) => {
console.error("Build error:", error);
process.exit(1)
});