Possibility of ES6 module support?
Rather than a script element in the HTML, it would be helpful to be able to import as a module. For example:
import {Engine, Render, Bodies, Composite} from "./matter.min.js";
// or
import Matter from "./matter.min.js";
Is this already possible? If not, is it something worth adding?
It's all in CJS so there is no way to do ESM import without an extra build / bundle step. Which is a bummer because it means this package doesn't actually run in a browser.
If you pull in the library via a package manager then there's arguably no good reason these days not to include a bundling step. Modern bundlers will be able to automatically handle CJS imports in ES modules.
In other instances you can import it through bundling services like JSDelivr's esm.run or esm.sh, e.g.
import Matter from "https://esm.run/matter-js";
If you pull in the library via a package manager then there's arguably no good reason these days not to include a bundling step.
I don't. Package managers are overkill; the browser already has module support, so why duplicate that?
In other instances you can import it through bundling services like JSDelivr's esm.run or esm.sh, e.g.
import Matter from "https://esm.run/matter-js";
Ah, now THAT looks more promising! Thank you. I just switched over to using this form. For anyone else following this thread, you can also do this:
import "https://esm.run/poly-decomp";
if you need concave object decomposition.
I'd definitely prefer to be able to (a) fetch directly from GitHub rather than adding another service dependency, or (b) use module imports with a vendored copy, but this is better than what I was doing previously with a script tag. Thanks.
Can this be added to the docs somewhere?
Related Stack Overflow thread showing the jsdelivr alternative to esm.run:
import Matter from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'