如何在react-native中填充babel.config.js中的依赖项?

nfs0ujit  于 2022-12-08  发布在  Babel
关注(0)|答案(1)|浏览(165)

我想在react-native项目上添加一个依赖项。我目前在babel.config.js文件上有以下代码:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      
    ],
  };
};

我已经找到了扩展babel-plugin-module-resolver,这似乎是有帮助的(任何其他替代品也会工作),并试图遵循their examples,但他们没有工作
我尝试了以下方法:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          root: ["./src"],
          alias: {
            '@dependency/to-shim': 'path/to-shimmer',
          },
        },
      ],
    ],
  };
};

但那不管用

lmvvr0a8

lmvvr0a81#

我也遇到了同样的错误。代码运行时工作正常。问题是构建。路径仍然是绝对的。
babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        root: ['.'],
        alias: {
          '@services': './src/services',
        },
      },
    ],
    'react-native-reanimated/plugin',
  ],
};

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@services*": ["./src/services"]
    },
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "esModuleInterop": true,
    "importsNotUsedAsValues": "error",
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "lib": ["esnext"],
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitUseStrict": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": false,
    "target": "esnext"
  },
  "include": ["src"],
  "exclude": [
    "node_modules",
    "commitlint.config.js",
    "metro.config.js",
    "jest.config.js",
    "babel.config.js"
  ]
}

screenshot - should be relative after build

相关问题