paulmillr / noble-ciphers

Audited & minimal JS implementation of Salsa20, ChaCha and AES
https://paulmillr.com/noble
MIT License
211 stars 8 forks source link

Revamp package exports for Metro/React Native #41

Open kigawas opened 2 weeks ago

kigawas commented 2 weeks ago

Description

Currently when importing noble-ciphers and noble-hashes on React Native, the following warnings are shown:

 WARN  Attempted to import the module "/Users/user/ReactNativeDemo/node_modules/.pnpm/@noble+ciphers@1.0.0/node_modules/@noble/ciphers/crypto.js" which is not listed in the "exports" of "/Users/user/ReactNativeDemo/node_modules/.pnpm/@noble+ciphers@1.0.0/node_modules/@noble/ciphers" under the requested subpath "./crypto.js". Falling back to file-based resolution. Consider updating the call site or asking the package maintainer(s) to expose this API.
 WARN  Attempted to import the module "/Users/user/ReactNativeDemo/node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/crypto.js" which is not listed in the "exports" of "/Users/user/ReactNativeDemo/node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes" under the requested subpath "./crypto.js". Falling back to file-based resolution. Consider updating the call site or asking the package maintainer(s) to expose this API.

It looks like for metro bundler, import { crypto } from '@noble/ciphers/crypto'; is equivalent to import { crypto } from '@noble/ciphers/crypto.js';. Because "./crypto.js" is not defined in package.json, it falls back to the file-based resolution.

Possible fix

Add "./crypto.js" in package.json can fix the warnings above

    "./crypto.js": {
      "types": "./crypto.d.ts",
      "node": {
        "import": "./esm/cryptoNode.js",
        "default": "./cryptoNode.js"
      },
      "import": "./esm/crypto.js",
      "default": "./crypto.js"
    },

Reference: https://metrobundler.dev/docs/package-exports/

paulmillr commented 2 weeks ago

Can fix or will fix? Test it first.

the imports are there for a reason. It’s the simplest and most compatible way with huge js ecosystem.

We have rollup, webpack, nodejs, and others tested inside of package ethereum-cryptography

kigawas commented 2 weeks ago

I've tested. It will fix the warning, but it seems kind of redundant