This project demonstrates how to set up a React application from scratch using Webpack and Babel. It provides a basic boilerplate for developing and running React applications.
- Installation
- Project Structure
- Scripts
- Development Server
- Building for Production
- Key Configurations
-
Clone the repository or create a new directory:
mkdir my-react-app cd my-react-app -
Initialize a new Node.js project:
npm init -y
-
Install required dependencies:
npm install react react-dom
-
Install development tools:
npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env @babel/preset-react html-webpack-plugin
After completing the setup, your project directory should look like this:
my-react-app/
├── public/
│ └── index.html
├── src/
│ └── index.js
├── .babelrc
├── webpack.config.js
├── package.json
└── node_modules/
public/index.html: Template HTML file with a root div.src/index.js: Entry point for the React app..babelrc: Configuration for Babel to transpile modern JavaScript and React JSX.webpack.config.js: Configuration for Webpack to bundle files and serve the app.
Add the following scripts to your package.json file:
"scripts": {
"start": "webpack serve --mode development",
"build": "webpack --mode production"
}- Start Development Server:
npm start - Build for Production:
npm run build
- Run the development server:
npm start
- Open your browser and navigate to:
http://localhost:3000
To create a production build:
npm run buildThe output will be generated in the dist/ folder.
The webpack.config.js file contains the configuration for bundling and serving the app:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: "babel-loader",
},
],
},
resolve: {
extensions: [".js", ".jsx"],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
devServer: {
static: path.resolve(__dirname, "dist"),
port: 3000,
open: true,
},
};The .babelrc file configures Babel to transpile modern JavaScript and JSX:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}The src/index.js file contains a basic React component:
import React from "react";
import ReactDOM from "react-dom";
const App = () => <h1>Hello, React!</h1>;
ReactDOM.render(<App />, document.getElementById("root"));- Add CSS or SCSS support.
- Include testing libraries like Jest or React Testing Library.
- Implement routing using React Router.
- Integrate state management using Redux or Context API.
This project is licensed under the MIT License.