Skip to content

fix(google-maps): error when minified through closure #24897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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