Closed nrenner closed 2 years ago
Loading leaflet-maplibre-gl.js with Dynamic Imports raises an error:
TypeError: root is undefined in leaflet-maplibre-gl.js:10
TypeError: root is undefined
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).
this
undefined
import()
Importing maplibre-gl.js works, the UMD wrapper there is generated by Rollup.
Detecting self (and/or globalThis) in addition to this helps, examples:
self
globalThis
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.thisIsA, global.thisIsB));
}(typeof self !== 'undefined' ? self : this, function (b) {
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.
.js
The second commit changes uses of window to root.
window
root
Thanks for sharing this fix. @webberig what do you think?
Loading leaflet-maplibre-gl.js with Dynamic Imports raises an error:
TypeError: root is undefined
in leaflet-maplibre-gl.js:10See minimal example to reproduce (output in console log), basically this line:
The error occurs because
this
isundefined
, as theimport()
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/orglobalThis
) in addition tothis
helps, examples:(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.thisIsA, global.thisIsB));
}(typeof self !== 'undefined' ? self : this, function (b) {
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.
(filter devtools Network tab by
.js
and observe when activating the Hillshading overlay)The second commit changes uses of
window
toroot
.