【发布时间】:2022-01-06 19:49:47
【问题描述】:
我在 monorepo 中开发了一个跨平台的 react-native 应用程序,并希望在浏览器中使用 react-native-web 呈现我的应用程序。为此,我遵循了本指南https://mmazzarolo.com/blog/2021-09-22-running-react-native-everywhere-web/。我还使用 metro-react-native-babel-preset 包来编译我的网络应用程序,如 react-native-web 指南 https://necolas.github.io/react-native-web/docs/multi-platform/ 中所述。这是我的 craco.config.js 文件的一部分(我将 create-react-app 与 craco 一起使用):
// craco.config.js
const webpack = require("webpack");
const { getWebpackTools } = require("react-native-monorepo-tools");
const monorepoWebpackTools = getWebpackTools();
module.exports = {
babel: {
presets: ["module:metro-react-native-babel-preset", "@babel/preset-react"]
},
webpack: {
configure: (webpackConfig) => {
// Allow importing from external workspaces.
monorepoWebpackTools.enableWorkspacesResolution(webpackConfig);
// Ensure nohoisted libraries are resolved from this workspace.
monorepoWebpackTools.addNohoistAliases(webpackConfig);
return webpackConfig;
},
现在看来metro-react-native-babel-preset 预设与 stylis 库(由 @emotion/react 导入)不兼容,因为在浏览器中启动应用程序时出现此错误(编译时没有错误):
Uncaught TypeError: (0 , _stylis.middleware) is not a function
at createCache (emotion-cache.browser.esm.js:288)
at Object.../node_modules/@emotion/react/dist/emotion-element-699e6908.browser.esm.js (emotion-element-699e6908.browser.esm.js:11)
at __webpack_require__ (bootstrap:851)
at fn (bootstrap:150)
at Object.<anonymous> (emotion-react.browser.esm.js:3)
at Object.../node_modules/@emotion/react/dist/emotion-react.browser.esm.js (emotion-react.browser.esm.js:347)
at __webpack_require__ (bootstrap:851)
at fn (bootstrap:150)
at Object.../node_modules...
我猜由于metro-react-native-babel-preset 预设,stylis-package 无法正确导入,因为没有预设错误就消失了(但编译步骤会引发错误,因此删除预设不是解决方案)。
我必须在我的 babel-/webpack-config 或代码中进行哪些更改才能消除此错误?
最小的、可重现的例子
【问题讨论】:
标签: javascript reactjs react-native webpack babeljs