Skip to content

Conversation

@mattpodwysocki
Copy link
Contributor

Summary

Implements a comprehensive style optimization tool that reduces Mapbox style file size and improves performance by removing redundancies and simplifying structure.

Key Features

  • Remove Unused Sources: Identifies and removes source definitions not referenced by any layer
  • Remove Duplicate Layers: Detects layers with identical properties (excluding ID) and removes duplicates
  • Simplify Expressions: Simplifies boolean logic in filters and paint/layout properties
  • Remove Empty Layers: Removes layers with no visible properties (preserves background layers)
  • Identify Consolidation Opportunities: Finds layers with identical filter expressions

Implementation Details

  • Extends BaseTool for offline operation (no API calls required)
  • Performs deep analysis of style JSON structure
  • Tracks all optimizations with detailed descriptions
  • Calculates size savings and percentage reduction
  • Selective optimization: Apply specific optimizations or all at once
  • Full Zod schema validation for inputs and outputs

Optimizations

Remove Unused Sources

Scans all layers to identify which sources are referenced, then removes any sources not in use.

Remove Duplicate Layers

Compares layers excluding their ID to find exact duplicates and removes them.

Simplify Expressions

Simplifies boolean expressions in filters and properties:

  • ["all", true]true
  • ["any", false]false
  • ["!", false]true
  • ["!", true]false

Remove Empty Layers

Removes layers with no paint or layout properties (background layers are preserved since they're valid even when empty).

Consolidate Filters

Identifies groups of layers with identical filter expressions that could potentially be consolidated.

Testing

  • 21 comprehensive test cases covering:
    • Tool metadata validation
    • Basic optimization with JSON string/object input
    • Remove unused sources (with/without unused sources)
    • Remove duplicate layers (with/without duplicates)
    • Simplify expressions (all simplification rules)
    • Remove empty layers (preserving background layers)
    • Consolidate filters identification
    • Combined optimizations
    • Summary statistics (size savings, percent reduction)
    • Error handling (invalid JSON)
    • Already-optimized styles (zero optimizations)

All tests passing ✅

Documentation

Added comprehensive README documentation with:

  • Parameters and options
  • Description of all optimization types
  • Return value structure with example JSON
  • Example prompts for users

Use Cases

This tool enables:

  • Reducing style file size for faster loading
  • Cleaning up styles after multiple edits/iterations
  • Identifying optimization opportunities
  • Preparing styles for production deployment
  • Understanding style complexity through size metrics

Related PRs

Completes the offline validation tools initiative:

🤖 Generated with Claude Code

Implements a comprehensive style optimization tool that reduces Mapbox
style file size and improves performance by removing redundancies and
simplifying structure.

Key features:
- Remove unused sources not referenced by any layer
- Remove duplicate layers with identical properties
- Simplify boolean expressions (["all", true] → true, etc.)
- Remove empty layers with no visible properties
- Identify layers with identical filters for consolidation

The tool extends BaseTool for offline operation (no API calls).
Performs deep analysis of style JSON to identify optimization
opportunities and tracks all changes with detailed reporting.

Optimizations:
- remove-unused-sources: Identifies and removes unreferenced sources
- remove-duplicate-layers: Detects and removes layers with identical properties (excluding ID)
- simplify-expressions: Simplifies boolean logic in filters and paint/layout properties
- remove-empty-layers: Removes layers with no paint or layout (preserves background layers)
- consolidate-filters: Identifies groups of layers with identical filter expressions

Returns optimized style with detailed report including:
- List of optimizations applied with counts
- Size comparison (original vs optimized in bytes)
- Percentage reduction achieved

Test coverage: 21 comprehensive test cases covering all optimization
types, combined optimizations, size calculations, error handling, and
edge cases including already-optimized styles.

Documentation: Added comprehensive README documentation with
parameters, all optimization types, return value structure, example
JSON output, and example prompts.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@mattpodwysocki mattpodwysocki requested a review from a team as a code owner January 8, 2026 23:11
mattpodwysocki added a commit that referenced this pull request Jan 9, 2026
…roduction prompt

Adds comprehensive style quality validation capabilities:

Skill:
- Created mapbox-style-quality skill document (390+ lines)
- Pre-production checklist and validation best practices
- Guidance on expression validation, GeoJSON validation, and accessibility
- Optimization strategies and workflow recommendations
- Integration patterns for Git hooks, CI/CD, and code review

Prompt:
- Created prepare-style-for-production prompt
- Orchestrates validation workflow using 5 quality tools:
  * validate_expression_tool - Validate expressions in filters/paint/layout
  * validate_geojson_tool - Validate GeoJSON sources
  * check_color_contrast_tool - WCAG accessibility compliance
  * optimize_style_tool - Remove redundancies and optimize
  * compare_styles_tool - Compare versions (implicit in workflow)
- Configurable WCAG level (AA/AAA) and optional optimization skip
- Generates comprehensive quality report with deployment readiness assessment

Testing:
- 15 test cases for PrepareStyleForProductionPrompt
- All 386 tests passing
- Updated prompt registry tests

Documentation:
- Updated README with new skill listing
- Added prompt documentation with usage examples
- Cross-referenced skill and prompt

Related PRs:
- PR #50: validate_geojson_tool
- PR #51: validate_expression_tool
- PR #52: compare_styles_tool
- PR #53: check_color_contrast_tool
- PR #54: optimize_style_tool

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@mattpodwysocki mattpodwysocki merged commit 6a03386 into main Jan 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants