【问题标题】:Babel failing to compile decorator syntax, despite correct plugin尽管插件正确,但 Babel 无法编译装饰器语法
【发布时间】:2017-05-25 11:53:33
【问题描述】:

尝试在我的 React 项目中使用装饰器,尽管我已经安装了启用它们的 babel 插件,并在 .babelrc 中引用了该插件,但我的控制台中仍然出现语法错误。

package.json

// ...
  "devDependencies": {
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-object-rest-spread": "^6.16.0",
    "chai": "^3.5.0",
    "file-loader": "^0.9.0",
    "url-loader": "^0.5.7"
  }
// ...

.babelrc - 我之前安装了transform-object-rest-spread 插件,所以刚刚扩展了plugins 数组:

{
  "plugins": ["transform-object-rest-spread", "transform-decorators-legacy"]
}

以及控制台错误,完整(以防万一):

BabelLoaderError: SyntaxError: Decorators are not officially supported yet in 6.x pending a proposal update.
However, if you need to use them you can install the legacy decorators transform with:

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

and add the following line to your .babelrc file:

{
  "plugins": ["transform-decorators-legacy"]
}

The repo url is: https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy.

我错过了什么?

【问题讨论】:

  • 同时发布你的 webpack 配置
  • 糟糕,刚收到您的回复,我就解决了:) 查看答案...

标签: reactjs plugins webpack decorator babeljs


【解决方案1】:

我已将插件包含在 .babelrc 但不是 webpack.config.js(正如 azium 指出的那样)。

当我加入这个时,它起作用了:

// ...
query: {
   plugins: [ 'transform-decorators-legacy' ]
}
// ...

希望在其他人遇到此问题时有所帮助:)

【讨论】:

  • 那么哪个优先? .babelrc 文件或 webpack 中的 babel 配置?它会合并它们吗?
  • 哦,有趣。看起来我们可以将bablerc: false 添加到 webpack,在这种情况下,它将真的只使用 webpack 配置。除此之外,仍然不确定会发生什么! gist.github.com/rmoorman/94eeed830942758e218d92f15ce58d88
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多