Skip to content

Commit 7333145

Browse files
committed
SDK: try only commonjs
1 parent 48fba68 commit 7333145

File tree

5 files changed

+36
-247
lines changed

5 files changed

+36
-247
lines changed

sdk/build.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,10 @@ 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/esm',
34-
target: 'node',
35-
format: 'esm',
36-
sourcemap: 'external',
37-
minify: false,
38-
external,
39-
loader: {
40-
'.scm': 'text',
41-
},
42-
})
43-
4430
console.log('📦 Building CJS format...')
4531
await Bun.build({
4632
entrypoints: ['src/index.ts'],
47-
outdir: 'dist/cjs',
33+
outdir: 'dist',
4834
target: 'node',
4935
format: 'cjs',
5036
sourcemap: 'external',
@@ -66,10 +52,6 @@ async function build() {
6652
console.warn('⚠ TypeScript declaration generation failed, continuing...')
6753
}
6854

69-
console.log('📦 Adding CJS package.json for proper CommonJS detection...')
70-
const cjsPackageJson = JSON.stringify({ type: 'commonjs' }, null, 2)
71-
await Bun.write('dist/cjs/package.json', cjsPackageJson)
72-
7355
console.log('📂 Copying WASM files for tree-sitter...')
7456
await copyWasmFiles()
7557

@@ -83,15 +65,10 @@ async function build() {
8365
* Create colocated declaration files for better TypeScript compatibility
8466
*/
8567
async function createColocatedDeclarations() {
86-
// Create colocated ESM declaration file
87-
const esmDeclaration = 'export * from "./sdk/src/index";\n'
88-
await Bun.write('dist/esm/index.d.ts', esmDeclaration)
89-
console.log(' ✓ Created dist/esm/index.d.ts')
90-
91-
// Create colocated CJS declaration file for extra compatibility
92-
const cjsDeclaration = 'export * from "../esm/index";\n'
93-
await Bun.write('dist/cjs/index.d.ts', cjsDeclaration)
94-
console.log(' ✓ Created dist/cjs/index.d.ts')
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')
9572
}
9673

9774
/**

sdk/package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
"name": "@codebuff/sdk",
33
"private": false,
44
"access": "public",
5-
"version": "0.1.30",
5+
"version": "0.1.31",
66
"description": "Official SDK for Codebuff — AI coding agent & framework",
77
"license": "Apache-2.0",
8-
"type": "module",
9-
"main": "./dist/cjs/index.js",
10-
"module": "./dist/esm/index.js",
11-
"types": "./dist/esm/index.d.ts",
12-
"typings": "./dist/esm/index.d.ts",
8+
"type": "commonjs",
9+
"main": "./dist/index.js",
10+
"types": "./dist/index.d.ts",
11+
"typings": "./dist/index.d.ts",
1312
"exports": {
1413
".": {
15-
"types": "./dist/esm/index.d.ts",
16-
"import": "./dist/esm/index.js",
17-
"require": "./dist/cjs/index.js",
18-
"default": "./dist/esm/index.js"
14+
"types": "./dist/index.d.ts",
15+
"require": "./dist/index.js",
16+
"default": "./dist/index.js"
1917
},
2018
"./package.json": "./package.json"
2119
},

sdk/smoke-test-dist.ts

Lines changed: 6 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Smoke test script to verify compiled dist builds work correctly with Node.js
1+
// Smoke test script to verify compiled CJS dist build works correctly with Node.js
22
// This ensures the actual published artifacts function properly
33

44
import { execSync } from 'child_process'
@@ -51,15 +51,9 @@ async function runDistSmokeTests() {
5151
// Create package.json and install dependencies for proper Node.js environment
5252
await setupTestEnvironment()
5353

54-
// Test ESM dist import
55-
await testESMDist()
56-
5754
// Test CJS dist require
5855
await testCJSDist()
5956

60-
// Test ESM tree-sitter functionality
61-
await testESMTreeSitter()
62-
6357
// Test CJS tree-sitter functionality
6458
await testCJSTreeSitter()
6559

@@ -91,7 +85,7 @@ async function setupTestEnvironment() {
9185
const testPackageJson = {
9286
name: 'sdk-dist-test',
9387
version: '1.0.0',
94-
type: 'module',
88+
type: 'commonjs',
9589
dependencies: {
9690
'web-tree-sitter': '^0.25.6',
9791
// Add other dependencies that the built dist files need
@@ -113,65 +107,14 @@ async function setupTestEnvironment() {
113107
}
114108
}
115109

116-
async function testESMDist() {
117-
console.log(' Testing ESM dist import with Node.js...')
118-
119-
const testFile = join(testDir, 'test-esm-dist.mjs')
120-
const testCode = `
121-
try {
122-
// Import from the built ESM dist files
123-
const pkg = await import('../dist/esm/index.js');
124-
console.log('ESM dist import successful');
125-
126-
// Verify basic structure
127-
if (typeof pkg === 'object' && pkg !== null) {
128-
const exportKeys = Object.keys(pkg);
129-
console.log('Package exports found:', exportKeys.length, 'exports');
130-
131-
// Check for expected exports
132-
const expectedExports = ['SDK_VERSION', 'SDK_NAME', 'getFileTokenScores', 'setWasmDir'];
133-
const foundExports = expectedExports.filter(exp => exp in pkg);
134-
135-
if (foundExports.length >= 2) {
136-
console.log('✅ ESM dist has expected exports:', foundExports.join(', '));
137-
process.exit(0);
138-
} else {
139-
console.error('❌ Missing expected exports. Found:', foundExports.join(', '));
140-
process.exit(1);
141-
}
142-
} else {
143-
console.error('❌ Package is not an object:', typeof pkg);
144-
process.exit(1);
145-
}
146-
} catch (error) {
147-
console.error('❌ ESM dist import failed:', error.message);
148-
process.exit(1);
149-
}
150-
`
151-
152-
writeFileSync(testFile, testCode)
153-
154-
try {
155-
execSync(`node test-esm-dist.mjs`, { stdio: 'pipe', cwd: testDir })
156-
testResults.push({ format: 'ESM', test: 'Basic Import', success: true })
157-
} catch (error: any) {
158-
testResults.push({
159-
format: 'ESM',
160-
test: 'Basic Import',
161-
success: false,
162-
error: error.message || 'Unknown error',
163-
})
164-
}
165-
}
166-
167110
async function testCJSDist() {
168111
console.log(' Testing CJS dist require with Node.js...')
169112

170113
const testFile = join(testDir, 'test-cjs-dist.cjs')
171114
const testCode = `
172115
try {
173116
// Require from the built CJS dist files
174-
const pkg = require('../dist/cjs/index.js');
117+
const pkg = require('../dist/index.js');
175118
console.log('CJS dist require successful');
176119
177120
// Verify basic structure
@@ -215,85 +158,6 @@ try {
215158
}
216159
}
217160

218-
async function testESMTreeSitter() {
219-
console.log(' Testing ESM dist tree-sitter functionality...')
220-
221-
const testFile = join(testDir, 'test-esm-treesitter.mjs')
222-
const testCode = `
223-
try {
224-
// Import tree-sitter functionality from built ESM dist
225-
const { getFileTokenScores, setWasmDir } = await import('../dist/esm/index.js');
226-
console.log('ESM tree-sitter imports successful');
227-
228-
// Set WASM directory to the correct location for dist tests
229-
const path = await import('path');
230-
const { fileURLToPath } = await import('url');
231-
232-
// Set WASM directory to the built dist location
233-
setWasmDir(path.join(process.cwd(), '..', 'dist', 'esm', 'wasm'));
234-
console.log('✅ setWasmDir function works');
235-
236-
// Test getFileTokenScores with sample TypeScript code
237-
const projectFiles = {
238-
'test.ts': \`${sampleCode.replace(/`/g, '\\`')}\`
239-
};
240-
241-
const tokenData = await getFileTokenScores(
242-
process.cwd(),
243-
['test.ts'],
244-
(filePath) => projectFiles[filePath] || null
245-
);
246-
247-
const { tokenScores } = tokenData;
248-
249-
if (!tokenScores['test.ts']) {
250-
throw new Error('No token scores found for test.ts');
251-
}
252-
253-
const tokens = Object.keys(tokenScores['test.ts']);
254-
console.log(\`✅ Found \${tokens.length} tokens in TypeScript file\`);
255-
256-
// Check for expected tokens
257-
const expectedTokens = ['calculateSum', 'Calculator', 'add', 'getHistory'];
258-
const foundTokens = expectedTokens.filter(token => tokens.includes(token));
259-
260-
if (foundTokens.length > 0) {
261-
console.log(\`✅ Found expected tokens: \${foundTokens.join(', ')}\`);
262-
process.exit(0);
263-
} else {
264-
console.warn('⚠ Warning: No expected tokens found. Found tokens:', tokens.slice(0, 10));
265-
process.exit(1);
266-
}
267-
268-
} catch (error) {
269-
console.error('❌ ESM tree-sitter test failed:', error.message);
270-
process.exit(1);
271-
}
272-
`
273-
274-
writeFileSync(testFile, testCode)
275-
276-
try {
277-
const result = execSync(`node test-esm-treesitter.mjs`, {
278-
stdio: 'pipe',
279-
cwd: testDir,
280-
timeout: 30000,
281-
encoding: 'utf8'
282-
})
283-
console.log('ESM TreeSitter test output:', result)
284-
testResults.push({ format: 'ESM', test: 'Tree-sitter', success: true })
285-
} catch (error: any) {
286-
console.log('ESM TreeSitter test stderr:', error.stderr)
287-
console.log('ESM TreeSitter test stdout:', error.stdout)
288-
testResults.push({
289-
format: 'ESM',
290-
test: 'Tree-sitter',
291-
success: false,
292-
error: error.message || 'Unknown error',
293-
})
294-
}
295-
}
296-
297161
async function testCJSTreeSitter() {
298162
console.log(' Testing CJS dist tree-sitter functionality...')
299163

@@ -302,14 +166,14 @@ async function testCJSTreeSitter() {
302166
const runTest = async () => {
303167
try {
304168
// Require tree-sitter functionality from built CJS dist
305-
const { getFileTokenScores, setWasmDir } = require('../dist/cjs/index.js');
169+
const { getFileTokenScores, setWasmDir } = require('../dist/index.js');
306170
console.log('CJS tree-sitter imports successful');
307171
308172
// Set WASM directory to the correct location for dist tests
309173
const path = require('path');
310174
311175
// Set WASM directory to the built dist location
312-
setWasmDir(path.join(process.cwd(), '..', 'dist', 'cjs', 'wasm'));
176+
setWasmDir(path.join(process.cwd(), '..', 'dist', 'wasm'));
313177
console.log('✅ setWasmDir function works');
314178
315179
// Test getFileTokenScores with sample TypeScript code
@@ -360,7 +224,7 @@ runTest();
360224
stdio: 'pipe',
361225
cwd: testDir,
362226
timeout: 30000,
363-
encoding: 'utf8'
227+
encoding: 'utf8',
364228
})
365229
console.log('CJS TreeSitter test output:', result)
366230
testResults.push({ format: 'CJS', test: 'Tree-sitter', success: true })

0 commit comments

Comments
 (0)