-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.mjs
77 lines (67 loc) · 1.66 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
73
74
75
76
77
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import esbuild from "esbuild";
import { nodeExternalsPlugin } from "esbuild-node-externals";
const __dirname = dirname(fileURLToPath(import.meta.url));
const BUILD_CONFIGS = [
{
name: "esm",
format: "esm",
platform: "neutral",
target: ["esnext"],
entryPoints: ["src/index.ts"],
},
{
name: "cjs",
format: "cjs",
platform: "node",
target: ["node14"],
entryPoints: ["src/index.ts"],
},
];
const commonOptions = {
bundle: true,
sourcemap: true,
minify: process.env.NODE_ENV === "production",
logLevel: "info",
metafile: true,
};
async function buildAll() {
const builds = BUILD_CONFIGS.map((config) => build(config));
const results = await Promise.all(builds);
if (process.env.NODE_ENV === "production") {
Promise.all(
results.map(async (r) => {
const analysis = esbuild.analyzeMetafile(r.metafile);
console.info(analysis);
}),
);
}
}
async function build({ name, ...config }) {
const outfile = resolve(__dirname, `dist/${name}.js`);
console.log(`Building ${name}`);
const buildOptions = {
...commonOptions,
...config,
outfile,
plugins: [nodeExternalsPlugin()],
};
if (process.argv.includes("--watch")) {
const ctx = await esbuild.context(buildOptions);
await ctx.watch();
console.log(`Watching ${name}`);
} else {
return esbuild.build(buildOptions);
}
}
async function main() {
try {
await buildAll();
console.log("Build completed successfully");
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}
main();