Adding React to Webpack 4
In this recipe, we are going to implement React with Webpack 4, but we will use a plugin called html-webpack-plugin to generate our index.html file to render our React application. In the next recipe, we will integrate Node.js to have more flexibility in our server-side before rendering the HTML code.
Getting Ready
For this recipe, you will need to install the following packages:
npm install react react-dom babel-preset-reactHow to do it...
Here are the steps to add React to Webpack 4:
- Using the same code of the last recipe, create a
.babelrcfile and add some presets:
{
"presets": [
"env",
"react"
]
}File: .babelrc
- In our
webpack.config.jsfile, where we have ourbabel-loader, we need to add the.jsxextension beside the.jsextension to be able to applybabel-loaderto our React components:
const webpackConfig = { module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: 'babel-loader' } ] } }; module...