Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
57 lines (51 loc) · 1.86 KB
/
Copy pathbuild.js
File metadata and controls
57 lines (51 loc) · 1.86 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
46
47
48
49
50
51
52
53
54
55
56
57
import { build } from "esbuild";
import { readFile, writeFile, stat, chmod } from "fs/promises";
// Read dependencies from package.json
const packageJson = JSON.parse(await readFile(new URL("./package.json", import.meta.url), "utf8"));
const externalDeps = Object.keys(packageJson.dependencies || {});
async function getFileSize(filePath) {
const stats = await stat(filePath);
return (stats.size / 1024).toFixed(2); // Convert bytes to kilobytes and format to 2 decimal places
}
await Promise.all([
build({
entryPoints: ["lib/index.js"], // Main library entry point
bundle: true,
format: "esm",
platform: "node",
sourcemap: false,
external: externalDeps,
outdir: "dist",
}).then(async () => {
const size = await getFileSize("dist/index.js");
console.log(`✅ Main library bundle size: ${size} KB`);
}),
build({
entryPoints: ["lib/cli.js"], // CLI entry point
bundle: true,
format: "esm", // Use CommonJS format for CLI
platform: "node",
sourcemap: false,
external: [...externalDeps], // Add Node.js built-in modules to external dependencies
outfile: "dist/cli.js",
loader: {
".js": "js",
".json": "json"
}, // ✅ Tell esbuild to handle JSON files
}).then(async () => {
// Restore the shebang after bundling
const cliPath = "dist/cli.js";
let content = await readFile(cliPath, "utf8");
if (!content.startsWith('#!/usr/bin/env node')) {
content = `#!/usr/bin/env node\n${content}`;
await writeFile(cliPath, content);
console.log("✅ Shebang restored in CLI build");
}
// Give the CLI script executable permissions
await chmod(cliPath, 0o755);
console.log("✅ Executable permissions set for CLI build");
const size = await getFileSize(cliPath);
console.log(`✅ CLI bundle size: ${size} KB`);
}),
]);
console.log("✅ Build successful");