yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

Issues importing as common js module #3

Closed sammacbeth closed 4 years ago

sammacbeth commented 4 years ago

I had some issues trying to use this module in a typescript project. This is because there is no 'main', so deep imports (like y-protocols/awareness) cannot be transformed to common JS compatible require statements (require('y-protocols/awareness') fails).

One possible fix is to have a index.js and point correctly to the module and main versions from the package.json. This will enable bundlers to pick the correct version when importing. In commonJS cases, require('y-protocols') can be used, while es6 modules can still import specific classes.

At the moment I'm using the following patch to fix these issues for my use-case, but this only exports the awareness class, so is not a full solution.

dmonad commented 4 years ago

y-protocols is esm module that should work in nodejs (https://nodejs.org/api/esm.html). In node >= 13 you can simply do import * as Awareness from 'y-protocols/awareness.js.

But I still recommend that you export a esm module and a cjs module. The esm module should point to y-protocols/awareness.js and the cjs module should point to y-protocols/dist/awareness.cjs.