-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
57 lines (53 loc) · 1.45 KB
/
build.js
File metadata and controls
57 lines (53 loc) · 1.45 KB
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
import * as esbuild from "esbuild"
import esbuildPluginTsc from "esbuild-plugin-tsc"
const buildOptions = {
entryPoints: ["src/main.ts"],
outfile: "dist/main.bundle.js",
bundle: true,
plugins: [
esbuildPluginTsc({
tsconfigPath: "./tsconfig.json",
force: true,
}),
],
sourcemap: true,
platform: "node",
// While this seems to be the desired setting, this doesn't work.
// See https://github.com/evanw/esbuild/issues/1921 and https://github.com/evanw/esbuild/issues/3324#issuecomment-2215644754
// format: 'esm',
minify: true,
}
if (process.env.NODE_ENV === "production") {
buildOptions.define = { "process.env.NODE_ENV": '"production"' }
} else {
buildOptions.define = { "process.env.NODE_ENV": '"development"' }
}
const plugins = [
{
name: "post-build-plugin",
setup(build) {
let count = 0
let t0 = Date.now()
build.onStart(() => {
t0 = Date.now()
})
build.onEnd(async (result) => {
if (result.errors.length > 0) {
console.error("build failed:", result.errors)
} else {
if (count++ === 0) console.log(`build completed in ${Date.now() - t0}ms`)
else console.log(`re-build completed in ${Date.now() - t0}ms`)
}
})
},
},
]
export const ctx = await esbuild.context({ ...buildOptions, plugins })
// Check if --watch flag is present in process arguments
if (process.argv.includes("--watch")) {
// Set up watch mode with hash computation after each rebuild
ctx.watch()
} else {
ctx.rebuild()
ctx.dispose()
}