Skip to content

Commit

Permalink
fix(google-maps): error when minified through closure (#24897)
Browse files Browse the repository at this point in the history
In #24865 the access of the global `MarkerClusterer` class changed to go through the `window`. This causes a runtime error for apps minified through Closure, because the name is no longer minified and it doesn't align with the minified class name on the `window`. These changes move around our code so that we reference the clusterer without going through the `window`.

(cherry picked from commit 8840b3c)
  • Loading branch information
crisbeto committed May 8, 2022
1 parent e3e6cf4 commit 9f27303
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/google-maps/map-marker-clusterer/map-marker-clusterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ import {
Calculator,
Cluster,
ClusterIconStyle,
MarkerClusterer,
MarkerClusterer as MarkerClustererInstance,
MarkerClustererOptions,
} from './marker-clusterer-types';

/** Default options for a clusterer. */
const DEFAULT_CLUSTERER_OPTIONS: MarkerClustererOptions = {};

/**
* The clusterer has to be defined and referred to as a global variable,
* otherwise it'll cause issues when minified through Closure.
*/
declare const MarkerClusterer: typeof MarkerClustererInstance;

/**
* Angular component for implementing a Google Maps Marker Clusterer.
*
Expand Down Expand Up @@ -197,19 +203,18 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
* googlemaps.github.io/v3-utility-library/classes/
* _google_markerclustererplus.markerclusterer.html
*/
markerClusterer?: MarkerClusterer;
markerClusterer?: MarkerClustererInstance;

constructor(private readonly _googleMap: GoogleMap, private readonly _ngZone: NgZone) {
this._canInitialize = this._googleMap._isBrowser;
}

ngOnInit() {
if (this._canInitialize) {
const clustererWindow = window as unknown as typeof globalThis & {
MarkerClusterer?: typeof MarkerClusterer;
};

if (!clustererWindow.MarkerClusterer && (typeof ngDevMode === 'undefined' || ngDevMode)) {
if (
typeof MarkerClusterer !== 'function' &&
(typeof ngDevMode === 'undefined' || ngDevMode)
) {
throw Error(
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
'Please install the MarkerClustererPlus library: ' +
Expand All @@ -221,7 +226,7 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this.markerClusterer = new clustererWindow.MarkerClusterer!(
this.markerClusterer = new MarkerClusterer(
this._googleMap.googleMap!,
[],
this._combineOptions(),
Expand Down Expand Up @@ -495,7 +500,7 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
.map(markerComponent => markerComponent.marker!);
}

private _assertInitialized(): asserts this is {markerClusterer: MarkerClusterer} {
private _assertInitialized(): asserts this is {markerClusterer: MarkerClustererInstance} {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (!this._googleMap.googleMap) {
throw Error(
Expand Down

0 comments on commit 9f27303

Please sign in to comment.