paulmillr / micro-key-producer

Produces secure keys and passwords. Supports SSH, PGP, BLS, OTP and many other formats
MIT License
34 stars 9 forks source link

[Question] Add CommonJs support #14

Closed mordonez-me closed 7 months ago

mordonez-me commented 7 months ago

Are there any plans to add support for commonJs?

paulmillr commented 7 months ago

What is your use case? Which bundler, etc? I'm trying to move away from it, because pure esm means Bun compat, etc

mordonez-me commented 7 months ago

This was my case:

1) monorepo with react-native and firebase functions (plus project packages) 2) firebase functions importing packages without extension and compiled using tsc (with commonjs) 3) packages created in all versions (cjs and esm) and using the library with a regular import (import { HDKey } from 'ed25519-keygen/hdkey') 4) In firebase functions if I use commonjs this package is not supported so I switched to esm but I was forced to use "file extensions in imports" otherwise I will have [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import error.

For now, I had to add a dynamic import which is accepted but since is begin used in more than one place is kind of annoying to add the dynamic import on each function using it.

It would be much easier to have a commonjs to avoid changing the code too much or use some weird babel transformation.

paulmillr commented 7 months ago

So, from what I understand, firebase doesn't support ed25519-keygen/hdkey import and requires appending .js.

For now, I had to add a dynamic import which is accepted

Why a dynamic import? Can you statically import ed25519-keygen/hdkey.js?

mordonez-me commented 7 months ago

Yes, when functions analyze the code doesn't allow you to upload it without path extensions (static import and using esm in the project) or dynamic import.

I am not using path extensions because I would have to change it everywhere to make it consistent through the whole project and is a bit risky to do it now, so I chose the dynamic import. Could also be the case where another library is using it, which will force the use of esm or path extensions even more difficult to deal with, I think in that case a patch would be the only viable option to avoid adding path extensions.

paulmillr commented 7 months ago

ed25519-keygen/hdkey is available because it's specified in package.json import map (exports field).

If firebase doesn't support import maps, you should use hdkey.js. Just because you don't want to use one extension (there is no need to change all of them "for consistency"), it doesn't mean keygen doesn't work.

mordonez-me commented 7 months ago

I didn't say it was not working, I said it would be easier for this case to add support to commonjs. The "consistency" is a matter of perspective but in any case, path extension or dynamic import, it works, and great job btw.