Skip to content

Refactor Leaflet.MarkerCluster initialization to factory-based injection (backwards compatible with global scope init)#1120

Open
sebastianthulin wants to merge 8 commits into
Leaflet:masterfrom
helsingborg-stad:master
Open

Refactor Leaflet.MarkerCluster initialization to factory-based injection (backwards compatible with global scope init)#1120
sebastianthulin wants to merge 8 commits into
Leaflet:masterfrom
helsingborg-stad:master

Conversation

@sebastianthulin

Copy link
Copy Markdown

This pull request refactors the MarkerCluster plugin to use explicit factory and extension functions for Leaflet integration, modernizing the codebase and improving modularity and compatibility. The main entry point is now addMarkerClusterToLeaflet, which takes a Leaflet instance and extends it with clustering, spiderfier, convex hull, refresh, and opacity features. This approach allows better support for ES module usage and tree-shaking, while maintaining backward compatibility for global usage.

Core refactor: modularization and explicit Leaflet extension

  • Introduced addMarkerClusterToLeaflet in src/index.js as the main entry point, which takes a Leaflet instance and adds all MarkerCluster functionality, replacing previous implicit global patching.
  • Refactored all major classes (MarkerClusterGroup, MarkerCluster, DistanceGrid, QuickHull, Spiderfier, Refresh, and MarkerOpacity) to export factory or extension functions instead of directly patching the global L object. [1] [2] [3] [4] [5] [6] [7]

Backward compatibility and ES module support

  • Updated src/index.js and each module to automatically patch the global L object if present, ensuring backward compatibility for users loading scripts via <script> tags. [1] [2] [3]
  • Added the "module" and "exports" fields to package.json to support ES module imports and proper file resolution for modern bundlers.

API consistency and internal usage

  • Updated all internal references to use locally created classes (e.g., MarkerCluster, MarkerClusterGroup) instead of global L properties, ensuring correct class usage in modular contexts. [1] [2] [3] [4] [5]
  • Changed the rollup dependency in package.json to a compatible semver range for easier upgrades.

Copilot AI and others added 8 commits August 28, 2025 06:53
Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
…ttern

Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
…ackage.json

Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
Implement factory function pattern to fix global monkeypatching issues in build environments with proper ES module support
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