streamich / json-joy

JSON CRDT, JSON CRDT Patch, JSON Patch+, JSON Predicate, JSON Pointer, JSON Expression, JSON Type
https://jsonjoy.com/libs/json-joy-js
Apache License 2.0
741 stars 14 forks source link

Dynamic import for libraries that depend on json-joy not (easily) possible #497

Open arietrouw opened 9 months ago

arietrouw commented 9 months ago

Hi,

I noticed you guys just stub out the export entries in the package.json and just expect people to import via direct file referencing. This is problematic for libraries that depend on the library that build to but esm and cjs. It forces the library writer to either wrap your library or to have two sets of source code, one importing each.

The preferred way to do this is to provide an exports section in the package.json that maps to the various exports and provide a import and require path for each of those exports. This makes it so that the loader can pick the correct one based on the project that is loading it, rather than the coder having to pick one manually.

Example: { ... "types": "dist/node/index.d.ts", "exports": { ".": { "browser": { "require": { "types": "./dist/browser/index.d.cts", "default": "./dist/browser/index.cjs" }, "import": { "types": "./dist/browser/index.d.mts", "default": "./dist/browser/index.js" } }, "node": { "require": { "types": "./dist/node/index.d.cts", "default": "./dist/node/index.cjs" }, "import": { "types": "./dist/node/index.d.mts", "default": "./dist/node/index.js" } } }, "./package.json": "./package.json" }, "main": "dist/node/index.cjs", "module": "dist/node/index.js", "type": "module" ... }

-Arie