|
| 1 | +// build.js |
| 2 | +import {build} from 'esbuild'; |
| 3 | +import {exec} from 'child_process'; |
| 4 | +import {promisify} from 'util'; |
| 5 | +import fs from 'fs'; |
| 6 | + |
| 7 | +const execAsync = promisify(exec); |
| 8 | + |
| 9 | +// Common build options |
| 10 | +const commonOptions = { |
| 11 | + entryPoints: ['src/index.ts'], |
| 12 | + bundle: true, |
| 13 | + platform: 'node', |
| 14 | + // External dependencies that shouldn't be bundled |
| 15 | + external: ['express', 'cors', '@brionmario-experimental/mcp-node'], |
| 16 | + sourcemap: true, |
| 17 | + minify: true, |
| 18 | + target: 'node18', // Target Node.js version |
| 19 | +}; |
| 20 | + |
| 21 | +// Build ESM version |
| 22 | +async function buildESM() { |
| 23 | + await build({ |
| 24 | + ...commonOptions, |
| 25 | + outfile: 'dist/index.js', |
| 26 | + format: 'esm', |
| 27 | + }); |
| 28 | + console.log('✅ ESM build complete'); |
| 29 | +} |
| 30 | + |
| 31 | +// Build CommonJS version |
| 32 | +async function buildCJS() { |
| 33 | + await build({ |
| 34 | + ...commonOptions, |
| 35 | + outfile: 'dist/cjs/index.js', |
| 36 | + format: 'cjs', |
| 37 | + }); |
| 38 | + |
| 39 | + // Create a package.json for the CJS directory to specify type |
| 40 | + fs.mkdirSync('dist/cjs', {recursive: true}); |
| 41 | + fs.writeFileSync('dist/cjs/package.json', JSON.stringify({type: 'commonjs'}, null, 2)); |
| 42 | + console.log('✅ CJS build complete'); |
| 43 | +} |
| 44 | + |
| 45 | +// Generate TypeScript declaration files |
| 46 | +async function generateTypes() { |
| 47 | + try { |
| 48 | + // Using the lib config to generate declarations |
| 49 | + await execAsync('tsc -p tsconfig.lib.json --emitDeclarationOnly'); |
| 50 | + console.log('✅ TypeScript declarations generated'); |
| 51 | + } catch (error) { |
| 52 | + console.error('❌ Error generating TypeScript declarations:', error); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Clean previous build |
| 58 | +async function clean() { |
| 59 | + try { |
| 60 | + await execAsync('rm -rf dist'); |
| 61 | + console.log('✅ Previous build cleaned'); |
| 62 | + } catch (error) { |
| 63 | + console.error('❌ Error cleaning previous build:', error); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Main build function |
| 68 | +async function runBuild() { |
| 69 | + try { |
| 70 | + console.log('🚀 Starting build process...'); |
| 71 | + |
| 72 | + await clean(); |
| 73 | + await Promise.all([buildESM(), buildCJS()]); |
| 74 | + await generateTypes(); |
| 75 | + |
| 76 | + console.log('✅ Build completed successfully!'); |
| 77 | + } catch (error) { |
| 78 | + console.error('❌ Build failed:', error); |
| 79 | + process.exit(1); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +runBuild(); |
0 commit comments