-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.ts
More file actions
101 lines (85 loc) · 3.02 KB
/
build.ts
File metadata and controls
101 lines (85 loc) · 3.02 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
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
import type { BunPlugin } from "bun";
import path from "path";
async function build(): Promise<void> {
const srcDir = path.join(import.meta.dir, "src");
const outDir = path.join(import.meta.dir, "dist");
// Build ESM output
const esmResult = await Bun.build({
entrypoints: [path.join(srcDir, "index.ts")],
outdir: outDir,
format: "esm",
naming: {
entry: "[name].mjs",
},
target: "node",
external: ["@opencode-ai/plugin", "zod"],
minify: false,
splitting: false,
});
if (!esmResult.success) {
console.error("ESM build failed:", esmResult.logs);
process.exit(1);
}
// Build CommonJS output
const cjsResult = await Bun.build({
entrypoints: [path.join(srcDir, "index.ts")],
outdir: outDir,
format: "cjs",
naming: {
entry: "[name].js",
},
target: "node",
external: ["@opencode-ai/plugin", "zod"],
minify: false,
splitting: false,
});
if (!cjsResult.success) {
console.error("CJS build failed:", cjsResult.logs);
process.exit(1);
}
// Fix CJS export for OpenCode compatibility
// OpenCode expects the plugin function directly as module.exports
const cjsPath = path.join(outDir, "index.js");
let cjsContent = await Bun.file(cjsPath).text();
// Remove the original CommonJS export line
cjsContent = cjsContent.replace(
/module\.exports = __toCommonJS\(exports_src\);\n?/,
""
);
// Append the export at the end of the file
// This ensures TpsMeterPlugin is defined before we export it
cjsContent += `\n// OpenCode compatibility: export plugin function\n`;
cjsContent += `module.exports = exports_src.default;\n`;
cjsContent += `module.exports.default = exports_src.default;\n`;
cjsContent += `Object.defineProperty(module.exports, "__esModule", { value: true });\n`;
await Bun.write(cjsPath, cjsContent);
console.log("✓ Fixed CJS exports for OpenCode compatibility");
// Create a package.json in dist to force CommonJS mode for .js files
// This is needed because the root package.json has "type": "module"
const distPkgPath = path.join(outDir, "package.json");
await Bun.write(distPkgPath, JSON.stringify({ type: "commonjs" }, null, 2));
console.log("✓ Created dist/package.json with type: commonjs");
// Generate type declarations using tsc
const tscProcess = Bun.spawn(["bunx", "tsc", "--emitDeclarationOnly", "--declaration", "--outDir", "dist"], {
cwd: import.meta.dir,
stdout: "inherit",
stderr: "inherit",
});
const exitCode = await tscProcess.exited;
if (exitCode !== 0) {
console.error("Type declaration generation failed");
process.exit(1);
}
console.log("✓ Build completed successfully");
console.log(" - dist/index.mjs (ESM)");
console.log(" - dist/index.js (CommonJS - OpenCode compatible)");
console.log(" - dist/index.d.ts (TypeScript declarations)");
}
// Run build if this file is executed directly
if (import.meta.main) {
build().catch((error) => {
console.error("Build failed:", error);
process.exit(1);
});
}
export { build };