1

我正在尝试使用此错误来构建我的应用程序wepback并陷入困境。unexpected token我的项目的所有依赖项都很好并且是最新的

这是我的 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.8, react-dom v0.14.8,babel-loader ^6.2.1

4

1 回答 1

7

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

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

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

于 2016-04-29T17:26:15.780 回答