我正在尝试配置webpack并将所有内容都启动并运行,但现在我遇到了同样的错误。我已经梳理了一些似乎通过纠正拼写/语法错误而解决的其他帖子,但我似乎无法在我的代码中查明错误。是否有更严重的事情发生?
ERROR in ./src/components/App/App.js
Module parse failed: /Users/Desktop/fred/src/components/App/App.js Unexpected token (7:11)
You may need an appropriate loader to handle this file type.
|
| render() {
| return (
| <h1>Hello</h1>;
| );
| };
|
@ ./src/index.js 3:0-39
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack-dev-server/client?http://localhost:8080 webpack/hot/only-dev-server ./src/index
这是我的webpack.config:
var path = require("path");
var webpack = require("webpack");
var autoprefixer = require('autoprefixer');
var precss = require('precss');
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/index'
],
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '/build/',
filename: "bundle.js"
},
resolve: {
extensions: ['*', '.js', '.jsx', '.png', '.json']
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.LoaderOptionsPlugin({
options: {
context: __dirname,
postcss: [
autoprefixer
]
}
})
],
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: ['react-hot-loader', 'babel-loader']
},
{
test: /\.jsx?$/,
loaders: ['react-hot-loader', 'babel-loader']
},
{
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader']
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: 'url-loader?limit=10000',
}
],
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
}
};
我的App.js:
import React, { Component } from 'react';
import s from './App.scss';
class App extends Component {
render() {
return (
<h1>Hello</h1>;
);
};
}
export default App;
我的index.js:
import React from 'react';
import { render } from 'react-dom';
import App from './components/App/App';
let element = React.createElement(App, {});
render(element, document.querySelector('.container'));
我的.babelrc:
{
"presets" : ["es2015", "react", "stage-0"]
}
和我的package.json:
{
"name": "fred test",
"version": "1.0.0",
"description": "a fred test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server"
},
"author": "fred",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^6.7.7",
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.0",
"file-loader": "^0.11.1",
"postcss": "^5.2.17",
"postcss-loader": "^1.3.3",
"precss": "^1.4.0",
"react-hot-loader": "^1.3.1",
"style-loader": "^0.16.1",
"webpack": "^2.3.3",
"webpack-dev-server": "^2.4.2"
},
"dependencies": {
"normalize.css": "^6.0.0",
"react": "^15.5.4",
"react-dom": "^15.5.4"
}
}
答案 0 :(得分:2)
问题在于您定义了module.rules
和module.loaders
。只要存在module.loaders
,Webpack就会完全忽略module.rules
(仅出于兼容性原因而存在)。在您的情况下,所有JavaScript规则都在module.loaders
内,因此没有使用它们。
要修复它,请将所有内容放在module.rules
内。您还有两个.js
文件规则,因为/\.js?$/
匹配.j
和.js
,因为?
表示一次或零次出现,类似{{1}匹配/\.jsx?$/
和.js
,这是你真正想要的,而.jsx
的规则是没有意义的。
.j
答案 1 :(得分:0)
您使用的是J6的ES6版本。您需要在webpack中预设配置才能理解并转换它。
您可以尝试在webpack模块配置中添加以下内容吗?
loaders : [
{
test:/\.jsx?$/,
exclude:/node_modules/,
loader: 'babel-loader',
query:{
presets: ['es2015','react','stage-0']
}
},
// ...
]