Skip to content

Commit 9f27303

Browse files
committed
fix(google-maps): error when minified through closure (#24897)
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)
1 parent e3e6cf4 commit 9f27303

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/google-maps/map-marker-clusterer/map-marker-clusterer.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ import {
3535
Calculator,
3636
Cluster,
3737
ClusterIconStyle,
38-
MarkerClusterer,
38+
MarkerClusterer as MarkerClustererInstance,
3939
MarkerClustererOptions,
4040
} from './marker-clusterer-types';
4141

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

45+
/**
46+
* The clusterer has to be defined and referred to as a global variable,
47+
* otherwise it'll cause issues when minified through Closure.
48+
*/
49+
declare const MarkerClusterer: typeof MarkerClustererInstance;
50+
4551
/**
4652
* Angular component for implementing a Google Maps Marker Clusterer.
4753
*
@@ -197,19 +203,18 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
197203
* googlemaps.github.io/v3-utility-library/classes/
198204
* _google_markerclustererplus.markerclusterer.html
199205
*/
200-
markerClusterer?: MarkerClusterer;
206+
markerClusterer?: MarkerClustererInstance;
201207

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

206212
ngOnInit() {
207213
if (this._canInitialize) {
208-
const clustererWindow = window as unknown as typeof globalThis & {
209-
MarkerClusterer?: typeof MarkerClusterer;
210-
};
211-
212-
if (!clustererWindow.MarkerClusterer && (typeof ngDevMode === 'undefined' || ngDevMode)) {
214+
if (
215+
typeof MarkerClusterer !== 'function' &&
216+
(typeof ngDevMode === 'undefined' || ngDevMode)
217+
) {
213218
throw Error(
214219
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
215220
'Please install the MarkerClustererPlus library: ' +
@@ -221,7 +226,7 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
221226
// We'll bring it back in inside the `MapEventManager` only for the events that the
222227
// user has subscribed to.
223228
this._ngZone.runOutsideAngular(() => {
224-
this.markerClusterer = new clustererWindow.MarkerClusterer!(
229+
this.markerClusterer = new MarkerClusterer(
225230
this._googleMap.googleMap!,
226231
[],
227232
this._combineOptions(),
@@ -495,7 +500,7 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
495500
.map(markerComponent => markerComponent.marker!);
496501
}
497502

498-
private _assertInitialized(): asserts this is {markerClusterer: MarkerClusterer} {
503+
private _assertInitialized(): asserts this is {markerClusterer: MarkerClustererInstance} {
499504
if (typeof ngDevMode === 'undefined' || ngDevMode) {
500505
if (!this._googleMap.googleMap) {
501506
throw Error(

0 commit comments

Comments
 (0)