我試圖用webpack和babel構建reactjs應用程序。爲什麼webpack不喜歡這個表達
我開始這個應用程序從反應啓動器與react-scripts build
以前工作。但是,這是一個黑盒子,並沒有真正提供我需要的所有功能,特別是當涉及到一個模塊不喜歡UglifyJS時。
我webpack.config.js
看起來是這樣的,這是非常簡單的:
var path = require('path');
var webpack = require('webpack');
var BUILD_DIR = path.resolve(__dirname, 'build_webpack');
var APP_DIR = path.resolve(__dirname, 'src');
module.exports = {
entry: APP_DIR + '/index.js',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /.jsx?$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
}
]
}
}
,我有這個配置在我package.js
:
"scripts": {
"start": "react-scripts start",
"reactbuild": "react-scripts build",
"webpackbuild": "webpack --watch",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
而且我.babelrc
文件只有這個:
{
"presets" : ["es2015", "es2016", "react"]
}
但是,在中工作的代碼這裏失敗,錯誤輸出表示:在./src/Pages/SearchTool/SearchResult.js
ERROR 模塊構建失敗:語法錯誤:意外的令牌(100:13)
(錯誤被參考到數據輸入參數)
renderChip = (data) => {
return (
<Chip
key={data.key}
我真的不明白這個表達式有什麼特別之處。我不認爲這是很常用的,但應該是合法的。另外,我切換到webpack的一個原因是以前的反應版本不像es6,但是我的一個重要的模塊是用es6編寫的。
我不知道我的配置或其他地方丟失了什麼。感謝所有的幫助!
編輯:
我package.js:
{
"name": "myCoolApps",
"version": "0.1.0",
"private": true,
"devDependencies": {
"css-loader": "^0.26.1",
"react-scripts": "0.7.0",
"webpack": "^1.13.3"
},
"dependencies": {
"ace": "git+https://github.com/ajaxorg/ace.git#master",
"antd": "^2.7.2",
"axios": "^0.15.3",
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-preset-env": "^1.4.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-react": "^6.24.1",
"card": "^2.2.1",
"card-react": "^1.2.6",
"chat-template": "0.0.22",
"codemirror": "^5.25.0",
"credit-card-type": "^5.0.1",
"css-loader": "^0.26.1",
"d3": "^4.7.4",
"firechat": "^3.0.1",
"firepad": "^1.4.0",
"flux": "^3.1.0",
"gulp": "^3.9.1",
"gulp-sass": "^3.1.0",
"history": "^1.17.0",
"little-loader": "^0.2.0",
"lodash": "^4.17.4",
"material-ui": "^0.16.6",
"moment": "^2.17.1",
"node-sass": "^4.5.0",
"quill": "^1.2.3",
"rc-calendar": "^7.6.5",
"react": "^15.5.4",
"react-autosuggest": "^7.0.1",
"react-cookie": "^1.0.4",
"react-credit-card": "^0.20.0",
"react-dom": "^15.5.4",
"react-dropzone": "^3.8.0",
"react-event-timeline": "^1.2.2",
"react-infinite": "^0.10.0",
"react-infinite-scroller": "^1.0.7",
"react-list": "^0.8.3",
"react-notification-system": "^0.2.12",
"react-router": "^3.0.0",
"react-tap-event-plugin": "^2.0.1",
"seedrandom": "^2.4.2",
"simplewebrtc": "^2.2.2",
"style-loader": "^0.13.1",
"superagent": "^3.3.1",
"webpack": "^1.15.0",
"y-array": "^10.0.6",
"y-indexeddb": "^8.1.9",
"y-leveldb": "0.0.1",
"y-map": "^10.0.5",
"y-memory": "^8.0.8",
"y-richtext": "^9.0.8",
"y-text": "^9.3.2",
"y-webrtc": "^8.0.7",
"y-websockets-client": "^8.0.15",
"yjs": "^12.1.7"
},
"scripts": {
"start": "react-scripts start",
"reactbuild": "react-scripts build",
"webpackbuild": "webpack",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
UPDATE:
我試過ENV預設的,現在我的webpack.config.js
看起來是這樣的:
{
"presets":
[
"react",
[
"env",
{
"targets": {
"uglify": false,
"node": "current",
"debug": true,
},
}
],
],
}
但是,它仍然抱怨這個表達式:
ERROR in ./src/Pages/SearchTool/SearchResult.js
Module build failed: SyntaxError: Unexpected token (100:13)
98 | }
99 |
> 100 | renderChip = (data) => {
| ^
101 | return (
102 | <Chip
103 | key={data.key}
@ ./src/Pages/Connections/Connections.js 43:20-57
功能原本看起來像這樣的程序(注:這是一個類中):
renderChip = (data) => {
return (
<Chip
key={data.key}
onRequestDelete={this.handleRequestDelete}
style={{
borderRadius: '6px',
margin: '0 4px',
height: 35
}}
>
{data.label}
</Chip>
);
}
而且我也嘗試過包括插件手動
這個功能在我的課看起來像的地方:
class Name extends Component {
constructor(props) {...}
renderChip = (data) => {
return (
<Chip
key={data.key}
onRequestDelete={this.handleRequestDelete}
style={{
borderRadius: '6px',
margin: '0 4px',
height: 35
}}
>
{data.label}
</Chip>
);
}
render() {...}
}
我手動包含transform-es2015-function-name
和transform-es2015-arrow-functions
,但他們沒有工作。是否有一些其他插件需要手動添加?或者是由其他原因造成的?
解決
解決辦法是改變功能的表達研究到:
renderChip() {
...
}
什麼是您的webpack版本? – loelsonk
它是版本4.4.4 – Jason
我問'webpack'版本,最新是2.4.1。那麼你的是什麼? – loelsonk