我有一个带有反应导入的.js文件,该函数带有返回span标签的函数,如下所示:
makeSpan = (number, prefix) => {
return <span>{prefix} {number}</span>
};
我将这个.js文件添加到我的react类中,并调用它来呈现跨度。问题是webpack给了我语法错误意外的令牌,这显然与JSX语法有关(返回跨度)
webpack.config.js
const path = require('path')
HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'index_bundle.js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test:/\.css$/,
use:['style-loader','css-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/template.html'
})
]
}
babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
答案 0 :(得分:0)
我解决了这个问题,我的解决方案是将babel插件也放在webpack.config.js中。我不知道为什么会这样。
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-proposal-class-properties"]
}
}
},
{
test:/\.css$/,
use:['style-loader','css-loader']
}
]
},