Webpack错误:意外的令牌

时间:2016-01-20 09:52:32

标签: javascript reactjs ecmascript-6 webpack

朋友们!我在使用webpack构建时遇到了麻烦。 在webpack中,我使用babel 6 ^ +这个预设:

presets: ['es2015', 'stage-1', 'react']

在npm开始后我发现错误:

ERROR in ./src/common/components/layout/Header.js
Module build failed: SyntaxError: C:/Users/Anton/projects/isomorphic-redux-app/src/common/components/layout/Header.js: Unexpected token (13:15)
  11 |   }
  12 |
  13 |   handleToggle = () => this.setState({open: !this.state.open});
     |                ^
  14 |
  15 |   render() {
  16 |     return (

起初我认为我的代码有错误,我只是从Material-UI文档中复制/粘贴它,但它也被破坏了。 Header.js文件:

import React, { Component, PropTypes } from 'react';
import LeftNav from 'material-ui/lib/left-nav';
import AppBar from 'material-ui/lib/app-bar';
import RaisedButton from 'material-ui/lib/raised-button';

export default class Header extends Component {

  constructor(props) {
    super(props);
    this.state = {open: false};
  }

  handleToggle = () => this.setState({open: !this.state.open});

  render() {
    return (
      <div>
        <RaisedButton
          label="Controlled LeftNav That Opens From Right"
          onTouchTap={this.handleToggle} />
        <LeftNav width={200} openRight={true} open={this.state.open} >
          <AppBar title="AppBar"/>
        </LeftNav>
      </div>
    );
  }
}

和webpack.config:

var path = require('path');
var webpack = require('webpack');
var merge = require('merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');


var webpackConfig = {
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.NoErrorsPlugin()
  ]
};

if (process.env.NODE_ENV === 'production') {

  webpackConfig = merge(webpackConfig,{
    devtool: "source-map",
    entry : [
      './src/client/index.js'
    ],
    resolve: {
      extensions: ["", ".js", ".jsx"]
    },
    module: {
    loaders: [{
    test: /\.js$/,
    loader: 'babel',
    exclude: /node_modules/,
    include: __dirname,
    query: {
      presets: ['es2015', 'stage-1', 'react'],

    }
      },
      { 
        test: /\.jsx$/, 
        loader: 'babel', 
        exclude: /node_modules/,
        include: __dirname,
        query: {
          presets: ['es2015', 'stage-1', 'react'],

        }
      },
      { test: /\.(png|jpg|gif|jpeg)$/, loader: 'url-loader?limit=8192'},
      { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap') }
    ]},
    plugins : [
      new webpack.DefinePlugin({
       'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
      }),
      new ExtractTextPlugin("app.css"),
      new webpack.optimize.UglifyJsPlugin({minimize: true})
    ]  
  });

}else{

  webpackConfig = merge(webpackConfig,{
    devtool: 'inline-source-map',
    module: {
      loaders: [{
    test: /\.js$/,
    loader: 'babel',
    exclude: /node_modules/,
    include: __dirname,
      env: {
        development: {
          plugins: [
            'react-transform'
          ],
          extra: {
            'react-transform': {
              transforms: [{
                transform:  'react-transform-hmr',
                imports: ['react'],
                locals:  ['module']
              },
              {
                transform: 'react-transform-catch-errors',
                imports: ['react','redbox-react' ]
              }
            ]}
          }
        }
      },//
        query: {
//          optional: ['runtime'],
      presets: ['es2015', 'stage-1', 'react'],

    }
  },
  { 
    test: /\.jsx$/, 
    loader: 'babel', 
    exclude: /node_modules/,
    include: __dirname,
    env: {
        development: {
          plugins: [
            'react-transform'
          ],
          extra: {
            'react-transform': {
              transforms: [{
                transform:  'react-transform-hmr',
                imports: ['react'],
                locals:  ['module']
              },
              {
                transform: 'react-transform-catch-errors',
                imports: ['react','redbox-react' ]
              }
            ]}
          }
        }
      },
    query: {
      presets: ['es2015', 'stage-1', 'react'],

    }
  },
  { test: /\.(png|jpg|gif|jpeg)$/, loader: 'url-loader?limit=8192'},
  { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap') }
]},
entry : [
  'webpack-hot-middleware/client',
  './src/client/index.js',
],
resolve: {
  extensions: ["", ".js", ".jsx"]
},
plugins : [
  new webpack.HotModuleReplacementPlugin(),
  new ExtractTextPlugin("app.css")
    ]  
  });

}

module.exports = webpackConfig;

我如何解决它?

4 个答案:

答案 0 :(得分:2)

您不需要箭头功能(这是无效的语法),因为您正在定义class方法:

handleToggle = () => this.setState({open: !this.state.open});

请改为尝试:

handleToggle() { this.setState({open: !this.state.open}); } 

但是,由于类方法不会自动绑定,您需要在构造函数中或在使用它时将其绑定:

constructor(props) {
    super(props);
    this.state = {open: false};
    this.handleToggle = this.handleToggle.bind(this);
  }

如果您在render或其他类方法中,则需要在前面添加const或等效内容(使用=时的分配):

render() {
    const handleToggle = () => this.setState({open: !this.state.open});
}

答案 1 :(得分:0)

您需要使用Babel阶段1来获取类属性。

http://babeljs.io/docs/plugins/preset-stage-1/

步骤1:按如下方式添加依赖项:

npm install babel-preset-stage-1 --save

第2步:更改.babelrc文件如下:

{
  "presets": ["es2015", "react","stage-1"]
}

答案 2 :(得分:0)

如果我是正确的,您正在尝试使用属性初始化程序,这是一个ES7功能。要解决此问题,您必须使用stage-1预设。

更多信息here

答案 3 :(得分:0)

如果你想在类上使用箭头函数而避免绑定到构造函数(这一位):

this.handleToggle = this.handleToggle.bind(this);

然后你可以使用babel的变换类属性。为此,请在命令行中下载模块:

npm i --save babel-plugin-transform-class-properties

然后在你的.babelrc:

{
  "plugins": ["transform-class-properties"]
}

然后,您可以使用清除语法并在构造函数中删除绑定:

handleToggle = () => this.setState({open: !this.state.open});

这应该是在阶段0或阶段1预设,但我只是通过明确地引用插件(如上所述)设法使其工作。