8

I'm trying to bundle markdown files without creating much overhead (i.e. not adding them manually to the asset bundles in Xcode and Android Studio, not using 3rd party dependencies).

My idea was to allow require() to include them by adjusting the metro bundler settings in metro.config.js:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

module.exports = {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: false,
            },
        }),
    },
    resolver: {
        assetExts: [`md`] // < include md
    }
};

Sadly metro bundler replaces the array of given defaults with what's set here.

I don't want to explicitly repeat the default asset extension list which lists about 20+ file extensions, especially since I want to stick to the defaults otherwise. See: https://github.com/facebook/metro/blob/master/packages/metro-config/src/defaults/defaults.js.

Appending to the array does not work, too.

Using RN 0.59.3.

Anything I'm missing?

Frederik Winkelsdorf
  • 4,383
  • 1
  • 34
  • 42

2 Answers2

18

Found the answer on how to include the defaults here: https://stackoverflow.com/a/55118654/844907.

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

// get defaults assetExts array
const defaultAssetExts = require("metro-config/src/defaults/defaults").assetExts;

module.exports = {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: false,
            },
        }),
    },
    resolver: {
        assetExts: [
            ...defaultAssetExts, // <- array spreading defaults
            'md'
        ]
    }
};
Frederik Winkelsdorf
  • 4,383
  • 1
  • 34
  • 42
  • Could you please help on this issue? https://stackoverflow.com/questions/64625169/react-native-could-not-resolve-module-masked-view-from-react-navigation-stack – newdeveloper Nov 01 '20 at 03:18
3

Other way you could do this is use the same syntax as react-native-svg-transformer

  1. Make metro.config.js asynchronous
  2. call the getDefaultConfig
  3. Pull off the assetExts key

Example

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

module.exports = (async () => {
    const {
        resolver: { assetExts },
    } = await getDefaultConfig()
    return {
        transformer: {
            getTransformOptions: async () => ({
                transform: {
                    experimentalImportSupport: false,
                    inlineRequires: false,
                },
            }),
        },
        resolver: {
            assetExts: [...assetExts, 'md'],
        },
    }
})()
fengelhardt
  • 965
  • 11
  • 24
  • Looks a bit nicer but doesn't really change the functionality: getting default config (asynchronously), setting transformation options and adding assets. Fun fact: Looking at the revision history they published the solution about the same week as I did. Thanks for the hint! – Frederik Winkelsdorf Aug 24 '19 at 07:56