yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

Non-transpiled code being exported #17

Closed ViktorQvarfordt closed 2 years ago

ViktorQvarfordt commented 2 years ago

There is a dist folder, but when importing normally from this package, you get the non-transpiled files. The problem with this is that the non-transpiled code uses ESM which isn't always supported

It looks like this:

  "exports": {
    "./sync.js": "./sync.js",
    "./dist/sync.cjs": "./dist/sync.cjs",

But I believe it should be something like this:

  "exports": {
    "./sync.js": "./dist/sync.cjs",
dmonad commented 2 years ago

Hi Victor,

Usually, you won't interact with this project. If you do, I recommend importing y-protocols/sync instead of y-protocols/sync.js to get dynamic resolution depending on your system (browser / node, cjs / esm). In some cases, it is helpful to be explicit about which version you want to use. In this case, you can import the required module directly (.js is always a esm module since all packages in the Yjs organization are esm bundles as specified by type: 'module').

The npm developers even discourage from linking files with extensions to other paths. You can only link paths without an extension like .js to a new location (sync.js ⇒ dist/sync.cjs is not allowed). I think that the latest npm version even throws errors if you do so.

My own take on this is that I want to push esm modules and make them a first-class citizen. Esm has real advantages when it comes to bundling size and maintainability which is why I want to support it. Today, you can write any node module as a pure mjs module. If it wasn't for cjs, none of the projects in the Yjs organization wouldn't even need a bundler (except the yjs-demos repo).

I'm going to close this ticket because we can't implement this change, but let me know if you have more questions.