kristerkari / react-native-svg-transformer

Import SVG files in your React Native project the same way that you would in a Web application.
MIT License
1.56k stars 117 forks source link

Config not working in monorepo containing multiple apps #177

Open bkeph opened 2 years ago

bkeph commented 2 years ago

I have a monorepo containing multiple apps managed through yarn workspaces, each with it's own metro.config.js file.

module.exports = (async () => {
  const {
    resolver: {sourceExts, assetExts},
  } = await getDefaultConfig();
  return {
    projectRoot: path.resolve(__dirname, "../../"),
    watchFolders: [path.resolve(__dirname, "../../")],
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: false,
        },
      }),
      babelTransformerPath: require.resolve(
        "./react-native-svg-transformer.js",
      ),
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"],
    },
  };
})();

If I make the modifications above to the metro.config.js, I get the error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: number.

But if I move the metro.config.js to the root folder, it works properly. However, I cannot move it into the root folder, because each app has a different metro configuration.

Why doesn't it work if the config is in the specific app folder, and how should I adapt the config in order to make it work?

// react-native-svg-transformer.js
const svgTransformer = require("react-native-svg-transformer");
const upstreamTransformer = require("metro-babel-transformer");

module.exports.transform = function ({src, filename, options}) {
  if (filename.endsWith(".svg")) {
    return svgTransformer.transform({src, filename, options});
  } else {
    return upstreamTransformer.transform({src, filename, options});
  }
};
kristerkari commented 2 years ago

Thanks @bkeph ! I have to admit, that I don't have a lot of experience with working monorepos, so I can't provide you with any quick fix.

I'm suspecting that some of the config/transformer is not pointing to the correct folder, and is failing because of that.

Maybe you can check that is the file path for babelTransformerPath correct regarding the setup that you have?

ajiehatajie commented 1 year ago

same issue with turborepo.

config metro https://raw.githubusercontent.com/Enricopv/turbo-boilerplate/master/apps/rnative/metro.config.js

PiotrekPKP commented 1 year ago

Same issue here, using Turborepo with NextJS and React Native

whalemare commented 11 months ago

about .svgrrc file loading

https://github.com/kristerkari/react-native-svg-transformer/issues/217#issuecomment-1732500149