【问题标题】:Unable to use babel-plugin-module-resolver with expo and react-native无法将 babel-plugin-module-resolver 与 expo 和 react-native 一起使用
【发布时间】:2019-05-02 22:06:30
【问题描述】:

我是 Expo 和 React Native 的新手。在导入模块时,我在使用绝对路径时遇到了一些问题。我遇到了众所周知的 babel-plugin-module-resolver 插件,但我无法正确使用它。关于这个问题,我几乎做了所有在不同线程上发现的事情,但我仍然无法在 VS Code 中使用绝对路径(使用 TypeScript)。在我的 expo 项目中,我也没有任何 .babelrc 文件,而是有 babel.config.js 文件,我不知道为什么会这样。可能是什么问题? TIA

【问题讨论】:

  • 你解决了吗?
  • 不 :( 仍然使用相对路径

标签: typescript react-native expo


【解决方案1】:

我的似乎在下面工作。试试这个

expo-typescript 项目示例 https://github.com/thu-san/expo-typescript-example

babel.config.js

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          extensions: [
            '.js',
            '.jsx',
            '.ts',
            '.tsx',
            '.android.js',
            '.android.tsx',
            '.ios.js',
            '.ios.tsx'
          ],
          root: ['./src']
        }
      ]
    ]
  };
};

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "jsx": "react-native",
    "lib": ["es2017"],
    "module": "es2015",
    "moduleResolution": "node",
    "noEmitHelpers": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "sourceMap": false,
    "strict": true,
    "target": "es2017"
  },
  "exclude": ["node_modules"]
}

【讨论】:

  • 是的,带有 sdk 32 的博览会
  • 另外我的项目中没有 tsconfig.json 文件,我应该手动创建一个吗?
  • 我将在 github 上分享我的工作代码。只是带有一些屏幕的入门代码。
  • 我在这里创建了示例项目github.com/thu-san/expo-typescript-example
  • 哦,伙计,非常感谢你......终于我让它工作了。再次感谢老哥。现在不需要在 GitHub 上上传代码 :)
猜你喜欢
  • 2019-05-19
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 2020-07-29
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多