All files / scripts analyze-bundle.mjs

0% Statements 0/92
0% Branches 0/1
0% Functions 0/1
0% Lines 0/92

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                                                                                                                                                                                                                 
import { build } from 'esbuild';
import { writeFile } from 'fs/promises';
import path from 'path';
 
/**
 * Bundle analyzer for Coherent.js packages
 */
export async function analyzeBundles() {
  const packages = [
    'core', 'api', 'database', 'client', 
    'express', 'fastify', 'koa', 'nextjs'
  ];
 
  const results = {};
 
  for (const pkg of packages) {
    console.log(`šŸ“Š Analyzing ${pkg} bundle...`);
    
    try {
      const result = await build({
        entryPoints: [`packages/${pkg}/dist/index.js`],
        bundle: true,
        minify: true,
        write: false,
        metafile: true,
        format: 'esm',
        platform: pkg === 'client' ? 'browser' : 'node',
      });
 
      results[pkg] = {
        size: result.outputFiles[0].contents.length,
        sizeKB: Math.round(result.outputFiles[0].contents.length / 1024 * 100) / 100,
        metafile: result.metafile
      };
 
      console.log(`  šŸ“ Size: ${results[pkg].sizeKB}KB`);
    } catch (error) {
      console.warn(`  āš ļø  Could not analyze ${pkg}: ${error.message}`);
      results[pkg] = { error: error.message };
    }
  }
 
  // Generate bundle report
  const report = {
    timestamp: new Date().toISOString(),
    packages: results,
    summary: {
      totalSize: Object.values(results)
        .filter(r => !r.error)
        .reduce((sum, r) => sum + r.size, 0),
      largestPackage: Object.entries(results)
        .filter(([_, r]) => !r.error)
        .sort(([_, a], [__, b]) => b.size - a.size)[0]?.[0],
      recommendations: generateRecommendations(results)
    }
  };
 
  await writeFile(
    'bundle-analysis.json', 
    JSON.stringify(report, null, 2)
  );
 
  console.log('\nšŸ“Š Bundle Analysis Summary:');
  console.log(`Total bundle size: ${Math.round(report.summary.totalSize / 1024 * 100) / 100}KB`);
  console.log(`Largest package: ${report.summary.largestPackage}`);
  
  if (report.summary.recommendations.length > 0) {
    console.log('\nšŸ’” Recommendations:');
    report.summary.recommendations.forEach(rec => {
      console.log(`  - ${rec}`);
    });
  }
 
  return report;
}
 
/**
 * Generate performance recommendations
 */
function generateRecommendations(results) {
  const recommendations = [];
  
  Object.entries(results).forEach(([pkg, data]) => {
    if (data.error) return;
    
    if (data.sizeKB > 100) {
      recommendations.push(`Consider reducing ${pkg} package size (${data.sizeKB}KB)`);
    }
    
    if (pkg === 'core' && data.sizeKB > 50) {
      recommendations.push(`Core package is large (${data.sizeKB}KB) - consider splitting features`);
    }
    
    if (pkg === 'client' && data.sizeKB > 30) {
      recommendations.push(`Client package is large (${data.sizeKB}KB) - optimize for browser`);
    }
  });
  
  return recommendations;
}
 
// Run analysis if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
  analyzeBundles().catch(console.error);
}