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.54k stars 116 forks source link

how to configure metro.config.js in 0.72.0 #276

Closed adsalihac closed 1 year ago

adsalihac commented 1 year ago

This version they are changed metro config.

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config'); const config = {}; module.exports = mergeConfig(getDefaultConfig(__dirname), config);

ngondat97 commented 1 year ago

same issue

jackfiallos commented 1 year ago

I have added something like this, metro its running now but I haven't confirmed if the app will work after a RN 0.72.0 upgrade

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts;
const defaultAssetExts = require('metro-config/src/defaults/defaults').assetExts;
/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */

module.exports = mergeConfig(getDefaultConfig(__dirname), {
    transformer: {
        babelTransformerPath: require.resolve('react-native-svg-transformer'),
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        assetExts: defaultAssetExts.filter(ext => ext !== 'svg'),
        sourceExts: [...defaultSourceExts, 'svg'],
    },
});
adsalihac commented 1 year ago

yes

stefkampen commented 1 year ago

works like a charm

bayuengx1 commented 1 year ago

I have added something like this, metro its running now but I haven't confirmed if the app will work after a RN 0.72.0 upgrade

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts;
const defaultAssetExts = require('metro-config/src/defaults/defaults').assetExts;
/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */

module.exports = mergeConfig(getDefaultConfig(__dirname), {
    transformer: {
        babelTransformerPath: require.resolve('react-native-svg-transformer'),
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        assetExts: defaultAssetExts.filter(ext => ext !== 'svg'),
        sourceExts: [...defaultSourceExts, 'svg'],
    },
});

Very helping bro, thank u so much. You save my life 😭

abgaryanharutyun commented 1 year ago

I have added something like this, metro its running now but I haven't confirmed if the app will work after a RN 0.72.0 upgrade

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts;
const defaultAssetExts = require('metro-config/src/defaults/defaults').assetExts;
/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */

module.exports = mergeConfig(getDefaultConfig(__dirname), {
    transformer: {
        babelTransformerPath: require.resolve('react-native-svg-transformer'),
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        assetExts: defaultAssetExts.filter(ext => ext !== 'svg'),
        sourceExts: [...defaultSourceExts, 'svg'],
    },
});

Thank you so much @jackfiallos, this works 100000% 🚀

RominHalltari commented 1 year ago

This does not work for me, I'm getting the following error: image

Adnan-Bacic commented 1 year ago

I have added something like this, metro its running now but I haven't confirmed if the app will work after a RN 0.72.0 upgrade

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts;
const defaultAssetExts = require('metro-config/src/defaults/defaults').assetExts;
/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */

module.exports = mergeConfig(getDefaultConfig(__dirname), {
    transformer: {
        babelTransformerPath: require.resolve('react-native-svg-transformer'),
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        assetExts: defaultAssetExts.filter(ext => ext !== 'svg'),
        sourceExts: [...defaultSourceExts, 'svg'],
    },
});

this works for me. i have made a small change. i think the empty config variable in the new template is meant as a way to have our own config for readability if we want to avoid writing our own config inside the default config. then the new mergeConfig function merges them together.

so alternatively, i have it like this:

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

const sourceExts = require('metro-config/src/defaults/defaults').sourceExts;
const assetExts = require('metro-config/src/defaults/defaults').assetExts;

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
      getTransformOptions: async () => {
        return {
          transform: {
            experimentalImportSupport: false,
            inlineRequires: true,
          },
        };
      },
    },
    resolver: {
      assetExts: assetExts.filter((ext) => { return ext !== 'svg'; }),
      sourceExts: [...sourceExts, 'svg'],
    },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);
kristerkari commented 1 year ago

I'll update the docs as soon as I have tested that everything works with the suggested config / PR 🙂

wjaykim commented 1 year ago

From React Native 0.72.1 (with "@react-native/metro-config": "^0.72.7"), following works, too:

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  transformer: {
    babelTransformerPath: require.resolve('react-native-svg-transformer'),
  },
  resolver: {
    assetExts: assetExts.filter(ext => ext !== 'svg'),
    sourceExts: [...sourceExts, 'svg'],
  },
};

module.exports = mergeConfig(defaultConfig, config);
trmylinh commented 1 year ago

This does not work for me, I'm getting the following error: image

Do you have a solution to fix that ?

wjaykim commented 1 year ago

@trmylinh Run react-native start --reset-cache

trmylinh commented 1 year ago

@trmylinh Run react-native start --reset-cache

when I run react-native --resert-cache, it throw error 'Cannot read properties of undefined (reading 'filter').' in file metro.config.js @@

wjaykim commented 1 year ago

when I run react-native --resert-cache, it throw error 'Cannot read properties of undefined (reading 'filter').' in file metro.config.js @@

You should update react-native to 0.72.1 and @react-native/metro-config to ^0.72.7

trmylinh commented 1 year ago

when I run react-native --resert-cache, it throw error 'Cannot read properties of undefined (reading 'filter').' in file metro.config.js @@

You should update react-native to 0.72.1 and @react-native/metro-config to ^0.72.7

yeah, I have done update version like that, but it throw same error T.T

trmylinh commented 1 year ago

@trmylinh Run react-native start --reset-cache

oh now it works, thank you so much

adsalihac commented 1 year ago

https://github.com/kristerkari/react-native-svg-transformer/blob/master/README.md

it is updated in README.md file.

MobileMon commented 10 months ago

In addition to what was said above, I also had to run

npm install @react-native/metro-config