Reputation: 103
I am trying to implement [email protected] and babel-loader@8 to process react scripts. I have troubles with bundling jsx syntax (webpack babel-loader exiting with error while encountering any html tags.
I use the command: npm run build
(webpack --mode production)
index.js
// Import the wrapper component, and the the creator function
import React from 'react';
import ReactDOM from 'react-dom';
class App extends React.Component {
render () {
return <p> Hello React project</p>;
}
}
render(<App/>, document.getElementById('app'));
Usual error message:
[0] ./src/index.js 339 bytes {0} [built] [failed] [1 error]
ERROR in ./src/index.js 9:11
Module parse failed: Unexpected token (9:11)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| class App extends React.Component {
| render () {
> return <p> Hello React project</p>;
| }
| }
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `webpack --mode production`
npm ERR! Exit status 2
Bundling also fails constantly when trying to do: render(<App/>, document.getElementById('app'));
after importing App component from another file or defining App function which renders html tags.
webpack.config.js
var webpack = require('webpack');
var path = require('path');
module.exports = {
output: {
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
}
};
package.json
"name": "webpack_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.8.6",
"@babel/preset-env": "^7.8.6",
"@babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11"
},
"dependencies": {
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-router-dom": "^5.1.2"
}
}
I tried bundling the index.js file with @babel/cli
, which seems to work (command npx babel --presets @babel/preset-env,@babel/preset-react
). I have been trying to solve this for too long now, and I would really appreciate some pointers :) Thanks for help,
UPDATE
webpack.config.js
had a trailing space, so it was not recognized by webpack.
Upvotes: 2
Views: 1396
Reputation: 20132
You can check my config
My webpack
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader", "eslint-loader"]
},
.babelrc file
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime"
]
}
package.json
"@babel/core": "^7.8.4",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/preset-react": "^7.8.3",
Update:
Make sure to install "eslint": "^6.8.0"
to your package.json in devDependencies section
Upvotes: 1