-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
esbuild.js
112 lines (106 loc) · 2.83 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
import esbuild from "esbuild";
// Automatically exclude all node_modules from the bundled version
import { nodeExternalsPlugin } from "esbuild-node-externals";
const build = async () => {
const bundleCtx = await esbuild.context({
entryPoints: ["./src/main.ts"],
outfile: process.env.ESM_BUILD ? "dist/bundle.js" : "dist/bundle.cjs",
bundle: true,
platform: "node",
sourcemap: true,
target: "node12",
format: process.env.ESM_BUILD ? "esm" : "cjs",
minify: true,
banner: {
js: process.env.ESM_BUILD
? `
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire as topLevelCreateRequire } from 'module';
const require = topLevelCreateRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
`
: "",
},
plugins: [
nodeExternalsPlugin(),
{
name: "watch",
setup(build) {
build.onEnd((result) => {
console.log("===========================================");
// error if errors array have errors
if (result.errors.length > 0) {
console.log(
`${new Date().toLocaleString()}: main module build failed.`,
);
process.exit(1);
} else {
console.log(
`${new Date().toLocaleString()}: main module build succeeded.`,
);
}
});
},
},
],
});
const cliCtx = await esbuild.context({
entryPoints: ["./src/cli.ts"],
outfile: process.env.ESM_BUILD
? "dist/cli-bundle.js"
: "dist/cli-bundle.cjs",
bundle: true,
platform: "node",
sourcemap: true,
target: "node12",
format: process.env.ESM_BUILD ? "esm" : "cjs",
minify: true,
banner: {
js: process.env.ESM_BUILD
? `
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire as topLevelCreateRequire } from 'module';
const require = topLevelCreateRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
`
: "",
},
plugins: [
nodeExternalsPlugin(),
{
name: "watch",
setup(build) {
build.onEnd((result) => {
console.log("===========================================");
// error if errors array have errors
if (result.errors.length > 0) {
console.log(
`${new Date().toLocaleString()}: cli module build failed.`,
);
process.exit(1);
} else {
console.log(
`${new Date().toLocaleString()}: cli module build succeeded.`,
);
}
});
},
},
],
});
if (process.env.NODE_ENV === "production") {
await bundleCtx.rebuild();
bundleCtx.dispose();
await cliCtx.rebuild();
cliCtx.dispose();
} else {
await bundleCtx.watch();
await cliCtx.watch();
}
};
build();