簡體   English   中英

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

[英]How to shim a dependency in babel.config.js in react-native?

我有一個依賴項,我想對我的 react-native 項目進行填充。 我目前在我的babel.config.js文件中有以下代碼:

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

我發現擴展babel-plugin-module-resolver似乎很有幫助(任何其他替代方案也可以)並嘗試按照他們的示例進行操作,但它們沒有用

我嘗試了以下方法:

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

但這不起作用

我有同樣的錯誤。 運行時的代碼可以正常工作。 問題在於構建。 路徑仍然是絕對的。

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"
  ]
}

屏幕截圖 - 構建后應該是相對的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM