-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
64 lines (59 loc) · 1.63 KB
/
build.ts
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
import * as esbuild from "esbuild";
import { exec } from "child_process";
import { promisify } from "util";
const execPromise = promisify(exec);
// Function to generate TypeScript definitions using the tsc command
async function generateDts(entry: string, outfile: string): Promise<void> {
try {
const { stdout, stderr } = await execPromise(
`./node_modules/.bin/dts-bundle-generator -o ${outfile} ${entry}`
);
console.log(stdout);
if (stderr) {
console.error(stderr);
}
} catch (error) {
console.error(`Error generating .d.ts files: ${error}`);
throw error; // Rethrow to be caught by calling function
}
}
// Build configuration for Node.js
async function buildCjs(): Promise<void> {
try {
await esbuild.build({
entryPoints: ["./entries/index.ts"],
bundle: true,
outfile: "./dist/index.cjs",
format: "cjs",
platform: "node",
minify: true,
target: ["node20"],
});
console.log("Node build complete");
} catch (error) {
console.error("Node build failed:", error);
}
}
// Build configuration for the browser
async function buildBrowser(): Promise<void> {
try {
await esbuild.build({
entryPoints: ["./entries/index.ts"],
bundle: true,
outfile: "./dist/index.js",
format: "esm",
platform: "browser",
minify: true,
target: ["es2022"],
});
console.log("Browser build complete");
await generateDts("./entries/index.ts", "./dist/index.d.ts");
} catch (error) {
console.error("Browser build failed:", error);
}
}
// Run builds
(async () => {
await buildCjs();
await buildBrowser();
})();