Building a React ecosystem from scratch
Aline Ebone
Posted on February 7, 2023
What do we need?
- An entry point
- Support ES6 and JSX
- Building and serving with Webpack
- React-hot-loader
An entry point
Create a folder to hold the project
Go to this folder and run
npm init -y
to initialize a new npm packageCreate a
public
folder to hold our static codeCreate a
src
folder to hold our React codeCreate an
index.html
file inside thepublic
folder. This is what is sent to the client when a request to our website is made by the user.Create a
div
withid="root"
in theindex.html
. Thisdiv
will receive the React content.Add a
script
tag to load the React code:<script src="../dist/bundle.js"></script>
(it doesn't exist yet)
Support ES6 and JSX
Install Babel compiler
npm i --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
Create a
.babelrc
file in the root of the project to tell Babel which presets and plugins should be used to transpile the codeInsert a json object inside this file with preset-env (transform ES6 in VanillaJS) and preset-react (understands JSX):
{"presets": ["@babel/preset-env", "@babel/preset-react"]}
Building and serving with Webpack
Install
react
andreact-dom
to be able to run a React project:npm i react react-dom
Create the
index.js
andApp.js
files inside thesrc
folder to start coding the application
index.js:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App/>, document.getElementById("root"));
App.js
import React from "react";
const App = () => <div>Hello</div>;
export default App;
Install
webpack
to build and serve the projectnpm i --save-dev webpack webpack-cli webpack-dev-server style-loader css-loader babel-loader
Create a
webpack.config.js
file in the root directory and add:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
options: { presets: ["@babel/env"] }
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
resolve: { extensions: ['*', '.js', '.jsx'] },
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '/dist/',
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, 'public/'),
port: 3000,
publicPath: 'http://localhost:3000/dist/',
hotOnly: true
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
Run the server
npx webpack-dev-server --mode development
. It will run in the port 3000.To make it easier, add the following script in the
package.json
file:"dev": "npx webpack-dev-server --mode development"
React-hot-loader
Enable auto-reload installing react-hot-loader
npm i --save-dev react-hot-loader
Add
import {hot} from "react-hot-loader"
andexport default hot(module)(App)
in theApp.js
file
Project Link: Github
Posted on February 7, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.