【发布时间】:2017-05-31 08:03:32
【问题描述】:
我正在尝试在路由处拆分反应代码,但收到此错误:Uncaught SyntaxError: Unexpected token <
这是我的 webpack 代码:
var path = require('path')
var webpack = require('webpack')
var HappyPack = require('happypack')
var BundleTracker = require('webpack-bundle-tracker')
var path = require('path')
var ExtractTextPlugin = require("extract-text-webpack-plugin")
var polyfill = require("babel-polyfill")
function _path(p) {
return path.join(__dirname, p);
}
module.exports = {
context: __dirname,
entry: {
index: './assets/js/index',
},
output: {
path: path.resolve('./assets/bundles/'),
filename: '[name]-[hash].js'
},
devtool: 'inline-eval-cheap-source-map',
plugins: [
new BundleTracker({filename: './webpack-stats.json'}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new HappyPack({
threads: 4,
loaders: [ 'babel-loader' ],
}),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /pt-br/)
],
module: {
rules: [
{
test: /\.css$/,
include: path.resolve(__dirname, './assets/css/'),
use: ["style-loader", "css-loader", "resolve-url-loader"]
// use: ExtractTextPlugin.extract({ fallbackLoader: "style-loader", loader: ["css-loader", "resolve-url-loader"]})
},
{
test: /\.scss$/,
include: path.resolve(__dirname, './assets/css/'),
use: ["style-loader", "css-loader", "resolve-url-loader", "sass-loader"]
// use: ExtractTextPlugin.extract({ fallbackLoader: "style-loader", loader: ["css-loader", "resolve-url-loader", "sass-loader"]})
},
{
test: /\.scss$/,
include: path.resolve(__dirname, './assets/vendors/'),
use: ["style-loader", "css-loader", "resolve-url-loader", "sass-loader"]
// use: ExtractTextPlugin.extract({ fallbackLoader: "style-loader", loader: ["css-loader", "resolve-url-loader", "sass-loader"]})
},
{
test: /\.jsx?$/,
include: path.resolve(__dirname, './assets/js/'),
exclude: /node_modules/,
use: [{
loader: 'happypack/loader',
}]
},
{
test: /\.png$/,
use: {
loader: 'file-loader' ,
options: {
name: '/static/img/[name].[ext]'
}
}
}
]
},
resolve: {
extensions: ['.js', '.jsx'],
modules: [ path.resolve(__dirname, 'node_modules')]
}
}
我的 babelrc 代码:
{
"presets": [
["es2015", {modules: false}],
"react"
]
}
还有我的路由器:
<Router history={browserHistory}>
<Route path="login" onEnter={redirectHome} getComponent={(location, cb) => {
System.import('./components/common/LoginPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="app" onEnter={requireAuth} getComponent={(location, cb) => {
System.import('./components/App')
.then(loadRoute(cb))
.catch(errorLoading);
}
}>
<IndexRoute getComponent={(location, cb) => {
System.import('./components/middle/dashboard/DashboardPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
}/>
<Route path="clients">
<IndexRoute getComponent={(location, cb) => {
System.import('./components/middle/clients/ClientPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="form/:method(/:clientId)" getComponent={(location, cb) => {
System.import('./components/middle/clients/ClientForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="detail/:id" getComponent={(location, cb) => {
System.import('./components/middle/clients/ClientDetail')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="attends/:clientId" getComponent={(location, cb) => {
System.import('./components/middle/attends/ClientAttend')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="contacts">
<Route path="form/:method/:client(/:contactId)" getComponent={(location, cb) => {
System.import('./components/middle/contacts/ContactForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="list/:id" getComponent={(location, cb) => {
System.import('./components/middle/contacts/ContactPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="detail/:id/:contactId" getComponent={(location, cb) => {
System.import('./components/middle/contacts/ContactDetail')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="transmissors">
<Route path="form/:method/:client(/:transmissorId)" getComponent={(location, cb) => {
System.import('./components/middle/transmissors/TransmissorForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="list/:id" getComponent={(location, cb) => {
System.import('./components/middle/transmissors/TransmissorPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="detail/:client(/:transmissorId)" getComponent={(location, cb) => {
System.import('./components/middle/transmissors/TransmissorDetail')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="attends">
<IndexRoute getComponent={(location, cb) => {
System.import('./components/middle/attends/AttendancePage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="call/:id(/:attend)" getComponent={(location, cb) => {
System.import('./components/middle/attends/AttendanceDetail')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="reports">
<IndexRoute getComponent={(location, cb) => {
System.import('./components/middle/reports/ReportPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="list/alarms/:filter" getComponent={(location, cb) => {
System.import('./components/middle/reports/ReportAlarm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="list/attendants/:filter" getComponent={(location, cb) => {
System.import('./components/middle/reports/ReportAttendant')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="list/clients/:filter" getComponent={(location, cb) => {
System.import('./components/middle/reports/ReportClient')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="user/form/:method(/:userId)" getComponent={(location, cb) => {
System.import('./components/middle/user/UserForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="manage">
<Route path="phones" getComponent={(location, cb) => {
System.import('./components/middle/user/UserForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="attend" getComponent={(location, cb) => {
System.import('./components/middle/manage/ManageAttend')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
<Route path="alarms" getComponent={(location, cb) => {
System.import('./components/middle/manage/AlarmManagement')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="events">
<Route path="detail/:id" getComponent={(location, cb) => {
System.import('./components/middle/events/EventsPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="alarms">
<Route path="form/:method(/:alarmId)" getComponent={(location, cb) => {
System.import('./components/middle/manage/AlarmForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
<Route path="phones">
<Route path="form/:method(/:phoneId)" getComponent={(location, cb) => {
System.import('./components/middle/manage/PhoneForm')
.then(loadRoute(cb))
.catch(errorLoading);
}
} />
</Route>
</Route>
<Route path="*" onEnter={redirect}/>
</Router>
我在后端使用 django,以及不支持 webpack 代码拆分的 webpack-bundle-tracker。
所以我创建了一个问题并为此做了一个解决方案(或变通方法): https://github.com/owais/webpack-bundle-tracker/issues/19
我的 index.html 是:
{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>title</title>
<link rel="icon" type="image/png" sizes="32x32" href="static/img/favicon-32x32.png">
{% render_bundle 'index' 'css' %}
</head>
<body>
<div id ="app" class="app"></div>
<audio id="audio_sip" autoplay="autoplay"></audio>
{% render_bundle 'index' 'js' %}
{% render_bundle 'split' 'js' %}
</body>
</html>
所以我不知道我是否在 react 路由器上正确使用了导入,或者问题可能与 django 相关。
谁能帮帮我?
提前致谢。
【问题讨论】:
-
如果你说的是应用路由,是}>
-
我没有看到它在某处有一个结束标签。
-
- 用于应用程序 - 应用程序有很多孩子
标签: django reactjs react-router webpack-2