You could start your project by copying boilerplate code from a Github repository or simply running the create-react-app
command. Though create-react-app is a great tool, but I would like to suggest you to create your own boilerplate code. This will help you better understand the React ecosystem with Webpack and Babel and how they work together.
Let’s jump right in.
What Is Webpack?
Webpack official doc says:
What Is Babel?
Babel official doc says:
- Transform syntax
- Polyfill features that are missing in your target environment (through @babel/polyfill)
- Source code transformations (codemods)
- And more!
Prerequisite
These are the npm modules we are going to import in our project.
React
- react
- react-dom
Babel
- babel/core
- babel/preset-env
- babel/preset-react
- babel-loader
Webpack
- css-loader
- webpack
- webpack-cli
- style-loader
- webpack-dev-server
Let’s get started.
1) Set up a project folder and move into it. This will be our final project structure
React app structure
|
|
2) Creating a package.json file into the project root folder
|
|
Answer these questions and press yes to proceed.
package.json
3) Create src folder and these files in your project root folder
|
|
|
|
|
|
4) Install React and React-Dom modules
|
|
5) Install Babel modules
In this step, we are going to install these Babel’s modules.
- babel-core
- babel-loader
- babel/preset-env
- babel/preset-react
|
|
6) Install Webpack’s modules
In this step, we are going to install following Webpack’s modules.
- webpack
- webpack-cli
- webpack-dev-server
|
|
Install webpack-dev-server
module as well.
|
|
7) Add start and build settings in scripts node of package.json.
package.json
8) Install style-loader and css-loader webpack helper modules to use CSS in our application
|
|
9) Install html-webpack-plugin and html-loader
These are HTML webpack html plugins for displaying our HTML pages.
|
|
10) Create webpack.config.js file at root project folder
Once all modules are installed, we have to create a webpack.config.js
file to keep settings here. Let’s see how to create this file.
|
|
Once this config file is created, copy the below code in this config file.
|
|
11) Create .babelrc file in root project folder
|
|
Add these lines of code in .babelrc
file as well.
|
|
12) Open index.js file and add these lines of codes.
|
|
13) Open index.html file and add these lines of codes
|
|
14) Run your project
Everything is setup now. It’s time to run your app. Run this command in your terminal.
|
|
If everything is setup properly, you will see browser window with a message.
15) Build the app
By running below command, it will build React app.
|
|
It will create a dist folder in the project root folder having index.html
and main.js
file.
Conclusion
In this blog, we learned about how to create your React app with the help of Webpack and Babel. When you set up your own react project in this way it is pretty easy to customize and optimize it and upgrade dependency.
Further Reading
Create Your First React App In Easy Steps