React Native Monorepo 项目不适用于发布版本

React Native Monorepo project doesn't work on release build

我正在使用 yarn workspaces 方法对 Monorepo 进行试验,该方法包含一个示例 react-native 项目,该项目已配置并且在调试构建中运行良好。当我创建一个发布版本和 运行 应用程序时,它一打开就崩溃了。我在下面提供了日志和代码。

Android工作室Logcat:

2022-05-16 23:18:08.466 6620-6644/? E/AndroidRuntime: FATAL EXCEPTION: create_react_context
Process: com.sample.mobile, PID: 6620
java.lang.RuntimeException: Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
    at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
    at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:248)
    at com.facebook.react.bridge.JSBundleLoader.loadScript(JSBundleLoader.java:29)
    at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:277)
    at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1422)
    at com.facebook.react.ReactInstanceManager.access00(ReactInstanceManager.java:138)
    at com.facebook.react.ReactInstanceManager.run(ReactInstanceManager.java:1111)
    at java.lang.Thread.run(Thread.java:923)

Monorepo 结构:

monorepo
    |
    |___Common
    |
    |___Mobile

Monorepo 的根 package.json:

{
   "name": "monorepo",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "license": "UNLICENSED",
   "workspaces": {
     "packages": [
       "packages/*"
     ],
     "nohoist": [
       "**/react-native",
       "**/react-native/**"
     ]
   },
   "references": [
     {
       "path": "packages/Common"
     },
     {
       "path": "packages/Mobile"
     }
   ],
   "private": true
 }

普通根package.json

{
   "name": "common",
   "version": "1.0.0",
   "description": "",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "license": "UNLICENSED",
   "devDependencies": {
     "@types/react": "^18.0.9",
     "@types/react-native": "^0.67.7"
   }
 }

React Native 的package.json:

...
 "peerDependencies": {
    "common": "0.0.1"
 },
...

React Native 的metro.config.js

const path = require('path');
module.exports = {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        sourceExts: ['jsx', 'js', 'ts', 'tsx', 'mjs'],
        extraNodeModules: new Proxy(
            {},
            {
              get: (target, name) => {
                return path.join(__dirname, `node_modules/${name}`);
              },
            },
          ),
    },
    watchFolders: [
        path.resolve(__dirname),
        path.resolve(__dirname.replace('Mobile', 'Common'))
    ],
};

有什么建议吗?

我发现问题不在于 monorepo 设置,而在于 React Native 本身。我正在使用导致此问题的最新版本的 React Native (0.68.2)。我验证了这一点,我创建了一个简单的新 React Native 项目,其中发生了同样的问题。 React Native 存储库本身也存在未解决的问题。