This repo will help to create a react app with webpack 4 and latest babel config and, moreover this is a very basic starter kit to setup the React app
Simple boilerplate for React + webpack 4
Clone repository:
git clone https://github.com/ssthil/react-webpack4-starter.git
and then
yarn install
or npm install
to install dependencies.
in order to run the application, run the below command
yarn start
or nom start
The project will be running at http://localhost:8080/
Let's start from the following steps ...
Make a new project
We need to install webpack as a dev dependency and webpack-cli
Install react and react-dom as a dependency
Then install babel as a dev dependency
mkdir react-webpack-starter
cd react-webpack-starter
yarn init -y
yarn add webpack webpack-cli webpack-dev-server @babel/core @babel/preset-env @babel/preset-react babel-loader html-webpack-plugin --dev
yarn add react react-dom && touch webpack.config.js .babelrc README.md .gitignore
mkdir src && cd $_ && touch index.js index.html
mkdir react-webpack-starter && cd $_ && yarn init -y && yarn add webpack webpack-cli webpack-dev-server @babel/core @babel/preset-env @babel/preset-react babel-loader html-webpack-plugin --dev && yarn add react react-dom && touch webpack.config.js .babelrc README.md .gitignore && mkdir src && cd $_ && touch index.js index.html
Add this code in webpack.config.js file to state the rules for our babel-loader
const HtmlWebPackPlugin = require("html-webpack-plugin");
const htmlPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
});
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [htmlPlugin]
};
When you state that you’re using babel-loader in your webpack config, it will look for .babelrc
file if there is one.
{
"presets": ["@babel/env", "@babel/preset-react"]
}
index.html with id="app"
In index.js
import React from 'react';
import ReactDOM from 'react-dom';
const App = () => {
return(
<div>
<h1>Hello React</h1>
</div>
)
}
const SelectorRef = document.getElementById("app");
ReactDOM.render(<App />, SelectorRef);
add below code in the package.json
"scripts": {
"start": "webpack-dev-server --mode development --open",
"build": "webpack --mode production"
}