React,Babel,Webpack没有解析jsx,意外的令牌错误

时间:2016-04-29 17:13:24

标签: reactjs webpack babeljs react-dom

我正在尝试使用wepback构建我的应用,并且遇到此unexpected token错误。我的项目的所有依赖项都很好,并且最新我在其他地方使用相同的项目并且它正在构建正常但是当我尝试构建我的当前代码时,它给出了错误,下面是config.js和错误描述< / p>

这是我的app.jsx

import React                from 'react';
import ReactDOM             from 'react-dom';
import {Router}             from 'react-router';
import Routes               from '../routes/routes';
import injectTapEventPlugin from 'react-tap-event-plugin';
import { browserHistory }   from 'react-router';

require('../Config');

injectTapEventPlugin();

window.EventEmitter = {
    _events: {},
    dispatch: function (event, data, returnFirstResult) {
        if (!this._events[event]) return;
        for (var i = 0; i < this._events[event].length; i++)
        {
            if(this._events[event][i])
            {
                var r = this._events[event][i](data);

                if(returnFirstResult)   
                    return r;
            }
        }
    },
    subscribe: function (event, callback, onlyOne) {
        if (!this._events[event] || onlyOne) this._events[event] = []; // new event
        this._events[event].push(callback);
    }
};

// Render the main app react component into the app div.
// For more details see: https://facebook.github.io/react/docs/top-level-api.html#react.render
ReactDOM.render(<Router history={browserHistory}>{Routes}</Router>, document.getElementById('app'));

这是我的config.js

var webpack = require('webpack');
var path = require('path');
var buildPath = path.resolve(__dirname, '../project/public/js/');
var nodeModulesPath = path.resolve(__dirname, 'node_modules');
var TransferWebpackPlugin = require('transfer-webpack-plugin');

var config = {
    entry: {
        app: path.join(__dirname, '/app/entry_points/app.jsx'),
        vendor: ['react', 'radium'],
    },
    resolve: {
        extensions: ["", ".js", ".jsx"]
    },

    devtool: 'source-map',
    output: {
        path: buildPath,
        publicPath: '/js/',
        filename: 'mobile.[name].js' 
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin("vendor", "mobile.vendor.js"),
        new webpack.DefinePlugin({}),
        new webpack.NoErrorsPlugin(),
    ],
    module: {
        preLoaders: [
            {
                test: /\.(js|jsx)$/,
                loader: 'eslint-loader',
                include: [path.resolve(__dirname, "src/app")],
                exclude: [nodeModulesPath]
            },
        ],
        loaders: [
            {
                test: /\.(js|jsx)$/,
                loaders: [
                    'babel-loader'
                ],
                exclude: [nodeModulesPath]
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
        ]
    },
    eslint: {
        configFile: '.eslintrc'
    },
};

module.exports = config;

这是我尝试构建时遇到的错误:

ERROR in ./app/entry_points/app.jsx
Module build failed: SyntaxError: /home/zeus/Glide/project/project-mobile/app/entry_points/app.jsx: Unexpected token (46:16)
  44 | // Render the main app react component into the app div.
  45 | // For more details see: https://facebook.github.io/react/docs/top-level-api.html#react.render
> 46 | ReactDOM.render(<Router history={browserHistory}>{Routes}</Router>, document.getElementById('app'));
     |                 ^

我正在使用react v0.14.8react-dom v0.14.8babel-loader ^6.2.1

1 个答案:

答案 0 :(得分:7)

我相信您需要使用babel指定预设并安装npm模块babel-preset-react

loaders: [
        {
            test: /\.(js|jsx)$/,
            loaders: [
                'babel-loader'
            ],
            exclude: [nodeModulesPath],
            query: {
              presets: ['react']
            }
        },
        ...
    ]

如果您正在使用它,您还希望将es2015添加到该预设数组中。