Setting Up Size Guard
What you'll learn
- Adding the
size-guardcommand to your monorepo toolkit - Configuring file size thresholds per file type
- Running size checks in CI to prevent bloat
- Handling threshold violations and exceptions
Prerequisites
- An existing Archipelago monorepo (see Monorepo Scaffolding)
- Monorepo Toolkit already generated (see Monorepo Toolkit Setup)
Step 1: Generate the Size Guard
bash
archipelago generate size_guardYou will be prompted for:
- appName —
MyApp(must match your monorepo app name) - isForMonorepo —
true
Or use a config file:
json
{
"appName": "MyApp",
"isForMonorepo": true
}bash
archipelago generate size_guard --config size_guard_config.jsonStep 2: Understand the Generated Structure
The Size Guard adds a command to your existing monorepo toolkit:
devtools/
├── monorepo_toolkit/
│ └── lib/src/commands/
│ └── size_guard_command.dart # New CLI command
└── scripts/
└── size_guard/
├── lib/src/
│ ├── size_guard.dart # Core logic
│ ├── config.dart # Threshold configuration
│ └── reporter.dart # Violation reporting
└── pubspec.yamlStep 3: Configure Thresholds
Create a size_guard.yaml at your monorepo root:
yaml
# size_guard.yaml
thresholds:
"*.dart": 500KB
"*.json": 1MB
"*.png": 200KB
"*.svg": 50KB
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "**/build/**"
fail_on_violation: trueStep 4: Run Size Guard
bash
monorepo_toolkit size-guardOutput example:
Scanning workspace...
VIOLATION: lib/src/data/large_json.json (2.3MB > 1MB threshold)
VIOLATION: assets/hero_image.png (450KB > 200KB threshold)
2 violations found. Build will fail.Step 5: CI Integration
Add size guard to your PR checks:
yaml
- name: Check file sizes
run: |
dart pub global activate --source path devtools/monorepo_toolkit
monorepo_toolkit size-guardThis prevents large files from being merged accidentally — no more 10MB PNGs committed by mistake.
Common Customizations
| Customization | Where to Change |
|---|---|
| Adjust thresholds | size_guard.yaml — per-extension limits |
| Exclude patterns | size_guard.yaml — exclude list |
| Custom reporters | scripts/size_guard/lib/src/reporter.dart |
| Warning vs. failure | fail_on_violation flag in config |
| Per-directory rules | Add directory-scoped thresholds |
Next Steps
- Set up the Monorepo Toolkit if you haven't already
- Configure CI to run size guard on every PR