Recently i have implemented Webpack 4
setup for my react
app.
My webpack.config.js
looks like this
const HtmlWebPackPlugin = require('html-webpack-plugin');
const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: './src/index.js',
filename: './index.html',
});
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: '[name]_[local]_[hash:base64]',
sourceMap: true,
minimize: true,
},
},
],
},
],
},
plugins: [htmlWebpackPlugin],
};
Here is my package.json
scripts
"scripts": {
"dev": "webpack-dev-server --mode development --open",
"prod": "webpack --mode production"
}
Here the problem is when i use ...
(spread operator) it's throwing an error
i believe this is something related to babel
which is not properly transpiling. Any suggestions would be appreciated. Thanks.
It throws an error
something like the one below.
ERROR in ./src/index.js
Module build failed: SyntaxError: D:/cp/src/index.js: Unexpected token (31:6)
29 | return {
30 | headers: {
> 31 | ...headers,
| ^
32 | authorization: token ? `Bearer ${token}` : null,
33 | },
34 | };
Just install babel-plugin-transform-object-rest-spread
module.
https://www.npmjs.com/package/babel-plugin-transform-object-rest-spread
Then add it to .babelrc:
"plugins": [
"babel-plugin-transform-object-rest-spread",
],
"babel-plugin-transform-object-rest-spread"
does not even have a github page. if you use this, you will see too much output on the console and there is no option to silent it or use "verbose". I suggest you to use
@babel/plugin-transform-spread. then add this to the .babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-transform-spread"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With