maplibre / maplibre-gl-leaflet

This is a binding from MapLibre GL JS to the familiar Leaflet API.
ISC License
134 stars 36 forks source link

Support dynamic imports in UMD wrapper #27

Closed nrenner closed 2 years ago

nrenner commented 2 years ago

Loading leaflet-maplibre-gl.js with Dynamic Imports raises an error:

TypeError: root is undefined in leaflet-maplibre-gl.js:10

See minimal example to reproduce (output in console log), basically this line:

import('https://unpkg.com/@maplibre/maplibre-gl-leaflet@0.0.16/leaflet-maplibre-gl.js')
  .then(() => console.log('L.maplibreGL: ', L.maplibreGL))

The error occurs because this is undefined, as the import() call loads as module in strict mode (but can be called from a regular script).

Importing maplibre-gl.js works, the UMD wrapper there is generated by Rollup.

Detecting self (and/or globalThis) in addition to this helps, examples:

I propose to use Rollup-style global detection, to be compatible with Maplibre. Rollup is licensed under MIT, which is supposed to be functionally equivalent to ISC.

The second commit changes uses of window to root.

wipfli commented 2 years ago

Thanks for sharing this fix. @webberig what do you think?