LiosK / uuidv7

A JavaScript implementation of UUID version 7
Apache License 2.0
173 stars 3 forks source link

Export CommonJS in addition to ES Modules #4

Closed leite08 closed 1 year ago

leite08 commented 1 year ago

Currently this project exports ESM packages on NPM. It would be useful to have CJS exported as well for projects that rely on that format.

Our Use Case

In our setup, we transpile our Typescript code into CJS. After installing uuidv7 from NPM the transpilation works fine (tsc), but we couldn't make our Jest to work.

We get SyntaxError: Unexpected token 'export' because out setup is for CJS, but the imported source from uuidv7 is ESM.

image

If we take the route to adjust our setup to ESM [1, 2] this goes away, but a bunch of other stuff break.

Proposed Solution

  1. Create separate tsconfig files, one for CJS and another for ESM;
  2. Update the build script to transpile twice, once for each tsconfig;
  3. Update package.json to export imports to the ESM dist folder and requires to the CJS dist folder.

Source: https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html

LiosK commented 1 year ago

Thank you for the request. The age of dual packaging seems to be over, and many libraries are migrating to the pure ESM arrangement. I'm currently not planning to reintroduce a CJS entry point.

leite08 commented 1 year ago

Thanks for the response and for building this!

reecefenwick commented 1 year ago

Hitting this issue as well. @leite08 would you mind sharing the approach you ended up taking, if any?

It would be great if you could reconsider supporting CJS as well @LiosK, I don't think the ecosystem is there yet in terms of ESM support and this could inhibit adoption of your awesome library

LiosK commented 1 year ago

Let's see if other similar requests will come.

leite08 commented 1 year ago

@reecefenwick we just ended up copying the index.ts file.

Other attempts (mainly make our project ESM-only) led to other libraries CJS-only to break.

reecefenwick commented 1 year ago

I ended up using https://github.com/jetpack-io/typeid-js as it was more of a complete solution for what I was doing, but it uses this library under the hood so thanks for your work @LiosK !

bakura10 commented 1 year ago

I'm also facing this issue. I also ended up copying the file locally, but it makes upgrades harder :). Would love it to be exported as CommonJS as well.

LiosK commented 1 year ago

v0.6 reintroduced the CommonJS entry point! However, I will remove it once this library reaches the stable release (v1.0). I am planning to make the last unstable release (v0.x) functionally equivalent to v1.0 except the CommonJS export, so that those who want the CJS support can stay with that unstable release, but no new feature will be offered to CJS users.