BabelLoaderError:SyntaxError:缺少类属性转换

时间:2016-11-05 10:52:00

标签: reactjs webpack babeljs arrow-functions class-properties

当我尝试运行我的dev-server时收到以下消息:

BabelLoaderError: SyntaxError: Missing class properties transform.

我已经安装了class-properties babel插件,我已将其合并到我的.babelrc文件中:

.babelrc

{ "presets": ["es2015", "stage-2"],
  "plugins": ["transform-class-properties"]
}

但仍然不会渲染。如果我删除这个ES6箭头函数 - add = (a, b) => a + b - 它渲染得很完美,但是类属性插件似乎没有修复它......

如果有人能发现出了什么问题,我们将不胜感激。

的package.json

{
  "name": "judo-heroes",
  "version": "1.0.0",
  "description": "Simple application to showcase how to achieve universal rendering and routing with React and Express.",
  "main": "src/server.js",
  "repository": "git@github.com:lmammino/judo-heroes.git",
  "scripts": {
    "start": "NODE_ENV=production node_modules/.bin/babel-node --presets 'react,es2015' src/server.js",
    "start-dev": "npm run start-dev-hmr",
    "start-dev-single-page": "node_modules/.bin/http-server src/static",
    "start-dev-hmr": "node_modules/.bin/webpack-dev-server --progress --inline --hot",
    "build": "NODE_ENV=production node_modules/.bin/webpack -p"
  },
  "author": "Luciano Mammino",
  "license": "MIT",
  "dependencies": {
    "babel-cli": "^6.11.4",
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.5",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "babel-preset-react-hmre": "^1.1.1",
    "ejs": "^2.5.1",
    "express": "^4.14.0",
    "react": "^15.3.1",
    "react-dom": "^15.3.1",
    "react-redux": "^4.4.5",
    "react-router": "^2.6.1",
    "redux": "^3.6.0"
  },
  "devDependencies": {
    "babel-plugin-transform-class-properties": "^6.18.0",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-stage-2": "^6.18.0",
    "http-server": "^0.9.0",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.14.1"
  }
}

webpack.config.js

"use strict";

const debug = process.env.NODE_ENV !== "production";

const webpack = require('webpack');
const path = require('path');

module.exports = {
  devtool: debug ? 'inline-sourcemap' : null,
  entry: path.join(__dirname, 'src', 'app-client.js'),
  devServer: {
    inline: true,
    port: 3333,
    contentBase: "src/static/",
    historyApiFallback: {
      index: '/index-static.html'
    }
  },
  output: {
    path: path.join(__dirname, 'src', 'static', 'js'),
    publicPath: "/js/",
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: path.join(__dirname, 'src'),
      loader: ['babel-loader'],
      query: {
        cacheDirectory: 'babel_cache',
        presets: debug ? ['react', 'es2015', 'react-hmre'] : ['react', 'es2015']
      }
    }]
  },
  plugins: debug ? [] : [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: { warnings: false },
      mangle: true,
      sourcemap: false,
      beautify: false,
      dead_code: true
    }),
  ]
};

1 个答案:

答案 0 :(得分:1)

好的,我明白了。我需要将它包含在我的.babelrc文件中:

{ "presets": ["es2015"] }

而且,我在webpack.config.js中需要这个(特别是stage-2部分):

presets: debug ? ['react', 'es2015', 'stage-2', 'react-hmre'] : ['react', 'es2015', 'stage-2']

另请注意,我发现声明预设的顺序非常重要。

希望这有助于任何面临类似问题的人。