This commit implements a chunk extension optimization that reduces the number of
output files by merging shared modules into entry chunks when appropriate.
## What's Implemented
### 1. Chunk Extension Optimization
When `preserveEntrySignatures` is set to "allow-extension" (default), shared
modules can be merged into existing entry chunks rather than creating separate
common chunks. This reduces the total number of files that need to be loaded.
Example results:
- With "strict": 2 entry files + 1 common chunk = 3 files
- With "allow-extension": 2 entry files (shared code merged) = 2 files
### 2. preserveEntrySignatures Option
Added a new bundler option with four modes:
- `"strict"`: Prevents chunk extension, maintains exact entry signatures
- `"allow-extension"`: Allows merging shared modules into entries (default)
- `"exports-only"`: Preserves only explicit exports while allowing optimization
- `"false"`: Maximum optimization with no restrictions
### 3. Advanced Chunking API (Structure Only)
Added the API structure for future advanced chunking features:
```javascript
await Bun.build({
entrypoints: ["./app.js"],
splitting: true,
advancedChunks: {
minShareCount: 2,
minSize: 1024,
maxSize: 512000,
groups: [{
name: "vendor",
test: "node_modules",
priority: 10,
type: "javascript"
}]
}
});
```
Note: The advanced chunking configuration is parsed and validated but the
actual chunking logic based on these rules is not yet implemented.
## Technical Details
### Implementation
- Two-phase chunk assignment: collect candidates, then optimize placement
- BitSet-based tracking of which entry points can reach each module
- Module uniqueness enforcement (each module belongs to exactly one chunk)
- Debug-only verification with zero production overhead
### Testing
- 19 tests covering the optimization and edge cases
- Verified backward compatibility
- All tests passing
## Usage
```bash
# CLI
bun build app.js admin.js --splitting --preserve-entry-signatures=allow-extension
# API
await Bun.build({
entrypoints: ["./app.js", "./admin.js"],
splitting: true,
preserveEntrySignatures: "allow-extension"
});
```
This optimization can reduce the number of HTTP requests needed to load a
bundled application by intelligently merging shared code into entry chunks.