Webpack babel 6 ES6 decorators

后端 未结 5 953
一个人的身影
一个人的身影 2020-11-28 04:47

I\'ve got a project written in ES6 with webpack as my bundler. Most of the transpiling works fine, but when I try to include decorators anywhere, I get this error:

5条回答
  •  孤城傲影
    2020-11-28 05:19

    This Babel plugin worked for me:

    https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy

    npm i --save-dev babel-plugin-transform-decorators-legacy
    

    .babelrc

    {
      "presets": ["es2015", "stage-0", "react"],
      "plugins": [
        ["transform-decorators-legacy"],
        // ...
      ]
    }
    

    or

    Webpack

    {
      test: /\.jsx?$/,
      loader: 'babel',
      query: {
        cacheDirectory: true,
        plugins: ['transform-decorators-legacy' ],
        presets: ['es2015', 'stage-0', 'react']
      }
    }
    

    React Native

    With react-native you must use the babel-preset-react-native-stage-0 plugin instead.

    npm i --save babel-preset-react-native-stage-0
    

    .babelrc

    {
      "presets": ["react-native-stage-0/decorator-support"]
    }
    

    Please see this question and answer for a complete explanation.

提交回复
热议问题