如何在Expo和React Native中使用绝对路径导入?

a7qyws3x  于 2023-01-05  发布在  React
关注(0)|答案(7)|浏览(574)

我尝试使用绝对导入路径,而不是Expo和React Native的相对路径。
我查看了Expo文档,但找不到答案...在react社区中搜索主题时,我找到了babel-plugin-module-resolver,但Expo似乎已经在使用它,因此我更改了我的.babelrc以创建一些别名:

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": [
        "transform-react-jsx-source",
        ["module-resolver", {
          "root": ["./app"],
          "alias": {
            "Components": "./app/components",
          }
        }]
      ]
    }
  }
}

但我得到了以下错误:

Unable to resolve module '@expo/vector-icons/glyphmaps/Entypo.json' 
    from '/Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/Entypo.js': 
    Module does not exist in the module map or in these directories:   /Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/node_modules/@expo/vector-icons/glyphmaps ,   /Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/glyphmaps  This might be related to https://github.com/facebook/react-native/issues/4968 To resolve try the following:   
    1. Clear watchman watches: 'watchman watch-del-all'.   
    2. Delete the 'node_modules' folder: 'rm -rf node_modules && npm install'.   
    3. Reset packager cache: 'rm -fr $TMPDIR/react-*' or 'npm start --reset-cache'.
    ABI16_0_0RCTFatal -[ABI16_0_0RCTBatchedBridge stopLoadingWithError:] __34-[ABI16_0_0RCTBatchedBridge start]_block_invoke_2 _dispatch_call_block_and_release _dispatch_client_callout _dispatch_main_queue_callback_4CF __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ __CFRunLoopRun CFRunLoopRunSpecific GSEventRunModal UIApplicationMain main start 0x0

有什么简单的方法可以导入绝对路径吗?

gojuced7

gojuced71#

更新:Expo v32.0.0及更高版本
Expo init正在为您创建一个babel.config.js。只需将plugins密钥添加到您的babel.config.js文件并添加您的别名。不再需要env密钥。

module.exports = function(api) {
  api.cache(true)

  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          alias: {
            assets: './assets',
            components: './src/components',
            modules: './src/modules',
            lib: './src/lib',
            types: './src/types',
            constants: './src/constants',
          },
        },
      ],
    ],
  }
}

更新:Expo.io SDK v20.0.0的变更
从SDK v20.0.0开始,您可以使用常规的巴别塔博览会预设

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "alias-name": "./app",
      }
    }]
  ]
}

Expo.io v19.0.0及之前版本
如果没有root-元素,分隔plugins并将presets更改为babel-preset-react-native-stage-0/decorator-support,则别名对我来说是有效的。
在Expo.io版本16.0.0上使用www.example.com
在世博会论坛上找到了这个:https://forums.expo.io/t/relative-paths-with-expo/654/3
你能证明这对你的案子也有效吗?

{
  "presets": ["babel-preset-react-native-stage-0/decorator-support"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "alias-name": "./app",
      }
    }]
  ]
}
5t7ly7z5

5t7ly7z52#

对于最新的expo用户(sdk版本〉= 32)。
只需在babel.config.js中添加plugins属性(在项目根目录中找到此文件)。

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          alias: {
            'alias-name': './src/assets/bla/bla',
          },
        },
      ],
    ],
  };
};
cwdobuhd

cwdobuhd3#

经过一段时间的尝试,我可以解决以下.babelrc的问题:

{
  "presets": ["babel-preset-react-native-stage-0/decorator-support"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "react-native-vector-icons": "@expo/vector-icons",
        "@expo/vector-icons/lib/create-icon-set": "react-native-vector-icons/lib/create-icon-set",
        "@expo/vector-icons/lib/icon-button": "react-native-vector-icons/lib/icon-button",
        "@expo/vector-icons/lib/create-icon-set-from-fontello": "react-native-vector-icons/lib/create-icon-set-from-fontello",
        "@expo/vector-icons/lib/create-icon-set-from-icomoon": "react-native-vector-icons/lib/create-icon-set-from-icomoon",
        "@expo/vector-icons/lib/icon-button": "react-native-vector-icons/lib/icon-button",
        "@expo/vector-icons/glyphmaps": "react-native-vector-icons/glyphmaps",
        "$components": "./app/components",
        "$config": "./app/config",
        "$helpers": "./app/helpers",
        "$navigators": "./app/navigators",
        "$reducers": "./app/reducers",
        "$screens": "./app/screens",
        "$images": "./assets/images",
        "$fonts": "./assets/fonts",
        "$icons": "./assets/icons",
        "$videos": "./assets/videos",
      }
    }]
  ]
}

我把babel-preset-expo的内容拷贝到我的.babelrc上,这不是最好的解决方案......但是可以正常工作。
更多详情here

vhmi4jdf

vhmi4jdf4#

./src
 |- package.json
 |- Bar/
 |   `- index.js
 `- Foo.js

package.json

{
  "name": "~" // whatever u want
}

那么

import { Foo } from "~/Foo";
import { Bar } from "~/Bar";
// ...
67up9zun

67up9zun5#

Laszlo Schurg's answer的版本中,以防有人在 typescript 中遇到同样的问题。
如果您正在使用Typescript,则还需要将其添加到tsconfig

"compilerOptions": {
  ...
  "baseUrl": ".",
  "paths": {
    "@src/*": ["./src/*"],
    "@assets/*": ["./assets/*"]
  }
},

这是我对babel.config的配置

plugins: [
  ...
  [
    "module-resolver",
    {
      alias: {
        "@assets": "./assets",
        "@src": "./src",
      },
    },
  ],
],
tjvv9vkg

tjvv9vkg6#

我同意这个建议:
在您的tsconfig.json中:

<pre>
     <code>
{
  "compilerOptions": {
    "baseUrl": "./app",
    "paths": {
      "Components": ["components/*"]
    }
  },
  "include": ["app"]
}
     </code>
  </pre>

vite.config.ts解析可能如下所示:

<pre>
    <code>
resolve: {
    alias: {
      "Components": "app/components",
    }
}
    </code>
 </pre>
mo49yndu

mo49yndu7#

只是简单的让你.babelrc简单这样:

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  }
}

并像这样导入:

import Entypo from '@expo/vector-icons/Entypo';

相关问题