简体   繁体   English

带有装饰器和 class 属性的 Babel 配置不起作用

[英]Babel configuration with decorators & class properties not working

I have a simple class which needs to be tested using Jest.我有一个简单的 class 需要使用 Jest 进行测试。 The babel configuration for the project I am using -我正在使用的项目的 babel 配置 -

Babel version - 7通天塔版本 - 7

{
  "plugins": [
    "@babel/plugin-transform-react-jsx",
    "@babel/plugin-proposal-function-bind",
    "@babel/plugin-transform-modules-commonjs",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-classes",
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ],
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ],
  "presets": ["@babel/preset-env", "@babel/react"]
}


I am getting an error -我收到一个错误 -

  SyntaxError: Missing class properties transform.
  5 | 
       6 | export default class Template {
    >  7 |   id;

So, I figured out the issue @babel/plugin-proposal-decorators should come before @babel/plugin-proposal-class-properties所以,我发现@babel/plugin-proposal-decorators 应该出现在@babel/plugin-proposal-class-properties 之前

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["@babel/plugin-transform-react-jsx"],
    "@babel/plugin-transform-modules-commonjs",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-proposal-function-bind",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    "@babel/plugin-proposal-class-properties"
  ]
}

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

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