luizbarboza / polyclip-ts

MIT License
28 stars 10 forks source link

Types not being exported in a robust way #18

Open smallsaucepan opened 1 month ago

smallsaucepan commented 1 month ago

Hi @luizbarboza. We're quite keen to use polyclip-ts over in Turfjs. However having some issues gettings the types imported e.g.

index.ts(10,27): error TS7016: Could not find a declaration file for module 'polyclip-ts'. '/Users/james/Code/turf/cmurphy23/node_modules/.pnpm/polyclip-ts@0.16.6/node_modules/polyclip-ts/dist/index.js' implicitly has an 'any' type.
  There are types at '/Users/james/Code/turf/cmurphy23/packages/turf-intersect/node_modules/polyclip-ts/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'polyclip-ts' library may need to update its package.json or typings.

One fix would be to flesh out the exports section of polyclip-ts/package.json, so would be happy to prepare a PR for that. However, without packaging CJS version of the library as well, the best we could hope for from arethetypeswrong would be:

Screenshot 2024-10-26 at 7 19 28β€―PM

Would you like a PR that will generate and export CJS also? Or do you prefer to stick with ESM only?

Thanks for maintaining the package!

luizbarboza commented 1 month ago

Hi! Thanks for the suggestion and for supporting the package!

If possible, could you split it into two PRsβ€”one to fix the type export issue and another to add CJS support? This would make the review process easier.

smallsaucepan commented 1 month ago

Sure thing πŸ‘ Will do the smaller type export one first.

smallsaucepan commented 1 month ago

Oh, actually just saw #8, which had a similar goal to this issue. That PR was merged in e079fa8 but then rolled back some months later by 7bfaa6c.

Did something go wrong? Couldn't find any chatter about the reason.

luizbarboza commented 1 month ago

The PR was merged but rolled back before publishing a new version on npm. It references a PR in another repository that hasn’t been merged. I still have doubts about the ideal solution and want to be cautious with breaking changes.

smallsaucepan commented 4 weeks ago

Ah, understand. What I have in mind is quite similar to @vschroeter's PR, and a lot of the discussions they're having on the vite PR Turf has had too.

Appreciate you being cautious, and I certainly don't want to ruin anyone's day. To test we can implement a bunch of dummy clients and make sure we don't introduce any errors. Do you get any stats on who is using the library? Node or browser versions?

One thing I'd like to know more about is the existing "umd": "..." entry in exports. Not seen that before, and can't find it documented anywhere. If I understand this documentation it would act as a subpath should someone try to import * as whatever from "polyclip-ts/umd"? Do you recall which client/s that was intended for?

smallsaucepan commented 6 days ago

Hi @luizbarboza. Do you have any thoughts on the above?

luizbarboza commented 5 days ago

@smallsaucepan Honestly, I’m not entirely sure what is causing the error when resolving types.

The UMD bundle is intended for legacy environments and can be used via a CDN, as explained in the README.

A previous PR that was reverted had removed the UMD entry, but it’s important to keep it for backward compatibility. Could you submit a PR to fix the issue while preserving the UMD entry?

smallsaucepan commented 3 days ago

Honestly, I’m not entirely sure what is causing the error when resolving types.

@luizbarboza it's a couple of things. The immediate problem solved below is that typescript tools will (without further config) look for typedefs in the same directory as dist/index.js. But the index.d.ts is currently in the types/ directory. That's why we get errors in rows 3. and 4. below.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   β”‚ "polyclip-ts"                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node10            β”‚ 🟒                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node16 (from CJS) β”‚ ❌ No types                  β”‚
β”‚                   β”‚ ⚠️ ESM (dynamic import only) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node16 (from ESM) β”‚ ❌ No types                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ bundler           β”‚ ❌ No types                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

I have a four line PR that leaves umd untouched and gets you to this:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   β”‚ "polyclip-ts"                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node10            β”‚ 🟒                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node16 (from CJS) β”‚ ❌ No types                  β”‚
β”‚                   β”‚ ⚠️ ESM (dynamic import only) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ node16 (from ESM) β”‚ 🟒 (ESM)                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ bundler           β”‚ 🟒                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

No support for CJS require, same as current library. It can be added though that's a bit more involved. Happy to go with your preference.

Actually just remembered your comment above about adding CJS support separately. Will create a PR just for fixing up ESM types πŸ‘

smallsaucepan commented 1 day ago

Thanks for merging that @luizbarboza. Are you planning to release 0.16.7 with just this PR, or do you want to do the CJS generation as well first?

luizbarboza commented 1 day ago

Version 0.16.7 will be released with just this PR, leaving support for CJS for a later new version.

smallsaucepan commented 1 day ago

Wonderful. Thank you!