朋友!我無法使用webpack構建。 在的WebPack我用巴貝爾6^+這個預設:Webpack錯誤:意外的代幣
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;
我怎樣才能解決這個問題?
你的Webpack配置是什麼樣的? – jlowgren
添加了Webpack.conf –