Skip to content

Commit ab17e33

Browse files
authored
SDK: Support both esm and commonjs again (#304)
1 parent d6671e2 commit ab17e33

File tree

16 files changed

+973
-117
lines changed

16 files changed

+973
-117
lines changed

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/build.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
import { execSync } from 'child_process'
55
import { 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
/**

sdk/package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
"version": "0.2.0",
66
"description": "Official SDK for Codebuff — AI coding agent & framework",
77
"license": "Apache-2.0",
8-
"type": "commonjs",
9-
"main": "./dist/index.js",
8+
"type": "module",
9+
"main": "./dist/index.cjs",
10+
"module": "./dist/index.mjs",
1011
"types": "./dist/index.d.ts",
1112
"typings": "./dist/index.d.ts",
1213
"exports": {
1314
".": {
1415
"types": "./dist/index.d.ts",
15-
"require": "./dist/index.js",
16-
"default": "./dist/index.js"
16+
"import": "./dist/index.mjs",
17+
"require": "./dist/index.cjs"
1718
},
1819
"./package.json": "./package.json"
1920
},
@@ -25,14 +26,17 @@
2526
],
2627
"scripts": {
2728
"build": "bun run build.ts",
28-
"build:verify": "bun run build && bun run smoke-test && bun run smoke-test:dist",
29-
"smoke-test": "bun run smoke-test.ts",
29+
"build:types": "tsc -p tsconfig.build.json",
30+
"build:verify": "bun run build && bun run smoke-test:dist && bun run test:cjs && bun run test:esm",
3031
"smoke-test:dist": "bun run smoke-test-dist.ts",
32+
"test:cjs": "cd test/cjs-compatibility && npm install --silent && npm run test:all",
33+
"test:esm": "cd test/esm-compatibility && npm install --silent && npm run test:all",
3134
"clean": "rm -rf dist",
3235
"prepare-dist": "bun run scripts/publish.ts --dry-run",
3336
"publish-sdk": "bun run scripts/publish.ts --public",
3437
"publish-dry-run": "bun run build:verify && bun run scripts/publish.ts --dry-run",
3538
"prepack": "bun run build",
39+
"prepublishOnly": "bun run build",
3640
"typecheck": "tsc --noEmit -p .",
3741
"test": "bun test"
3842
},

sdk/smoke-test-dist.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async function testCJSDist() {
114114
const testCode = `
115115
try {
116116
// Require from the built CJS dist files
117-
const pkg = require('../dist/index.js');
117+
const pkg = require('../dist/index.cjs');
118118
console.log('CJS dist require successful');
119119
120120
// Verify basic structure
@@ -166,7 +166,7 @@ async function testCJSTreeSitter() {
166166
const runTest = async () => {
167167
try {
168168
// Require tree-sitter functionality from built CJS dist
169-
const { getFileTokenScores, setWasmDir } = require('../dist/index.js');
169+
const { getFileTokenScores, setWasmDir } = require('../dist/index.cjs');
170170
console.log('CJS tree-sitter imports successful');
171171
172172
// Set WASM directory to the correct location for dist tests

sdk/smoke-test.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)