0

我正在使用 react-app-rewired 来配置我的 CRA 项目,因为我遇到了 2 个共存版本的 React 问题,我确信这是react-app-rewired/customize-cra.

一切都很好,直到我需要安装react-native-calendars为依赖项。正如您在我的addBabelPlugins配置中看到的那样;我需要安装@babel/plugin-proposal-class-properties以转换库的组件...但是我现在收到一个错误,抱怨我的代码库中的 JSX,我认为首先应该由 CRA 处理并看到我们正在覆盖/扩展 CRAreact-app-rewired我认为我不需要添加其他插件/预设,例如@babel/preset-react& @babel/plugin-syntax-jsx...

有人可以指出我正确的方向吗?我希望这个问题很常见,我刚刚搜索的问题页面很糟糕。

谢谢

信息

react@17.0.1
react-scripts@3.4.3
customize-cra@1.0.0 
npm@6.14.9
node@14.8.0

错误

Failed to compile.

./src/App.js
SyntaxError: /Users/.../src/App.js: Support for the experimental syntax 'jsx' isn't currently enabled (28:5):

  26 | 
  27 |   return (
> 28 |     <Router>
     |     ^
  29 |       <Switch>

Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

配置覆盖.js

const {
  override,
  addWebpackAlias,
  babelInclude,
  addBabelPresets,
  addBabelPlugins,
} = require('customize-cra');

module.exports = override(
  babelInclude([
    require.resolve('./src'),
    require.resolve('./node_modules/react-native-calendars'),
  ]),
  ...addBabelPresets(
    '@babel/preset-react',
  ),
  ...addBabelPlugins(
    '@babel/plugin-proposal-class-properties', // <~ needed for react-native-calendars
    '@babel/plugin-syntax-jsx',
  ),
  addWebpackAlias({
    react: require.resolve('./node_modules/react'),
  }),
);

原始问题在这里

4

2 回答 2

1

这个答案解决了我的问题。

希望这可以帮助其他人面临这个问题!

于 2020-12-01T11:08:04.390 回答
0

像这样替换jsconfig.json/中的 jsx 配置选项tsconfig.json

{
  "compilerOptions": {
    ...
-   "jsx": "react-jsx"
+   "jsx": "react"
  }
}

我在这里找到了。更多关于 jsconfig 的信息在这里

于 2020-11-30T19:49:24.353 回答