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

前端 未结 5 660
無奈伤痛
無奈伤痛 2021-02-12 11:52

I get the following error: If you are migrating from Babylon/Babel 6 or want to use the old decorators proposal, you should use the \'decorators-legacy\' plugin instead of \'dec

相关标签:
5条回答
  • 2021-02-12 12:33
    "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"
    }
    
    0 讨论(0)
  • 2021-02-12 12:41
    {
     "presets": ['@babel/preset-env', '@babel/preset-react'],
     "plugins": [
      ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "less" }],
      [
        "@babel/plugin-proposal-decorators",
        {
          "decoratorsBeforeExport":true
        }
      ]
     ]
    }
    
    0 讨论(0)
  • 2021-02-12 12:42

    Answer are in the official document: https://mobx.js.org/best/decorators.html

    you can find many ways to enable it in section "Enabling decorator syntax"

    take the Babel 7 for example, create a project using mobx+create-react-app from scratch:

    npx create-react-app hello-mobx
    
    //This moves files around and makes your app’s configuration accessible.
    npm run eject
    
    npm install --save-dev babel-plugin-transform-decorators-legacy
    npm install --save-dev @babel/plugin-proposal-decorators
    npm install --save-dev @babel/plugin-proposal-class-properties
    
    

    edit package.json: package.json:

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

    install mobx:

    npm install mobx --save
    npm install mobx-react --save
    

    enjoy!

    0 讨论(0)
  • 2021-02-12 12:50

    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.

    0 讨论(0)
  • 2021-02-12 12:51

    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

    0 讨论(0)
提交回复
热议问题