yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

ESM Import #9

Closed flaviouk closed 3 years ago

flaviouk commented 3 years ago

Describe the bug Can't import in a barebones typescript environment

To Reproduce Steps to reproduce the behavior:

  1. Clone https://github.com/flaviouk/y-protocols-import
  2. Run yarn dev

Expected behavior No errors

Environment Information

dmonad commented 3 years ago

What is the error message?

dmonad commented 3 years ago

If you want to import the cjs file, you can do so. Use lib0/dist/sync.cjs instead.

flaviouk commented 3 years ago

Did you try to run this locally?

I did try with cjs but it also didn't work, are you able to hack the repo I sent with a solution that doesn't throw errors?

flaviouk commented 3 years ago

Btw this is for the y-protocols package not lib0

flaviouk commented 3 years ago

Alright got it working: https://github.com/flaviouk/y-protocols-import/commit/684c1ebbd92ab6f9fc442c114e180abec5f6a617

I hate configs

flaviouk commented 2 years ago

Okay after battling with esm issues in jest for a little longer, I found a way around it:

  moduleNameMapper: {
    '^y-protocols/(.*)$': 'y-protocols/dist/$1.cjs',
    '^lib0/dist/(.*).cjs$': 'lib0/dist/$1.cjs',
    '^lib0/(.*)$': 'lib0/dist/$1.cjs',
  },

Not a big fan of having to do this tbh @dmonad

dmonad commented 2 years ago

Can't you just import 'y-protocols/sync'? It does work for me in webpack, rollup, npm, and native esm. What is this about?

flaviouk commented 2 years ago

Importing awareness actually, but in jest it was pointing to the esm version which is not supported, so these module mappers in the jest config make it work somehow, esm tooling is complicated at the moment

dmonad commented 2 years ago

This seems to be more an issue with jest than with esm tooling.

If jest would support either esm modules or at least module mapping, then it should just work. We can only fix this by making y-protocols a cjs module (which is obviously not an option).

flaviouk commented 2 years ago

Correct yes! But at the same time, I’ve seen some of your rollup configs which have some exceptions to these modules as well, not great imo. I’ve never had issues when I’ve used a single file export type builder like https://github.com/jaredpalmer/tsdx, tooling get’s confused when you export multiple files on a single package, references become broken unless you fix it manually via config.. Maybe its the case for @yjs/protocol-awareness instead of y-protocol/awareness

dmonad commented 2 years ago

I removed these rollup configurations because the problem is now widely solved through module mapping.

Also, I only needed to rewrite the paths because I'm generating a CJS module based on the esm module. For compatibility, I needed to rewrite every path to their cjs version.