如何让装饰工作与babel&的WebPack?

时间:2016-02-08 11:43:06

标签: javascript decorator webpack babeljs ecmascript-next

我有以下设置:

{
  "babel-core": "~5.8.25",
  "babel-eslint": "^4.1.3",
  "babel-loader": "~5.3.2",
  "babel-polyfill": "^6.2.0",
  "eslint": "^1.7.3",
  "eslint-config-airbnb": "^0.1.0",
  "eslint-loader": "~1.1.0",
  "eslint-plugin-angular": "~0.12.0",
  // ...
}

的WebPack:

module: {
  preLoaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'eslint-loader'}],
  loaders: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      loaders: ['ng-annotate', 'babel-loader?plugins[]=transform-decorators-legacy'],
    }
  ]
}

但是我收到以下错误:

TypeError: The plugin "transform-decorators-legacy" didn't export a Plugin instance 

有谁知道我在这里做错了什么?

更新

我已升级到Babel 6,现在进行以下设置:

{
  "babel-core": "^6.0.0",
  "babel-eslint": "^4.1.3",
  "babel-loader": "^6.0.0",
  "babel-plugin-transform-decorators-legacy": "^1.3.4",
  "babel-polyfill": "^6.2.0",
  "babel-preset-es2015": "^6.0.0",
  "babel-preset-stage-0": "^6.5.0",
  "eslint": "^1.10.0",
  "eslint-config-airbnb": "^4.0.0",
  "eslint-loader": "^1.2.0",
  "eslint-plugin-angular": "^0.15.0",
  // ...
}

module: {
  preLoaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'eslint-loader'}],
  loaders: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      loaders: ['ng-annotate', 'babel?presets[]=es2015&presets[]=stage-0&plugins[]=transform-decorators-legacy'],
    }
  ]
},

但是请Parsing error: Unexpected token ILLEGAL引用装饰器。

1 个答案:

答案 0 :(得分:2)

经过一些搜索和this SO answer,我能够在不使用.babelrc文件的情况下使其正常运行。

安装transform-decorators-legacy

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

将插件包含在webpack.config.js

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