7

I'm trying to add multiple resolvers and transformers using metro for my react native project, how do I combine them in my metro.config.js file?

Background: I want to get both a sass transformer to work as well as a svg transformer.

I've tried the configurations separately and that seems to work, but I'm confused to how I combine them so they both work at the same time. I'm assuming they need to be in the same module.exports, because when they both are in the same file I get errors for my svg's

These are the configs I'm trying to combine:

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-sass-transformer")
    },
    resolver: {
      sourceExts: [...sourceExts, "scss", "sass"]
    }
  };
})();

When I try to run it with the code as above, that is two module exports in the metro.config.js, it seems that only the sass transformer works, when I try to draw an svg I get the following error:

Invariant violation: Element type is invalid: Expected a string (for built-in components) or a class/function (for composite components) but got number.
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Daniel
  • 471
  • 6
  • 16

3 Answers3

22

I solved it by creating a custom transformer as follows:

customTransformer.js :

// For React Native version 0.59 or later
var upstreamTransformer = require("metro-react-native-babel-transformer");
var sassTransformer = require("react-native-sass-transformer");
var svgTransformer = require("react-native-svg-transformer");

module.exports.transform = function({ src, filename, options }) {
  if (filename.endsWith(".scss") || filename.endsWith(".sass")) {
    return sassTransformer.transform({ src, filename, options });
  } else if (filename.endsWith(".svg")) {
    return svgTransformer.transform({ src, filename, options });
  }  else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

And in my metro.config.js:

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

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("./customTransformer.js")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg" && ext!=="scss"),
      sourceExts: [...sourceExts, "svg", "scss", "sass"]
    }
  };
})();

No idea if this is the best way, but it seems to work

Daniel
  • 471
  • 6
  • 16
  • 1
    This is the only way I've found that works. I tried to use mergeConfig from the metro docs, but since it doesn't deeply merge the objects, the transformers always get overwritten by whichever config you merge in last. – Doug Aug 09 '23 at 04:18
3

I solve this problem using mergeConfig from metro-config

const { getDefaultConfig, mergeConfig } = require('metro-config');
/* ... */
module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig();
  const svgTransformer = {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg'],
    },
  };

  const obfuscatingTransformer = {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: false,
        },
      }),
    },
    ...jsoMetroPlugin,
  };

  return mergeConfig(svgTransformer, obfuscatingTransformer);
})();
Oraci
  • 43
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '22 at 21:13
  • This likely won't work. See the source for mergeConfig at https://github.com/facebook/metro/blob/main/packages/metro-config/src/loadConfig.js#L150 - it just picks one of the babelTransformerPaths, it doesn't actually merge them like the accepted answer. – jkjustjoshing Aug 30 '23 at 14:15
2

Try this one:

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

const config1 = {};
const config2 = {};

module.exports = mergeConfig(config1, config2);

It will return the merged configuration of two or more configuration objects.

  • 7
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Oct 16 '20 at 12:17
  • This likely won't work. See the source for mergeConfig at https://github.com/facebook/metro/blob/main/packages/metro-config/src/loadConfig.js#L150 - it just picks one of the babelTransformerPaths, it doesn't actually merge them like the accepted answer. – jkjustjoshing Aug 30 '23 at 14:15