Skip to content

Commit 2969c31

Browse files
committed
Fix tree-shake issue
1 parent 4c8a937 commit 2969c31

7 files changed

Lines changed: 42 additions & 8 deletions

File tree

examples/basic/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/// <reference path="../../src/types.d.ts" />
22

33
import maplibregl from 'maplibre-gl';
4-
import '../../src/index'; // Import to extend Map.prototype
4+
import { extendMapPrototype } from '../../src/index';
55
import 'maplibre-gl/dist/maplibre-gl.css';
6+
7+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
8+
extendMapPrototype();
69
import { LayerControl } from 'maplibre-gl-layer-control';
710
import 'maplibre-gl-layer-control/style.css';
811
import type { BasemapName } from '../../src/lib/basemaps/types';

examples/cog-simple/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
/// <reference path="../../src/types.d.ts" />
99

1010
import maplibregl from 'maplibre-gl';
11-
import '../../src/index'; // Import to extend Map.prototype
11+
import { extendMapPrototype } from '../../src/index';
1212
import { COGLayerAdapter } from '../../src/lib/layers';
1313
import 'maplibre-gl/dist/maplibre-gl.css';
14+
15+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
16+
extendMapPrototype();
1417
import { LayerControl } from 'maplibre-gl-layer-control';
1518
import 'maplibre-gl-layer-control/style.css';
1619

examples/cog/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/// <reference path="../../src/types.d.ts" />
22

33
import maplibregl from 'maplibre-gl';
4-
import '../../src/index'; // Import to extend Map.prototype
4+
import { extendMapPrototype } from '../../src/index';
55
import { COGLayerAdapter } from '../../src/lib/layers';
66
import 'maplibre-gl/dist/maplibre-gl.css';
7+
8+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
9+
extendMapPrototype();
710
import { LayerControl } from 'maplibre-gl-layer-control';
811
import 'maplibre-gl-layer-control/style.css';
912

examples/react/main.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import React, { useState, useEffect, useRef } from 'react';
44
import { createRoot } from 'react-dom/client';
55
import maplibregl, { Map } from 'maplibre-gl';
66
import 'maplibre-gl/dist/maplibre-gl.css';
7-
import { MapExtendProvider, useMapExtend } from '../../src/react';
7+
import { MapExtendProvider, useMapExtend, extendMapPrototype } from '../../src/react';
88
import type { BasemapName } from '../../src/lib/basemaps/types';
99

10+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
11+
extendMapPrototype();
12+
1013
// Sample GeoJSON data
1114
const sampleGeojson = {
1215
type: 'FeatureCollection' as const,

examples/zarr-simple/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
/// <reference path="../../src/types.d.ts" />
99

1010
import maplibregl from 'maplibre-gl';
11-
import '../../src/index'; // Import to extend Map.prototype
11+
import { extendMapPrototype } from '../../src/index';
1212
import { ZarrLayerAdapter, getZarrLayersMap } from '../../src/lib/layers';
1313
import 'maplibre-gl/dist/maplibre-gl.css';
14+
15+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
16+
extendMapPrototype();
1417
import { LayerControl } from 'maplibre-gl-layer-control';
1518
import 'maplibre-gl-layer-control/style.css';
1619
import type { ZarrLayer } from '@carbonplan/zarr-layer';

examples/zarr/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/// <reference path="../../src/types.d.ts" />
22

33
import maplibregl from 'maplibre-gl';
4-
import '../../src/index'; // Import to extend Map.prototype
4+
import { extendMapPrototype } from '../../src/index';
55
import { ZarrLayerAdapter, getZarrLayersMap } from '../../src/lib/layers';
66
import 'maplibre-gl/dist/maplibre-gl.css';
7+
8+
// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
9+
extendMapPrototype();
710
import { LayerControl } from 'maplibre-gl-layer-control';
811
import 'maplibre-gl-layer-control/style.css';
912
import type { ZarrLayer } from '@carbonplan/zarr-layer';

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,25 @@ import {
4040

4141
// Note: Module augmentation for Map is in types.d.ts
4242

43+
// Track if prototype has been extended to avoid duplicate extensions
44+
let prototypeExtended = false;
45+
4346
/**
4447
* Extend the MapLibre GL Map prototype with convenience methods.
45-
* This function is called automatically when the module is imported.
48+
* This function is called automatically when the module is imported,
49+
* but can also be called explicitly to ensure reliable prototype extension
50+
* in production builds where tree-shaking might occur.
51+
*
52+
* It's safe to call this function multiple times - it will only extend
53+
* the prototype once.
54+
*
55+
* @returns true if the prototype was extended, false if already extended
4656
*/
47-
function extendMapPrototype(): void {
57+
export function extendMapPrototype(): boolean {
58+
if (prototypeExtended) {
59+
return false;
60+
}
61+
prototypeExtended = true;
4862
// Basemap methods
4963
Map.prototype.addBasemap = function (name) {
5064
return addBasemap(this, name);
@@ -160,6 +174,8 @@ function extendMapPrototype(): void {
160174
Map.prototype.removeZarrLayer = function (layerId) {
161175
return removeZarrLayer(this, layerId);
162176
};
177+
178+
return true;
163179
}
164180

165181
// Extend Map.prototype when module is imported

0 commit comments

Comments
 (0)