Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

DanHulton
Copy link
Contributor

(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 ongteckwu merged commit 9c9bbc6 into ongteckwu:main Aug 1, 2023
@ongteckwu
Copy link
Owner

published 0.3.0 npm

@ongteckwu
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants