Add ESM module support - #166
Conversation
- Add ESM source in src/leaflet-mapbox-gl.js with import/export syntax
- Set up Rollup to build both ESM and UMD bundles to dist/
- Add package.json exports field for conditional ESM/CJS resolution
- Add "module" field pointing to ESM build
- Add TypeScript type definitions
- Configure JSHint for ES6+ with .jshintrc
- Keep original leaflet-mapbox-gl.js for backwards compatibility
Usage:
- ESM: import { mapboxGL } from 'mapbox-gl-leaflet'
- CJS: const { mapboxGL } = require('mapbox-gl-leaflet')
- Browser: <script src="dist/leaflet-mapbox-gl.js">
There was a problem hiding this comment.
Pull request overview
This PR adds an ES Module (ESM) build alongside the existing UMD distribution, aiming to improve compatibility with modern bundlers/Node ESM while preserving browser/global usage.
Changes:
- Added an ESM source entry (
src/leaflet-mapbox-gl.js) withimport/exportand named/default exports. - Introduced a Rollup build producing
dist/leaflet-mapbox-gl.esm.js(ESM) anddist/leaflet-mapbox-gl.js(UMD). - Updated package metadata/scripts and added TypeScript declarations.
Reviewed changes
Copilot reviewed 4 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/leaflet-mapbox-gl.js | New ESM entrypoint that exports MapboxGL / mapboxGL and also mutates L for compatibility. |
| rollup.config.js | Rollup config to emit ESM + UMD builds. |
| package.json | Adds conditional exports, sets type: module, and adds build/lint scripts. |
| package-lock.json | Updates lockfile to include Rollup dependencies and lockfile v3 format. |
| dist/leaflet-mapbox-gl.d.ts | Adds TypeScript typings and Leaflet module augmentation. |
| .jshintrc | Enables ES module linting and modern ECMAScript settings. |
| .gitignore | Ignores generated dist/*.js and sourcemaps while keeping .d.ts tracked. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Rename CJS output to .cjs extension for explicit module type - Add separate UMD build (.umd.js) for browsers - Add "browser" field pointing to UMD build - Update exports to use .cjs for require()
jgravois
left a comment
There was a problem hiding this comment.
i'm not maintaining this lib anymore so take my question with a grain of salt, but isn't the leaflet-mapbox-gl.js file in the root of the repo a UMD file as well?
for an ESM rewrite i'd expect there to be no UMD checked into version control and rely on rollup for the one distributed via npm.
There was a problem hiding this comment.
i think this file was committed accidentally.
Summary
This PR modernizes the package to support ES Modules (ESM) alongside the existing UMD format, enabling better tree-shaking and native module support in modern bundlers and Node.js.
Changes
src/leaflet-mapbox-gl.js) with nativeimport/exportsyntaxdist/"type": "module"for native ESM"module"field pointing to ESM build"exports"field with conditional exports for ESM/CJS resolution"types"field for TypeScript supportnpm run build,prepublishOnly)dist/leaflet-mapbox-gl.d.ts).jshintrc) updated for ES6+Usage
ESM (modern bundlers/Node.js):
CommonJS:
Browser (unchanged):
Backwards Compatibility
leaflet-mapbox-gl.jsis kept in the package for users referencing it directlyL.mapboxGL/L.MapboxGLglobalsTest plan
npm run lintpassesnpm run buildproduces both ESM and UMD bundles