ongteckwu / typeid-ts

TypeID UUIDv7 implementation in Typescript (Lib and CLI)
Apache License 2.0
38 stars 5 forks source link

Alternate, await-free import of UUIDv7 ESM module. #7

Closed DanHulton closed 1 year ago

DanHulton commented 1 year ago

(Apologies for all the recent commits about this - these all should have been one commit if I had been properly paying attention.)

This PR changes to an alternate import of UUIDv7, where the module is imported using the CommonJS-safe await import(module) method inside of an anonymous function, which is then executed immediately. By the time the uuidv7() function is called later, it is nearly guaranteed to be loaded (like 5-9's guarantee, similar reliability to "UUIDs will not collide").

Without this, importing the module as CommonJS will work, but the moment basics.ts is imported, the compiler with throw an error similar to Error [ERR_REQUIRE_ESM]: require() of ES Module PROJECT/node_modules/uuidv7/dist/index.js from PROJECT/node_modules/typeid-ts/build/cjs/src/lib/basics.js not supported., as UUIDv7 does not expose a CommonJS export.

The only other ways of accomplishing this is to add the await import statement inside the from() function, which would change its signature to an async function, which reduces usability for ESM importers, or to custom-recompile UUIDv7 into a CommonJS-compatible format in the build step, as the maintainer of that library has indicated that they will not ever be adding a CommonJS export.

ongteckwu commented 1 year ago

published 0.3.0 npm

ongteckwu commented 1 year ago

I tried immediately calling and it works. So should be fine.