-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
46 lines (37 loc) · 1.31 KB
/
build.mjs
File metadata and controls
46 lines (37 loc) · 1.31 KB
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
import { build } from "esbuild";
import { fileURLToPath } from "url";
import { dirname, resolve } from "path";
import { builtinModules } from "module";
import { readFile, writeFile } from "fs/promises";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const outfile = resolve(__dirname, "built/main.mjs");
async function buildAndAddShebang() {
try {
// 1. Run esbuild with the banner to fix the dynamic require issue
await build({
entryPoints: [resolve(__dirname, "main.mts")],
bundle: true,
outfile: outfile,
platform: "node",
format: "esm",
tsconfig: resolve(__dirname, "tsconfig.json"),
external: [...builtinModules, "commander"],
define: { __dirname: '"__dirname"' },
banner: {
js: 'import { createRequire } from "module"; const require = createRequire(import.meta.url);',
},
});
// 2. Read the bundled output
const content = await readFile(outfile, "utf8");
// 3. Prepend the shebang
const finalContent = `#!/usr/bin/env node\n${content}`;
// 4. Write the final content back to the file
await writeFile(outfile, finalContent);
console.log("Build successful.");
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}
buildAndAddShebang();