我有一个项目react-native-web
,并且在其上配置了博览会。声明脚本的范围为 imports @
。我看到 metro.config.js 有 expo web 抱怨的那些导入的别名,这里是metro.config.js
:
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
const path = require("path")
const extraNodeModules = {
"@modules": path.resolve(__dirname, "modules"),
"@screens": path.resolve(__dirname, "screens"),
"@options": path.resolve(__dirname, "options")
}
const watchFolders = [
path.resolve(__dirname, "modules"),
path.resolve(__dirname, "screens"),
path.resolve(__dirname, "options")
]
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false
}
})
},
resolver: {
extraNodeModules: new Proxy(extraNodeModules, {
get: (target, name) =>
//redirects dependencies referenced from extraNodeModules to local node_modules
name in target
? target[name]
: path.join(process.cwd(), "node_modules", name)
})
},
watchFolders,
resetCache: true
}
我做了一些研究,并将我babel.config.js
的修改为:
module.exports = {
presets: ['babel-preset-expo', 'module:metro-react-native-babel-preset'],
plugins: [
["module:react-native-dotenv", {
"moduleName": "@env",
"path": ".env",
"blacklist": null,
"whitelist": null,
"safe": false,
"allowUndefined": true
},
],
"import-glob",
[
'module-resolver',
{
alias: {
'modules': './modules',
},
}
]
]
};
我知道上面的 babel 仅适用于模块,仅用于示例。我正在运行这个expo start
然后 w 用于网络:
./App.js:13
Module not found: Can't resolve '@modules'
11 |
12 | import { screens } from "@screens";
> 13 | import { hooks, slices, navigators, initialRoute } from "@modules";
14 | import { connectors } from "@store";
15 |
16 | const Stack = createStackNavigator()
./App.js:18
Module not found: Can't resolve '@options'
16 | const Stack = createStackNavigator()
17 |
> 18 | import { GlobalOptionsContext, OptionsContext, getOptions } from "@options"
19 |
20 | const getNavigation = (modules, screens, initialRoute) => {
21 | const Navigation = () => {
./App.js:12
Module not found: Can't resolve '@screens'
10 | } from "@reduxjs/toolkit"
11 |
> 12 | import { screens } from "@screens";
13 | import { hooks, slices, navigators, initialRoute } from "@modules";
14 | import { connectors } from "@store";
15 |
./App.js:14
Module not found: Can't resolve '@store'
12 | import { screens } from "@screens";
13 | import { hooks, slices, navigators, initialRoute } from "@modules";
> 14 | import { connectors } from "@store";
15 |
16 | const Stack = createStackNavigator()
关于如何使这项工作的任何线索?