dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
363 stars 65 forks source link

isomorphic-webcrypto not resolved correctly with react-native expo #93

Open aurmartin opened 1 week ago

aurmartin commented 1 week ago

Describe the bug The random module does not work with react-native expo.

I have followed the instructions in the README.md (installed isomorphic-webcrypto and enabled unstable_enablePackageExports).

The isomorphic-webcrypto module isn't resolved correctly.

Error message: TypeError: Cannot read property 'ensureSecure' of undefined, js engine: hermes

Stacktrace:

image

To Reproduce

  1. npx create-expo-app@latest
  2. Import import * as random from 'lib0/random' and use random.rand()
  3. npx expo start
  4. Open on android emulator

Expected behavior Lib0 random module works with react-native expo.

Environment Information

Additional context A workaround I have found is to hack the metro resolver:

// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require("expo/metro-config");

/** @type {import('expo/metro-config').MetroConfig} */
const defaultConfig = getDefaultConfig(__dirname);

console.warn("THERE")

const config = defaultConfig;

config.resolver.unstable_enablePackageExports = true;

config.resolver.resolveRequest = (context, moduleName, platform) => {
  if (platform !== "web" && moduleName === "isomorphic-webcrypto") {
    return {
      filePath:
        __dirname + "/node_modules/isomorphic-webcrypto/src/react-native.js",
      type: "sourceFile",
    };
  }

  return context.resolveRequest(context, moduleName, platform);
};

module.exports = config;
aurmartin commented 1 week ago

I also opened a PR with a fix for this issue: https://github.com/dmonad/lib0/pull/94