ES Module Support
Related: https://github.com/toji/gl-matrix/issues/444
The package will support the following usage in Node:
await import('gl-matrix')
await import('gl-matrix/mat4')
require('gl-matrix')
require('gl-matrix/mat4')
by making the following changes to the published package.json:
+ "type": "module",
- "main": "cjs/index.js",
+ "main": "cjs/index.cjs",
"module": "esm/index.js",
+ "exports": {
+ ".": {
+ "require": "./cjs/index.cjs",
+ "import": "./esm/index.js"
+ },
+ "./mat2": {
+ "require": "./cjs/mat2.cjs",
+ "import": "./esm/mat2.js"
+ },
+ "./mat2d": {
+ "require": "./cjs/mat2d.cjs",
+ "import": "./esm/mat2d.js"
+ },
+ ...
Related:
- https://github.com/toji/gl-matrix/issues/439#issuecomment-934516271
I've been using gl-matrix in a library that is going ESM-only and needs to support Node.js, so my workaround for the time being is to configure my build tool (microbundle) to inline the gl-matrix functions I need, rather than keeping the gl-matrix npm package as a production dependency. This is working for now, but I do look forward to full ESM support and package#exports in v4, if that's still the plan!
In the same vein, we are about upgrade math.gl 4.0 to type: module and unless this (or something equivalent) lands, we will need to replace our gl-matrix dependency with a local fork (at least temporarily).
I'm building my project on v4 and it would be awesome to just import from 'gl-matrix' instead of from 'gl-matix/dist/esm'!
This should probably not be merged into the 3.4.x branch, as it would be a breaking change.
Full ESM support (including package exports) was merged in https://github.com/toji/gl-matrix/pull/432 and released in version 3.4.0, but reversed in 3.4.{1,2,3} (due to https://github.com/toji/gl-matrix/issues/439). The ESM related changes seem to remain in the master branch, but I'm not sure what the release plan is there. Perhaps @toji or @stefnotch knows more...