不能进行本机添加自定义资产的反应,忽略metro.config.js

时间:2019-11-19 11:44:02

标签: javascript react-native metro-bundler

我试图在具有以下配置的react native应用程序中使用自定义资产:

"dependencies": {
    "glob": "^7.1.4",
    "metro-config": "latest",
    "react": "16.8.6",
    "react-native": "0.60.5",
    "react-native-camera": "git+https://git@github.com/react-native-community/react-native-camera",
    "react-native-easy-toast": "^1.2.0",
    "react-native-fs": "^2.14.1",
    "react-native-svg": "^9.8.4",
    "react-native-tensorflow": "^0.1.8",
    "res": "^0.4.0",
    "rn-fetch-blob": "github:joltup/rn-fetch-blob#master",
    "tflite-react-native": "0.0.5",
    "util": "^0.12.1"
  },

我声明了moule只能在metro.config.js和react-native.config.js中故意使用我的自定义扩展,如下所示:

module.exports = {
  project: {
    ios: {},
    android: {},
  },
  assets: [
    './src/assets/icons',
    './src/assets/svg',
    './src/assets/ai',
  ],
  assetExts: [
    ".xml", ".png", ".jpg", ".pb", ".tflite"
  ]
};

但消息仍然相同:

error: bundling failed: Error: Unable to resolve module `./ai/annotations/Part1.xml` from 
`...src/assets/default-assets.js`: The module `./ai/annotations/Part1.xml` could not be found from 
`.../src/assets/default-assets.js`. Indeed, none of these files exist:
  * `.../src/assets/ai/annotations/Part1.xml(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`

因此Metro.config.js和react-native.config.js显然被忽略了

更新: 在具有最新依赖关系的全新React-Native应用程序中尝试时,会显示一条验证消息:

● Validation Warning:

  Unknown option "assets" with value ["./src/assets/ai"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

● Validation Warning:

  Unknown option "assetExts" with value [".xml", ".png", ".jpg", ".pb", ".tflite"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

UPDATE2: 我创建了一个仓库来展示问题,并在一个单独的分支上提出了不可行的解决方案建议(使用app.json代替metro.config.js或react-native.config.js)。您可以在这里找到它:

https://github.com/Macilias/ReactNativeAssetFail

2 个答案:

答案 0 :(得分:0)

尝试将它们添加到具有相同结构的react-native.config.js文件中。我的看起来像这样:

module.exports = {
    assets: ["./app/assets/fonts"]
}

别忘了清洁和运行。

答案 1 :(得分:0)

更新,我设法通过从app.json指向metro.config.js来消除错误,如下所示:

{
  "name": "reactNativeTestapp",
  "displayName": "reactNativeTestapp",
  "packagerOpts": {
    "config": "metro.config.js"
  }
}

和metro.config.js的内容如下:

const {getDefaultConfig} = require('metro-config');

module.exports = (async () => {
  const {
    resolver: {sourceExts, assetExts},
  } = await getDefaultConfig();
  return {
    resolver: {
      assetExts: [assetExts, 'txt', 'xml', 'png', 'jpg', 'pb', 'tflite'],
      sourceExts: [...sourceExts, 'txt', 'xml', 'png', 'jpg', 'pb', 'tflite'],
    },
  };
})();

为什么xml文件的内容没有以App.js的形式显示,它只打印出可能与行数相对应的数字?但是我实际上该如何访问文件的内容?