繁体   English   中英

custom-cra - 当前未启用对实验语法“jsx”的支持

[英]customize-cra - Support for the experimental syntax 'jsx' isn't currently enabled

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

一切都很好,直到我需要安装react-native-calendars作为依赖项。 正如您在我的addBabelPlugins配置中看到的那样; 我需要安装@babel/plugin-proposal-class-properties来转译库的组件......然而,我现在收到一个错误,抱怨我的代码库中的 JSX,我认为这已经由 CRA 处理了放置并看到我们正在使用react-app-rewired覆盖/扩展 CRA,我认为我不需要添加其他插件/预设,例如@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'),
  }),
);

原始问题在这里

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM