-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
91 lines (79 loc) · 2.14 KB
/
esbuild.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { build, context } from "esbuild";
import fs from "fs";
import path from "path";
import { spawn } from "child_process";
const getNodeModules = () => {
const nodeModulesPath = path.resolve(import.meta.dirname, "node_modules");
if (!fs.existsSync(nodeModulesPath)) {
return [];
}
return fs.readdirSync(nodeModulesPath).filter((module) => {
return !module.startsWith(".");
});
};
const IS_WATCH_MODE = process.argv[2] === "watch";
const IS_NPM_BUNDLE = process.argv[2] !== "full";
const ARGUMENTS = process.argv[3] || "";
const ESBUILD_NAME = IS_NPM_BUNDLE ? "NPM Bundle" : "Full Bundle";
const OUT_FILE = IS_NPM_BUNDLE ? "dist/index.js" : "build/bundle/index.js";
const nodeModules = IS_NPM_BUNDLE ? getNodeModules() : [];
const buildOptions = {
bundle: true,
entryPoints: ["src/index.ts"],
external: nodeModules,
format: IS_NPM_BUNDLE ? "esm" : "cjs",
outfile: OUT_FILE,
platform: "node",
};
let childProcess = null;
const runOutputFile = () => {
if (childProcess) {
childProcess.kill();
}
childProcess = spawn("node", [OUT_FILE, ...ARGUMENTS.split(" ")], {
stdio: "inherit",
});
};
const cleanUp = () => {
if (childProcess) {
childProcess.kill();
}
process.exit();
};
const watchOutputFile = () => {
fs.watch(OUT_FILE, (eventType) => {
if (eventType === "change") {
runOutputFile();
}
});
};
process.on("exit", cleanUp);
process.on("SIGINT", cleanUp);
process.on("SIGTERM", cleanUp);
process.on("uncaughtException", cleanUp);
if (IS_WATCH_MODE) {
context(buildOptions)
.then(async (ctx) => {
await ctx.watch();
console.log(
`Watching ${buildOptions.entryPoints[0]}... (out: ${ESBUILD_NAME} '${OUT_FILE}')`,
);
runOutputFile();
watchOutputFile();
})
.catch((e) => {
console.error(
`Error setting up watch mode for ${ESBUILD_NAME}: ${e.message}`,
);
process.exit(1);
});
} else {
build(buildOptions)
.then(() => {
console.log(`${ESBUILD_NAME} has been built to ${OUT_FILE}`);
})
.catch((e) => {
console.error(`Error building ${ESBUILD_NAME}: ${e.message}`);
process.exit(1);
});
}