-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
51 lines (44 loc) · 1.22 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
import cp from "node:child_process";
import { build } from "esbuild";
import fs from "fs-extra";
const DIST_DIR = "./dist";
const BUILD_TS_CONFIG_PATH = "./tsconfig.build.json";
/** @type import("esbuild").BuildOptions */
const sharedBuildOptions = {
entryPoints: ["src/index.ts"],
outdir: DIST_DIR,
bundle: true,
packages: "bundle",
minify: true,
sourcemap: "linked",
};
const buildCJS = async () =>
build({
...sharedBuildOptions,
format: "cjs",
outExtension: { ".js": ".cjs" },
});
const buildESM = async () =>
build({
...sharedBuildOptions,
format: "esm",
outExtension: { ".js": ".mjs" },
});
/** @type { () => Promise<void> } */
const buildTypes = async () =>
new Promise((resolve, reject) => {
const proc = cp.spawn("npx", ["tsc", "-p", BUILD_TS_CONFIG_PATH], { stdio: "inherit" });
proc.on("exit", (code) => {
if (code != null && code !== 0) {
reject(Error(`tsc exited with code ${code}`));
} else {
resolve();
}
});
});
// remove outputs of the last build
fs.rmSync(DIST_DIR, { force: true, recursive: true });
Promise.all([buildCJS(), buildESM(), buildTypes()]).catch((e) => {
console.error(`failed to build: ${e}`);
process.exit(1);
});