React-MobX Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option, whose value must be a boolean

一个人想着一个人 提交于 2019-12-03 11:41:40

The error message is a little bit confusing however with some little bit deep searching, you can resolve it using the following approach.

I make no assumptions, except that you are using webpack in this guide.

You need to add babel proposal decorators to your dev dependencies (You only need it during dev time) (which you have added already).

if using yarn

yarn add --dev @babel/plugin-proposal-decorators 

otheriwse for npm

npm install --save-dev @babel/plugin-proposal-decorators 

then in your package.json file, locate babel config section or add one if not there. The config name is strictly "babel".

  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ]
    ]
  }

Pay extra attention to the indentation if typing it by hand. notice the object "@babel/plugin-proposal-decorators" is deeply nested inside two arrays, so it has to be as such to work.

and just for sanity check, your devDependencies would at a minimum be as

  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.1.2"
  }

Now you can build your application with either yarn or npm and live happily ever after.

React native 0.59

babel.config.js:

{ 
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
        ["@babel/plugin-transform-flow-strip-types"],
        ["@babel/plugin-proposal-decorators", { "legacy": true}],
        ["@babel/plugin-proposal-class-properties", { "loose": true}]
    ]
}

npm install @babel/plugin-transform-flow-strip-types @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties --save

Source: https://github.com/facebook/react-native/issues/20588#issuecomment-448218111

{
 "presets": ['@babel/preset-env', '@babel/preset-react'],
 "plugins": [
  ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "less" }],
  [
    "@babel/plugin-proposal-decorators",
    {
      "decoratorsBeforeExport":true
    }
  ]
 ]
}
sergey andriyaka
"babel": {
   "presets": [
     "react-app"
   ],
  "plugins": [
  [
    "@babel/plugin-proposal-decorators",
    {
      "legacy": true
    }
  ],
  [
    "@babel/plugin-proposal-class-properties",
    {
      "loose": true
    }
   ]
  ]
 },
 "devDependencies": {
 "@babel/plugin-proposal-decorators": "^7.3.0"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!