#!/usr/bin/env node import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const distPath = path.join(__dirname, '..', 'dist'); console.log('šŸ“¦ Bundle Analysis\n'); function getFileSize(filePath) { const stats = fs.statSync(filePath); const sizeInKB = (stats.size / 1024).toFixed(2); const sizeInBytes = stats.size; return { kb: `${sizeInKB} KB`, bytes: sizeInBytes }; } function analyzeFiles() { if (!fs.existsSync(distPath)) { console.log('āŒ No dist folder found. Run `npm run build` first.'); return; } const files = fs.readdirSync(distPath).filter(f => !f.includes('utils')); console.log('šŸ“Š File Sizes:'); files.forEach(file => { const filePath = path.join(distPath, file); if (fs.statSync(filePath).isFile()) { const size = getFileSize(filePath); const icon = file.endsWith('.esm.js') ? 'šŸ“¦ ESM' : file.endsWith('.js') ? 'šŸ“¦ CJS' : file.endsWith('.d.ts') ? 'šŸ“„ Types' : 'šŸ“„'; console.log(` ${icon}: ${file} - ${size.kb}`); } }); // Calculate totals const jsFiles = files.filter(f => f.endsWith('.js') && !f.includes('.map')); const totalSize = jsFiles.reduce((acc, file) => { const filePath = path.join(distPath, file); const size = getFileSize(filePath); return acc + size.bytes; }, 0); console.log(`\nšŸ’¾ Total JS Size: ${(totalSize / 1024).toFixed(2)} KB`); // Tree shaking effectiveness const esmFiles = files.filter(f => f.endsWith('.esm.js')); const cjsFiles = files.filter(f => f.endsWith('.js') && !f.endsWith('.esm.js') && !f.includes('.map')); console.log(`\n🌳 Tree-shaking ready: ${esmFiles.length} ESM builds`); console.log(`šŸ”§ CommonJS compatible: ${cjsFiles.length} CJS builds`); // Size comparison if (esmFiles.length > 0) { const avgEsmSize = esmFiles.reduce((acc, file) => { const filePath = path.join(distPath, file); return acc + getFileSize(filePath).bytes; }, 0) / esmFiles.length; console.log(`šŸ“ Average ESM build size: ${(avgEsmSize / 1024).toFixed(2)} KB`); } } analyzeFiles();