Search code examples
javascriptmoduleleafletgis

Can I use Leaflet as an es6 module? Plugins too?


Looking at the Leaflet docs, I don't see how to import Leaflet as an es6 module. And more importantly, all the Leaflet plugins as well.

Skypack converts Leaflet to modules:

import * as leaflet from 'https://cdn.skypack.dev/leaflet'

But I'm not sure how safe this approach would be. I'd prefer unpkg, but this:

https://unpkg.com/[email protected]?module

.. produces:

Package [email protected] does not contain an ES module

So basically will using Leaflet in a "modern" repo run into problems with modules?


Solution

  • Leaflet can be used as a module by importing it manually:

    import * as leaflet from "https://unpkg.com/leaflet/dist/leaflet-src.esm.js";
    

    Unfortunately, there doesn't seem to be a minified module build so uncompressed download size will be higher (422kB vs 147kB for [email protected], for example).