Alternate, await-free import of UUIDv7 ESM module. #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(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 theuuidv7()
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 toError [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 thefrom()
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.