Skip to content

build: leave ./unstable's libraries to the consumer's bundler - #1760

Merged
mscno merged 2 commits into
v3from
fix/unstable-external
Nov 28, 2026
Merged

mscno merged 2 commits into
v3from
fix/unstable-external

Conversation

@mscno

@mscno mscno commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

./unstable now leaves its libraries to the consumer's bundler instead of bundling them. Leaflet-Geoman Pro bundles this entry and also imports the same libraries itself, so until now it shipped Turf, polyclip-ts and lodash twice (class SweepEvent appeared twice in Pro's min.js).

  • The ./unstable build in bundle.mts gets an external-packages esbuild plugin:
    • Every bare import becomes external and keeps its package name.
    • A subpath of a package without an exports map is pointed at its file, because Node's ESM resolution needs it: lodash/get becomes lodash/get.js. With a plain packages: 'external', Node fails with Cannot find module '.../node_modules/lodash/get'.
    • The build fails when the entry imports a package that is not in dependencies or peerDependencies.
    • The build also fails when anything from node_modules is bundled into the entry, even through a relative path; the plugin checks the metafile for this.
  • leaflet-geoman-unstable.js: 553,787 → 339,688 bytes. Its bare imports are @turf/boolean-contains, @turf/kinks, @turf/line-intersect, @turf/line-split, lodash/get.js, polyclip-ts and leaflet, all in dependencies or peerDependencies.
  • Every other file in dist and dist/types is byte-identical to the afefa35 build: 60 of 62 files. Only leaflet-geoman-unstable.js and its .map differ.
  • The e2e fixture cypress/fixtures/unstable.html loaded the raw entry in the browser, which can't resolve package imports. The build now also bundles the built entry into cypress/fixtures/unstable-bundle.js (git-ignored), the way @geoman-io packages use it, and the fixture loads that. Its NODE_ENV define keeps the old browser behaviour: isDevelopment() was already true there.
  • Comments in src/js/unstable.ts and bundle.mts, and the CHANGELOG line for ./unstable, say the libraries are left to the consumer's bundler and why.

Tests: new test/unstable-entry.test.ts (unit, on the built dist, which pnpm install's prepare builds in CI):

  • bundles no library code: no node_modules source in the entry's source map;
  • imports only packages in dependencies or peerDependencies (read with esbuild's metafile);
  • loads under Node's own ESM resolution in a child node process, with PM and EditLine exported.

Before the fix:

  • The afefa35 build fails the first two tests.
  • A plain packages: 'external' build (339,679 bytes) fails the third with ERR_MODULE_NOT_FOUND ... lodash/get.
  • Build-time mutants:
    • an import 'tsx' fails the build with "'tsx' is from 'tsx', which is not in dependencies or peerDependencies";
    • an import '../../node_modules/lodash/get.js' fails it with "./unstable bundles node_modules/.pnpm/lodash@4.18.1/node_modules/lodash/isArray.js".

Packed tarball, clean consumer with only the tarball and leaflet@2.0.0-beta.4 (npm, 29 packages):

  • In Node, import('@geoman-io/leaflet-geoman-free/unstable') loads (31 exports), and the main entry loads as before.
  • Bundled with esbuild and run in Chromium, both apps load with no errors.
    • ./unstable app: toolbar, unstable_insertVertex (lodash), hasSelfIntersection (kinks), a polygon cut (polyclip-ts) and a line cut (line-split, line-intersect) all return results.
    • Main entry app: toolbar, edit markers and kinks work.
  • A Pro-like app that imports ./unstable, polyclip-ts and @turf/kinks bundles to 545,315 bytes with one SweepEvent class and one kinks. Against the afefa35 entry it is 641,092 bytes, with two of each.

Checks: build, typecheck, lint, unit 106/106 (+3). Full e2e (Electron): base 486/486 (afefa35), PR 486/486.

Independent review (Opus 5.5) at 989c338: no blocking findings and two Low notes, both fixed in the next commit. A dependency that resolves outside node_modules (a link: or file: one) now fails the build with a clear error instead of an ENOENT. The Node load test no longer fails on a Node warning in stderr, only on an Error. The dist hashes are unchanged.

Hashes of the afefa35 build and this PR (sha256, dist):

60 of 62 identical
file afefa35 PR
leaflet-geoman-global.js 9f085e754b880392 same
leaflet-geoman-global.js.map 12f75e22c0cd349a same
leaflet-geoman-global.min.js ea8d00c6633ffbde same
leaflet-geoman-global.min.js.map f8cd6042fc571b3d same
leaflet-geoman-unstable.js 906002b8ecf97932 e95c86274f6237ae
leaflet-geoman-unstable.js.map aaeda7bef5f17a57 b20d016e868011ca
leaflet-geoman.css 8d9f79b153a51c61 same
leaflet-geoman.css.map dc257a732fba5d1e same
leaflet-geoman.d.ts 1304086d87db9e39 same
leaflet-geoman.js 824aa5625df8bda7 same
leaflet-geoman.js.map 916e494d0264ad48 same
leaflet-geoman.min.js 71eed7885a8f2f9a same
leaflet-geoman.min.js.map b542247a165c916f same
types/assets/translations/index.d.ts 2608c05776547f08 same
types/js/Draw/L.PM.Draw.Circle.d.ts f1d67562b297bec9 same
types/js/Draw/L.PM.Draw.CircleMarker.d.ts e57cbaf650c7160a same
types/js/Draw/L.PM.Draw.Cut.d.ts ea5b3a75de00815a same
types/js/Draw/L.PM.Draw.Line.d.ts 59b41a6c5e0c34df same
types/js/Draw/L.PM.Draw.Marker.d.ts 694ae95effee89ce same
types/js/Draw/L.PM.Draw.Polygon.d.ts e9b65c433adb8ff0 same
types/js/Draw/L.PM.Draw.Rectangle.d.ts 700b007783468424 same
types/js/Draw/L.PM.Draw.Text.d.ts 860244b340e4d428 same
types/js/Draw/L.PM.Draw.d.ts 975ae9bda6d45195 same
types/js/Draw/L.PM.DrawTool.d.ts 1d468f72dc6a76c2 same
types/js/Edit/L.PM.Edit.Circle.d.ts cced9f0918e52117 same
types/js/Edit/L.PM.Edit.CircleMarker.d.ts 2b467de6816df682 same
types/js/Edit/L.PM.Edit.ImageOverlay.d.ts 94f724dab90bdc0d same
types/js/Edit/L.PM.Edit.LayerGroup.d.ts 6b37e316aae50df0 same
types/js/Edit/L.PM.Edit.Line.d.ts 9741b6bd31a7f836 same
types/js/Edit/L.PM.Edit.Marker.d.ts 6c5f2e91e2f71da3 same
types/js/Edit/L.PM.Edit.Polygon.d.ts 84ec61aae098ce7a same
types/js/Edit/L.PM.Edit.Rectangle.d.ts b431dda5a0ebb921 same
types/js/Edit/L.PM.Edit.Text.d.ts 0b8cfecbc327a005 same
types/js/Edit/L.PM.Edit.d.ts 90f9a69e0ef26cc8 same
types/js/L.PM.Map.d.ts 3dc024a985e1713a same
types/js/L.PM.Utils.d.ts 805925f9b26ca381 same
types/js/Mixins/Dragging.d.ts e4c3ce66ef16e686 same
types/js/Mixins/Events.d.ts 855a52b9351306b7 same
types/js/Mixins/Keyboard.d.ts 0f35c8324211dae9 same
types/js/Mixins/MarkerLimits.d.ts 01d9f7181ac4d641 same
types/js/Mixins/Modes/Mode.Drag.d.ts 2bc99763e46ce49d same
types/js/Mixins/Modes/Mode.Edit.d.ts 9648ec2fd649b64a same
types/js/Mixins/Modes/Mode.Removal.d.ts b6cee798b49c10f9 same
types/js/Mixins/Modes/Mode.Rotate.d.ts 38e01e659f5e21ce same
types/js/Mixins/Rotating.d.ts 550987b4919dcc56 same
types/js/Mixins/Snapping.d.ts 90cbe5524c441286 same
types/js/PM.d.ts 7582d94e8bb444b1 same
types/js/Toolbar/L.Controls.d.ts 9fb5fcb1d4924e80 same
types/js/Toolbar/L.PM.Toolbar.d.ts c5084b6ddd7d0226 same
types/js/helpers/DragSession.d.ts 9aa706761756ea44 same
types/js/helpers/Matrix.d.ts 0656b4771870a283 same
types/js/helpers/ModeHelper.d.ts 6037adc0366a7259 same
types/js/helpers/ModeManager.d.ts 422d8f66a40e50ff same
types/js/helpers/classes.d.ts 66fac5d922f0d1d1 same
types/js/helpers/index.d.ts 49f40ce1b831fb13 same
types/js/helpers/language.d.ts da97688b7dec77a6 same
types/js/helpers/options.d.ts 69db7ae4b463dac5 same
types/js/helpers/turfHelper.d.ts 9fa1115c541c3492 same
types/js/index.d.ts 868726d81c167b7b same
types/js/unstable.d.ts 00177bb487943dea same
types/types/leaflet-internals.d.ts 0e4b5a27b3e1c53a same
types/types/options.d.ts ec3dcf8d71e807f6 same

Pro bundles ./unstable and imports Turf, polyclip-ts and lodash itself, so it
shipped each twice. The entry now imports them by package name (a subpath of
a package without an exports map by its file, as Node's ESM resolution needs),
and the build fails on a package outside dependencies or on bundled library
code. The e2e fixture loads a bundle of it, as Pro does.
@mscno
mscno merged commit 774ddcc into v3 Nov 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants