@ babel / plugin-proposal-class-properties仍失败,但“当前未启用classProperties”

时间:2020-10-29 16:58:50

标签: javascript react-native babeljs

尝试使用@babel/plugin-proposal-class-properties

A

产生此错误

Failed to compile.

./node_modules/react-native-animatable/createAnimatableComponent.js
SyntaxError: /Users/timothyw/Projects/beatthemarket/beatthemarket.reactnative-paper/beatthemarket.frontend/node_modules/react-native-animatable/createAnimatableComponent.js: Support for the experimental syntax 'classProperties' isn't currently enabled (133:24):

  131 |
  132 |   return class AnimatableComponent extends Component {
> 133 |     static displayName = `withAnimatable(${wrappedComponentName})`;
      |                        ^
  134 |
  135 |     static propTypes = {
  136 |       animation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),

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

B

安装和代码如下。

sh

yarn add @babel/plugin-proposal-class-properties --dev

MyComponent.js

...
<Animatable.View
 animation="pulse"
 easing="ease-out"
 iterationCount="infinite">
 <SocialButton
   onPress={signInWithGoogle}
   imageSource={IMAGES.GOOGLE}
   style={{
     container: themedStyles.googleIconContainer,
     image: themedStyles.googleIcon,
   }}
 />
</Animatable.View>
...

babel.config.js

module.exports = {
  presets: [ "module:metro-react-native-babel-preset", "@babel/preset-env", "@babel/preset-react" ],
  plugins: [ ["@babel/plugin-proposal-class-properties", {"loose": true}] ],
}

C

这些SO注释似乎没有帮助。

1 个答案:

答案 0 :(得分:0)

您必须安装

npm install @babel/core @babel/plugin-proposal-class-properties @babel/preset-env @babel/preset-react babel-loader

<块引用>

更改输入和输出


const path = require('path')        
module.exports = {
  entry: path.resolve(__dirname,'src', 'app.js'),
  output: {
    path: path.resolve(__dirname, "public","dist",'javascript'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.(jsx|js)$/,
        exclude: /node_modules/,
        use: [{
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', {
                "targets": "defaults"
              }],
              '@babel/preset-react'
            ],
            plugins: [
              "@babel/plugin-proposal-class-properties"
            ]
          }
        }]
      }
    ]
  }
}

在文件webpack.config.js