【发布时间】:2019-02-06 06:05:45
【问题描述】:
当我尝试将我的模块与 webpack 捆绑时,它无法识别我的 index.js 文件中的 JSX 语法并给出以下错误:
ERROR in ./src/index.js 29:3
Module parse failed: Unexpected token (29:3)
You may need an appropriate loader to handle this file type.
| const searchTerm = _.debounce(term => {this.searchTerm(term)}, 300);
| return (
> <div>
| <SearchBar onInputChange= {searchTerm}/>
| <div className="row">
@ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js main[1]
这是我的 webpack 配置:
const Path = require('path');
module.exports = {
entry: ['./src/index.js'],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
include: Path.resolve(__dirname, '../src'),
use: {
loader: 'babel-loader',
options: {
presets: ["@babel/preset-react", "@babel/preset-env"]
}
}
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [ 'css-loader' ]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './',
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
}
};
我已经为 react 添加了 babel 预设,即 @babel/preset-react。我还添加了 babel-loader 但它仍然无法识别 JSX 语法。
【问题讨论】:
-
试试
"presets": ["react", "env"], -
试过了,没用@RomanMaksimov
-
请出示您的
package.json -
您可以尝试添加
babel-preset-react-app预设(nom i babel-preset-react-app或yarn add babel-preset-react-app)并使用"presets": ["react-app"]。此外,最好将babel选项专用于应用程序根目录中的.babelrc文件