Youâve worked on a few (or maybe a lot) of React projects but may have only started your own project by using create-react-app.
Create-react-app is great for beginners who just want to *get their hands into some React code *without going through the trouble of configuring the project with Webpack and Babel.
But youâre stronger now. Youâre *owning *ReactâŠ.and now itâs time to soldier on by creating your project from the ground up. The âReactâ equivalent of building your car engine from scratch.
Fear not. Youâve got this.
Create project directory
First, create a new directory and cd into it.
mkdir my-app-from-scratch && cd my-app-from-scratch
Next, letâs initialize the app with npm to manage all our dependencies.
npm init -y
Great! Now you have your package.json file set up.
Install Dependencies
Next up, letâs install all the dependencies weâll need for this application. Run this command and continue below for a breakdown of everything we just installed.
npm i webpack babel-loader @babel/preset-react @babel/core babel-preset-react html-webpack-plugin webpack-dev-server css-loader style-loader @babel/plugin-proposal-class-properties webpack-cli -D && npm i react react-dom -S
What we just installed:
-
Webpack: bundles all our files into one file
-
babel-loader: works with Webpack to transpile ES6+ into ES5 which is supported by older browsers
-
@babel/preset-react: extends Babel support to JSX
-
html-webpack-plugin: âsimplifies the creation of HTML files to serve your Webpack bundlesâ -https://webpack.js.org/plugins/html-webpack-plugin/
-
webpack-dev-server:- allows you to use Webpack with a development server that provides live reloading.
-
webpack-cli: âwebpack CLI provides a flexible set of commands for developers to increase speed when setting up a custom Webpack project.â -https://www.npmjs.com/package/webpack-cli
-
*css-loader: *allows Webpack to convert the CSS file into a JavaScript string.
-
*style-loader: *inserts the JavaScript string into HTML dom.
-
_@babel/plugin-proposal-class-properties: _âThis plugin transforms static class properties as well as properties declared with the property initializer syntaxâ -https://www.npmjs.com/package/@babel/plugin-proposal-class-properties
-
*react: *JavaScript library
-
_react-dom: _âServes as the entry point to the DOM and server renderers for Reactâ -https://www.npmjs.com/package/react-dom
Add src directory and index.js file
mkdir src && cd src && touch index.js
Add file src/index.html
While still inside the src directory, type this command into the terminal:
touch index.html
Your file tree should currently look like this:
my-app-from-scratch/
⣠node_modules/
⣠src/
⣠index.html
â index.js
⣠package-lock.json
â package.json
Great!
Now letâs add some starter code
Inside of src/index.html add this code:
<!doctype html>
<**html** lang=âenâ>
<**head**>
<**meta** charset=âutf-8">
<**title**>My React App from Scratch</**title**>
</**head**>
<**body**>
<**div** id=âappâ></**div**>
</**body**>
</**html**>
Inside of src/index.js add this code:
import ReactDOM from âreact-domâ;
import React from âreactâ;
const App = () => {
return <**h1**>This is my React app!</**h1**>;
}
ReactDOM.render(<**App** />, document.getElementById(âappâ));
Configure Webpack & Babel
Awesome. Now itâs time to rig up our Webpack and Babel configuration files. These files allow us to customize Webpack & Babel to handle things like JSX and CSS inside of our project.
In the root directory, type this into your terminal:
touch .babelrc && touch webpack.config.js
Your file tree should look like this:
my-app-from-scratch/
⣠node_modules/
⣠src/
⣠index.html
â index.js
⣠.babelrc
⣠package-lock.json
⣠package.json
â webpack.config.js
Inside of webpack.config.js add this code:
const HtmlWebPackPlugin = require(âhtml-webpack-pluginâ);
const htmlPlugin = new HtmlWebPackPlugin({
template: â./src/index.htmlâ,
filename: â./index.htmlâ
});
module.exports = {
mode: âdevelopmentâ,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: âbabel-loaderâ
}
},
{
test: /\.css$/,
use: [âstyle-loaderâ, âcss-loaderâ]
}
]},
plugins: [htmlPlugin]
};
Inside of .babelrc add this code:
{
âpresetsâ: [â@babel/preset-reactâ],
âpluginsâ: [â@babel/plugin-proposal-class-propertiesâ]
}
Add âstartâ script to package.json
Inside of package.json, add this code in scripts.
"start": "webpack serve --config webpack.config.js"
Your package.json file should look like this:
{
ânameâ: âmy-app-from-scratch â,
âversionâ: â1.0.0â,
âdescriptionâ: ââ,
âmainâ: âwebpack.config.jsâ,
âscriptsâ: {
âtestâ: âecho \âError: no test specified\â && exit 1",
"start": "webpack serve --config webpack.config.js"
},
âkeywordsâ: [],
âauthorâ: ââ,
âlicenseâ: âISCâ,
âdevDependenciesâ: {
â@babel/preset-reactâ: ââ·.12.13â,
âbabel-coreâ: ââ¶.26.3â,
âbabel-loaderâ: ââž.2.2â,
âbabel-preset-reactâ: ââ¶.24.1â,
âcss-loaderâ: ââ”.0.2â,
âhtml-webpack-pluginâ: ââ”.1.0â,
âstyle-loaderâ: âÂČ.0.0â,
âwebpackâ: ââ”.22.0â,
âwebpack-cliâ: ââŽ.5.0â,
âwebpack-dev-serverâ: âÂł.11.2â
},
âdependenciesâ: {
âreactâ: âÂčâ·.0.1â,
âreact-domâ: âÂčâ·.0.1â
}
}
Test to see your app running in the browser
Now itâs time for our moment-of-truth to see all our hard work in action!
In the root directory terminal, run
npm run start
You should see your app running at http://localhost:8080/ in the browser.
Great work! You made a React application completely from scratch!
Resources: webpack
Further Reading
5 Tools & Practices To Help You Develop Faster in React