Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion examples/basic/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/// <reference path="../../src/types.d.ts" />

import maplibregl from 'maplibre-gl';
import '../../src/index'; // Import to extend Map.prototype
import { extendMapPrototype } from '../../src/index';
import 'maplibre-gl/dist/maplibre-gl.css';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();
import { LayerControl } from 'maplibre-gl-layer-control';
import 'maplibre-gl-layer-control/style.css';
import type { BasemapName } from '../../src/lib/basemaps/types';
Expand Down
5 changes: 4 additions & 1 deletion examples/cog-simple/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
/// <reference path="../../src/types.d.ts" />

import maplibregl from 'maplibre-gl';
import '../../src/index'; // Import to extend Map.prototype
import { extendMapPrototype } from '../../src/index';
import { COGLayerAdapter } from '../../src/lib/layers';
import 'maplibre-gl/dist/maplibre-gl.css';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();
import { LayerControl } from 'maplibre-gl-layer-control';
import 'maplibre-gl-layer-control/style.css';

Expand Down
5 changes: 4 additions & 1 deletion examples/cog/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/// <reference path="../../src/types.d.ts" />

import maplibregl from 'maplibre-gl';
import '../../src/index'; // Import to extend Map.prototype
import { extendMapPrototype } from '../../src/index';
import { COGLayerAdapter } from '../../src/lib/layers';
import 'maplibre-gl/dist/maplibre-gl.css';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();
import { LayerControl } from 'maplibre-gl-layer-control';
import 'maplibre-gl-layer-control/style.css';

Expand Down
5 changes: 4 additions & 1 deletion examples/react/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import React, { useState, useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';
import maplibregl, { Map } from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import { MapExtendProvider, useMapExtend } from '../../src/react';
import { MapExtendProvider, useMapExtend, extendMapPrototype } from '../../src/react';
import type { BasemapName } from '../../src/lib/basemaps/types';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();

// Sample GeoJSON data
const sampleGeojson = {
type: 'FeatureCollection' as const,
Expand Down
5 changes: 4 additions & 1 deletion examples/zarr-simple/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
/// <reference path="../../src/types.d.ts" />

import maplibregl from 'maplibre-gl';
import '../../src/index'; // Import to extend Map.prototype
import { extendMapPrototype } from '../../src/index';
import { ZarrLayerAdapter, getZarrLayersMap } from '../../src/lib/layers';
import 'maplibre-gl/dist/maplibre-gl.css';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();
import { LayerControl } from 'maplibre-gl-layer-control';
import 'maplibre-gl-layer-control/style.css';
import type { ZarrLayer } from '@carbonplan/zarr-layer';
Expand Down
5 changes: 4 additions & 1 deletion examples/zarr/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/// <reference path="../../src/types.d.ts" />

import maplibregl from 'maplibre-gl';
import '../../src/index'; // Import to extend Map.prototype
import { extendMapPrototype } from '../../src/index';
import { ZarrLayerAdapter, getZarrLayersMap } from '../../src/lib/layers';
import 'maplibre-gl/dist/maplibre-gl.css';

// Explicitly extend Map.prototype to ensure it's not tree-shaken in production builds
extendMapPrototype();
import { LayerControl } from 'maplibre-gl-layer-control';
import 'maplibre-gl-layer-control/style.css';
import type { ZarrLayer } from '@carbonplan/zarr-layer';
Expand Down
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,25 @@ import {

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

// Track if prototype has been extended to avoid duplicate extensions
let prototypeExtended = false;

/**
* Extend the MapLibre GL Map prototype with convenience methods.
* This function is called automatically when the module is imported.
* This function is called automatically when the module is imported,
* but can also be called explicitly to ensure reliable prototype extension
* in production builds where tree-shaking might occur.
*
* It's safe to call this function multiple times - it will only extend
* the prototype once.
*
* @returns true if the prototype was extended, false if already extended
*/
function extendMapPrototype(): void {
export function extendMapPrototype(): boolean {
if (prototypeExtended) {
return false;
}
prototypeExtended = true;
Comment on lines +57 to +61
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new exported extendMapPrototype function and its idempotency behavior (using the prototypeExtended flag) lack test coverage. Consider adding tests to verify:

  1. The function returns true on first call and false on subsequent calls
  2. The Map prototype is properly extended with all expected methods after calling the function
  3. Multiple calls don't cause duplicate assignments or errors

This is particularly important since this function is now part of the public API and addresses a critical tree-shaking issue.

Copilot uses AI. Check for mistakes.
// Basemap methods
Map.prototype.addBasemap = function (name) {
return addBasemap(this, name);
Expand Down Expand Up @@ -160,6 +174,8 @@ function extendMapPrototype(): void {
Map.prototype.removeZarrLayer = function (layerId) {
return removeZarrLayer(this, layerId);
};

return true;
}

// Extend Map.prototype when module is imported
Expand Down
Loading