Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | import { build } from 'esbuild'; import { readFile, writeFile } from 'fs/promises'; import { spawn } from 'child_process'; import path from 'path'; /** * Shared build configuration for all packages */ export const commonConfig = { bundle: true, platform: 'node', target: 'node20', sourcemap: true, treeShaking: true, minify: process.env.NODE_ENV === 'production', define: { 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'), }, external: [ // Common externals that should not be bundled 'express', 'fastify', 'koa', 'next', 'react', 'react-dom', 'sqlite3', 'mysql2', 'pg', 'mongodb', ], }; /** * Build a package with both ESM and CJS formats */ export async function buildPackage({ packageName, entryPoint, outDir = 'dist', external = [], additionalConfig = {}, formats = ['esm', 'cjs'] // Allow specifying which formats to build }) { const config = { ...commonConfig, external: [...commonConfig.external, ...external], ...additionalConfig }; console.log(`🏗️ Building ${packageName}...`); // Build ESM version if (formats.includes('esm')) { await build({ ...config, entryPoints: [entryPoint], format: 'esm', outfile: `${outDir}/index.js`, }); } // Build CJS version (only for Node.js packages) if (formats.includes('cjs')) { await build({ ...config, entryPoints: [entryPoint], format: 'cjs', outfile: `${outDir}/index.cjs`, }); } console.log(`✅ Built ${packageName} successfully`); } /** * Generate TypeScript declarations for a package */ export async function generateDeclarations(packagePath) { return new Promise((resolve, reject) => { const process = spawn('tsc', ['--build', packagePath], { stdio: 'inherit', cwd: path.resolve('.') }); process.on('close', (code) => { if (code === 0) { console.log(`✅ Generated TypeScript declarations for ${packagePath}`); resolve(); } else { reject(new Error(`TypeScript compilation failed with code ${code}`)); } }); }); } /** * Build browser package with different configuration */ export async function buildBrowserPackage({ packageName, entryPoint, outDir = 'dist', minify = process.env.NODE_ENV === 'production' }) { console.log(`🏗️ Building browser package ${packageName}...`); const result = await build({ entryPoints: [entryPoint], bundle: true, platform: 'browser', target: 'es2020', sourcemap: true, minify, treeShaking: true, format: 'esm', outfile: `${outDir}/index.js`, metafile: true, define: { 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'), }, }); // Log bundle size info if (result.metafile) { const outputs = Object.entries(result.metafile.outputs); const mainOutput = outputs.find(([path]) => path.endsWith('index.js')); if (mainOutput) { const sizeKB = Math.round(mainOutput[1].bytes / 1024 * 100) / 100; console.log(` 📏 Bundle size: ${sizeKB}KB`); } } console.log(`✅ Built browser package ${packageName} successfully`); } /** * Generate package.json for built packages with correct exports */ export async function generatePackageExports(packagePath) { const packageJsonPath = path.join(packagePath, 'package.json'); const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8')); // Ensure consistent exports configuration packageJson.exports = { ".": { "import": "./dist/index.js", "require": "./dist/index.cjs", "types": "./dist/index.d.ts" } }; // Ensure files array includes dist if (!packageJson.files) { packageJson.files = []; } if (!packageJson.files.includes('dist/')) { packageJson.files.unshift('dist/'); } await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n'); } /** * Build all packages in dependency order */ export async function buildAll() { const buildOrder = [ 'core', 'api', 'database', 'client', 'express', 'fastify', 'koa', 'nextjs' ]; // First pass: Build JavaScript bundles for (const pkg of buildOrder) { try { const packagePath = `packages/${pkg}`; if (pkg === 'client') { await buildBrowserPackage({ packageName: `@coherentjs/${pkg}`, entryPoint: getEntryPoint(pkg), }); } else { await buildPackage({ packageName: `@coherentjs/${pkg}`, entryPoint: getEntryPoint(pkg), external: getPackageExternals(pkg) }); } await generatePackageExports(packagePath); } catch (error) { console.error(`❌ Failed to build ${pkg}:`, error); process.exit(1); } } // Second pass: Generate TypeScript declarations console.log('🔧 Generating TypeScript declarations...'); try { await generateDeclarations('.'); } catch (error) { console.warn('⚠️ TypeScript declaration generation failed:', error.message); console.log('📝 Continuing without type declarations...'); } console.log('🎉 All packages built successfully!'); } /** * Get entry point for each package */ function getEntryPoint(packageName) { const entryPoints = { 'core': '../../src/coherent.js', 'api': '../../src/api/index.js', 'database': '../../src/database/index.js', 'client': '../../src/client/hydration.js', 'express': '../../src/express/index.js', 'fastify': '../../src/fastify/index.js', 'koa': '../../src/koa/index.js', 'nextjs': '../../src/nextjs/index.js' }; return entryPoints[packageName] || `../../src/${packageName}/index.js`; } /** * Get package-specific externals */ function getPackageExternals(packageName) { const externals = { 'core': [], 'api': ['@coherentjs/core'], 'database': ['@coherentjs/core'], 'client': ['@coherentjs/core'], 'express': ['@coherentjs/core'], 'fastify': ['@coherentjs/core'], 'koa': ['@coherentjs/core'], 'nextjs': ['@coherentjs/core'] }; return externals[packageName] || []; } // If run directly, build all packages if (import.meta.url === `file://${process.argv[1]}`) { buildAll().catch(console.error); } |