1- // Build script for @codebuff /sdk using Bun's bundler with splitting
2- // Creates structured output with separate chunks for better Node.js compatibility
1+ // Build script for @codebuff /sdk using Bun's bundler with dual package support
2+ // Creates ESM + CJS bundles with TypeScript declarations
33
44import { execSync } from 'child_process'
55import { mkdir , cp } from 'fs/promises'
@@ -27,6 +27,21 @@ async function build() {
2727 'util' ,
2828 ]
2929
30+ console . log ( '📦 Building ESM format...' )
31+ await Bun . build ( {
32+ entrypoints : [ 'src/index.ts' ] ,
33+ outdir : 'dist' ,
34+ target : 'node' ,
35+ format : 'esm' ,
36+ sourcemap : 'external' ,
37+ minify : false ,
38+ external,
39+ naming : '[dir]/index.mjs' ,
40+ loader : {
41+ '.scm' : 'text' ,
42+ } ,
43+ } )
44+
3045 console . log ( '📦 Building CJS format...' )
3146 await Bun . build ( {
3247 entrypoints : [ 'src/index.ts' ] ,
@@ -36,6 +51,7 @@ async function build() {
3651 sourcemap : 'external' ,
3752 minify : false ,
3853 external,
54+ naming : '[dir]/index.cjs' ,
3955 define : {
4056 'import.meta.url' : 'undefined' ,
4157 'import.meta' : 'undefined' ,
@@ -48,27 +64,31 @@ async function build() {
4864 console . log ( '📝 Generating TypeScript declarations...' )
4965 try {
5066 execSync ( 'tsc -p tsconfig.build.json' , { stdio : 'inherit' } )
67+ await createSimpleIndexTypes ( )
5168 } catch ( error ) {
5269 console . warn ( '⚠ TypeScript declaration generation failed, continuing...' )
5370 }
5471
5572 console . log ( '📂 Copying WASM files for tree-sitter...' )
5673 await copyWasmFiles ( )
5774
58- console . log ( '📝 Creating colocated declaration files...' )
59- await createColocatedDeclarations ( )
60-
6175 console . log ( '✅ Build complete!' )
76+ console . log ( ' 📄 dist/index.mjs (ESM)' )
77+ console . log ( ' 📄 dist/index.cjs (CJS)' )
78+ console . log ( ' 📄 dist/index.d.ts (Types)' )
6279}
6380
6481/**
65- * Create colocated declaration files for better TypeScript compatibility
82+ * Create a simple index.d.ts that re-exports from the generated types
6683 */
67- async function createColocatedDeclarations ( ) {
68- // Create index.d.ts that exports everything from ./sdk/src/index.d.ts
69- const indexDeclaration = 'export * from "./sdk/src/index";\n'
70- await Bun . write ( 'dist/index.d.ts' , indexDeclaration )
71- console . log ( ' ✓ Created dist/index.d.ts' )
84+ async function createSimpleIndexTypes ( ) {
85+ try {
86+ const indexDeclaration = 'export * from "./sdk/src/index";\n'
87+ await Bun . write ( 'dist/index.d.ts' , indexDeclaration )
88+ console . log ( ' ✓ Created simple re-export types' )
89+ } catch ( error ) {
90+ console . warn ( ' ⚠ Warning: Could not create index types:' , error . message )
91+ }
7292}
7393
7494/**
0 commit comments