Skip to content

Setting Up Size Guard

What you'll learn

  • Adding the size-guard command 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

Step 1: Generate the Size Guard

bash
archipelago generate size_guard

You will be prompted for:

  • appNameMyApp (must match your monorepo app name)
  • isForMonorepotrue

Or use a config file:

json
{
  "appName": "MyApp",
  "isForMonorepo": true
}
bash
archipelago generate size_guard --config size_guard_config.json

Step 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.yaml

Step 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: true

Step 4: Run Size Guard

bash
monorepo_toolkit size-guard

Output 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-guard

This prevents large files from being merged accidentally — no more 10MB PNGs committed by mistake.

Common Customizations

CustomizationWhere to Change
Adjust thresholdssize_guard.yaml — per-extension limits
Exclude patternssize_guard.yamlexclude list
Custom reportersscripts/size_guard/lib/src/reporter.dart
Warning vs. failurefail_on_violation flag in config
Per-directory rulesAdd directory-scoped thresholds

Next Steps

Built by Banua Coder