mapbox / delaunator

An incredibly fast JavaScript library for Delaunay triangulation of 2D points
https://mapbox.github.io/delaunator/
ISC License
2.24k stars 139 forks source link

Can't use Delaunator as a ES module locally #80

Closed ahrnbom closed 1 year ago

ahrnbom commented 1 year ago

While I am by no means an expert on Javascript, I am a bit puzzled by the design of the built Delaunator files. Maybe I'm missing something obvious here.

I am making an offline application, so I need to download and store the libraries I'm using. I'm not using NPM or any other package manager, I'm trying to keep things simple for now.

Let's take Three.js as an example. I can simply download three.module.min.js, store this in a folder called three (along with its license, of course), and then simply do (in my javascript code) import * as THREE from './three/three.module.min.js';

This is extremely convenient because

  1. It works
  2. VS Code understands what you're doing and can autocomplete etc.

For Delaunator, however, the same approach does not work. I can either do import './delaunator/delaunator.min.js'; which works in the browser, but not in VS Code (it has no idea that Delaunator is a thing) or I can do import * as Delaunator from './delaunator/delaunator.min.js'; which VS Code understands perfectly, but doesn't work in the browser. In Chrome, it gets imported as a module object, and I see no obvious way to access the actual Delaunator class from it.

Is there a good reason to design the .js file like this? Am I missing something obvious in how to get both VS Code to understand what's going on, and making it work in a browser as well?

Thanks for making an awesome and fast library!

mourner commented 1 year ago

delaunator.min.js is a UMD bundle (exposing a global Delaunator variable), not a ES module bundle, so you can't use it as a module locally at the moment, only either through the Skypack or JSDelivr CDN. We might change this in the future, but meanwhile I'd suggest just building a ESM Delaunator bundle locally — e.g. using ESBuild.

ahrnbom commented 1 year ago

Thanks for the guidance, now at least I know where to start!

For what it's worth, here's one person who thinks providing a pre-built ES module bundle would be a good idea :)

mbostock commented 1 year ago

You can use a tool like download-esm to download an ES module bundle.

ahrnbom commented 1 year ago

Oh wow, that was exactly what I was looking for! Thanks!

EDIT: While download-esm can absolutely download delaunator as (what looks like) an ES module, I can't get it to work. Not sure which kind of import statement is supposed to work with it, none of the ones I'm familiar with seem to work. Oh well.