【问题标题】:Ejected 'Create-React-App' babel config issue弹出 'Create-React-App' babel 配置问题
【发布时间】:2019-06-25 09:51:34
【问题描述】:

在弹出的“create-react-app”中,我安装了react-copy-to-clipboard 包,但遇到了错误:

export class CopyToClipboard extends React.PureComponent {
>  7 |   static propTypes = {
     |                    ^
   8 |     text: PropTypes.string.isRequired,
   9 |     children: PropTypes.element.isRequired,
  10 |     onCopy: PropTypes.func,

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

按照我在网上找到的解决方案,我安装了以下依赖项:

yarn add @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties --dev

并编辑了负责 babel 配置的 package.json 部分:

  "babel": {
    "presets": [
      "react-app",
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-class-properties"
      ]
    ]
  },

还有一些人建议的 webpack.config.js,尽管对大多数人来说这是不必要的:

module: {
  strictExportPresence: true,
  rules: [
    {
      test: path.join(__dirname, '.'),
      exclude: /(node_modules)/,
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env',
          '@babel/react',{
            'plugins': ['@babel/plugin-proposal-class-properties']}]
      }
    },
(...)

我尝试一次更改两个或一个并将 babel 配置移动到新创建的 .babelrc 文件,但似乎没有任何改变,错误仍然存​​在。有什么线索吗?

【问题讨论】:

  • 你为什么要弹出它?代码看起来不错并且应该可以工作(.babelrc 版本),实际上它已经在 create-react-app 中工作了
  • 说实话我记不太清了。我已经在这个项目上工作了很长一段时间,很久以前我就把它弹出了。它不是 .babelrc,因为 create-react-app 中不再有 .babelrc 文件,并且 babel 配置在 package.json 中。
  • 嗯,也许这就是问题所在,我应该认为对 .babelrc 有更好的支持
  • 我尝试将 babel 配置移动到 .babelrc 并且错误仍然存​​在。

标签: webpack babeljs create-react-app


【解决方案1】:

发生错误的类(CopyToClipboard)在 node_modules 文件夹下,但您已排除 babel 在 node_modules 上运行。如果您从 babel 配置中删除 exclude 选项,它应该可以工作,但我不确定这是否是正确的做法。

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 2019-04-22
    • 2017-08-25
    • 2017-03-22
    • 2018-09-10
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多