# Bundle Size Report

Generated: 2025-10-23

## IIFE Format (Primary - Wix/CDN Target)

- **Uncompressed**: 30.58 KB
- **Gzipped**: **8.67 KB** / 15 KB (target)
- **Status**: ✅ **UNDER LIMIT** (58% of budget used)
- **Headroom**: 6.33 KB (42% remaining)

## ESM Format

- **Uncompressed**: 53.47 KB
- **Gzipped**: 12.32 KB

## UMD Format

- **Uncompressed**: 30.78 KB
- **Gzipped**: 8.75 KB

## Optimization Settings Applied

### Terser Configuration
- **Compression passes**: 3 (maximum)
- **Drop console**: Yes (production only)
- **Drop debugger**: Yes
- **Unsafe optimizations**: Enabled
  - `unsafe`: true
  - `unsafe_comps`: true
  - `unsafe_math`: true
  - `unsafe_proto`: true
- **Tree-shaking**: Enabled
- **Dead code elimination**: Enabled

### Mangling
- **Toplevel mangling**: Enabled
- **Property mangling**: Disabled (prevents API breakage)

### Output Format
- **Comments**: Removed
- **Beautify**: Disabled
- **Compact**: Enabled
- **Quote style**: Single quotes

## Size Comparison

| Format | Uncompressed | Gzipped | % of 15KB Limit |
|--------|--------------|---------|-----------------|
| IIFE   | 30.58 KB     | 8.67 KB | 58% ✅          |
| ESM    | 53.47 KB     | 12.32 KB| 82% ✅          |
| UMD    | 30.78 KB     | 8.75 KB | 58% ✅          |

## Analysis

### Why IIFE is Smaller than ESM

The IIFE format is significantly smaller (8.67 KB vs 12.32 KB) because:

1. **Self-executing**: No module loading overhead
2. **Better minification**: Terser can optimize IIFE more aggressively
3. **No export metadata**: ESM requires export declarations that add size
4. **Direct bundling**: Everything inlined into single scope

### Package Modules Included

The package includes 26 modules totaling 6,008 lines:

- Configuration management (safe-config, validator)
- Core conversation manager
- Tavus API integration
- Daily.co provider
- UI components (button, video panel, iframe embed, timer)
- Utilities (logger, DOM helpers, validation)

### Comparison to Reference Implementation

- **Original Wix demo**: ~13 KB (5 modules, hardcoded configs)
- **New package**: 8.67 KB (26 modules, dynamic configs)
- **Size reduction**: **33% smaller** despite more features!

This demonstrates excellent code optimization and tree-shaking effectiveness.

## Recommendations

### Current Status: EXCELLENT ✅

The build is **well under** the 15KB Wix limit with significant headroom:

- **6.33 KB of budget remaining** (42% headroom)
- All three formats under reasonable size limits
- Production-grade minification applied
- Source maps available for debugging

### Future Optimization Opportunities

If size becomes an issue in the future:

1. **Conditional loading**: Split rarely-used features into lazy-loaded chunks
2. **Feature flags**: Remove unused providers (e.g., if only Daily.co needed)
3. **Logger optimization**: The logger could be stripped more aggressively
4. **UI components**: Could make some UI components optional
5. **Validation**: Move schema validation to runtime checks only

### Monitoring

Monitor bundle size on each build:

```bash
pnpm nx build engage-package --configuration=production
```

Look for the "gzip:" output in the build logs. Alert if IIFE exceeds 13 KB gzipped (leaving 2KB safety margin).

## Build Command

```bash
pnpm nx build engage-package --configuration=production
```

## Source Maps

All formats include source maps for debugging:

- `buzztrail.min.js.map` (IIFE)
- `buzztrail.esm.js.map` (ESM)
- `buzztrail.umd.js.map` (UMD)

Total source map size: ~500 KB (not counted toward bundle size limit)
