配置 babel.config.js 在 React Native 项目中。

huangapple go评论52阅读模式
英文:

configuring babel.config.js on react native project

问题

I need to configure my babel.config.js to use react-native-vision-camera as:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'react-native-reanimated/plugin',
      {
        globals: ['__scanCodes'],
      },
    ],
  ],
};

but my current configuration is as:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    'react-native-reanimated/plugin',
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
  ],
};

Where to put my globals?

All the configurations I have tried are not working

Thanks!

英文:

I need to configure my babel.config.js to use react-native-vision-camera as:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'react-native-reanimated/plugin',
      {
        globals: ['__scanCodes'],
      },
    ],
  ],
};

but my current configuration is as:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    'react-native-reanimated/plugin',
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
  ],
};

Where to put my globals?

All the configurations I have tried are not working

Thanks!

答案1

得分: 1

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    ['react-native-reanimated/plugin', 
      { 
         globals: ['__scanCodes'],
      }
    ],
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
  ],
};
英文:
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    ['react-native-reanimated/plugin', 
      { 
         globals: ['__scanCodes'],
      }
    ],
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
  ],
};

答案2

得分: 0

这个技巧是,reanimated 建议将他们的插件放在最后使用!!

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
    [
      'react-native-reanimated/plugin',
      {
        globals: ['__scanCodes'],
      },
    ],
  ],
};
英文:

the trick is that reanimated warns to use their plugin LAST!!

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'nativewind/babel',
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '^@src/(.+)': './src/\',
          '^@assets/(.+)': './src/assets/\',
          '^@components/(.+)': './src/components/\',
          '^@contexts/(.+)': './src/contexts/\',
          '^@models/(.+)': './src/models/\',
          '^@navigation/(.+)': './src/navigation/\',
          '^@screens/(.+)': './src/screens/\',
          '^@services/(.+)': './src/services/\',
          '^@utils/(.+)': './src/utils/\',
        },
      },
    ],
    [
      'react-native-reanimated/plugin',
      {
        globals: ['__scanCodes'],
      },
    ],
  ],
};

huangapple
  • 本文由 发表于 2023年2月6日 13:31:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定