Search code examples
javascriptreact-nativemetro-bundler

react native adding custom assets not possible, metro.config.js ignored


I'm trying to use custom assets within a react native application with following configuration:

"dependencies": {
    "glob": "^7.1.4",
    "metro-config": "latest",
    "react": "16.8.6",
    "react-native": "0.60.5",
    "react-native-camera": "git+https://[email protected]/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"
  },

I declared the moule to use ONLY my custom extensions on purpose within the metro.config.js and react-native.config.js like this:

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

but the message is still the same:

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)`

so the metro.config.js and react-native.config.js are clearly ignored

UPDATE: When trying within a brand new react-native app with latest dependencies, one is presented with an validation message:

● 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: I created a repo showcasing the problem and also with a not working solution suggestion (using app.json instead of metro.config.js or react-native.config.js) on an separate branch. You can find it here:

https://github.com/Macilias/ReactNativeAssetFail


Solution

  • Update, I managed to get rid of the error by pointing to metro.config.js from app.json like this:

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

    and the content of the metro.config.js looks like this:

    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'],
        },
      };
    })();
    

    how ever the content of the xml file is not shown in the form App.js, it only prints a number maybe corresponding to the number of lines?! But how can I actually access the content of the file?