gl-matrix icon indicating copy to clipboard operation
gl-matrix copied to clipboard

ES Module Support

Open Pessimistress opened this issue 3 years ago • 5 comments

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"
+   },
+   ...

Pessimistress avatar Jan 06 '23 18:01 Pessimistress

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!

donmccurdy avatar Jan 25 '23 00:01 donmccurdy

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).

ibgreen avatar Jan 30 '23 14:01 ibgreen

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'!

mikeymaxdb avatar May 05 '23 05:05 mikeymaxdb

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...

lkmill avatar Jul 30 '23 09:07 lkmill