React Webpack Developement Environment
React Webpack Developement Environment
Script files for React apps, use libraries from node instead!
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-
dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-
core/5.8.23/browser.min.js"></script> ①
Node.js
Node.js allows you to use JavaScript to create applications that run on the server and have access to
APIs and system resources that your browser could’t have. It’s basically a full-fledged application
development runtime for apps build entirely in JavaScript.
Babel
Babel is a JavaScript translater for marking JSX and JS compatibility with older browsers. We’ll use its
in-browser version to transform our JSX into JavaScript all time in dev environment. It can be
integrated as part of our build process to generate an actual browser-readable JS file from our JSX.
Webpack
It is known as a module builder. A lot of the frameworks and libraries in your app includes a lot of
dependencies where different parts of the functionality you rely on might only be a subset of larger
componets. You probably don’t want all of that unnecessary code, so webpack enable you to only
include the relevant code needed to have your app to work into a single JavaScript file. This also
extends to CSS (LESS/SASS) files and other types of assets your app uses.
Node.js | 1
Setting UP React Development Environment
index.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <title>Document</title>
8 </head>
9 <body>
10 <div id="app"></div>
11 <script src="dist/bundle.js" async defer></script>
12 </body>
13 </html>
index.jsx
webpack.config.js
Step 1: Let’s create .babelrc and the babel.config.js files in the root folder
babelrc
1 {
2 "presets": [
3 "@babel/preset-env",
4 "@babel/preset-react"
5 ]
6 }
babel.config.js
1 module.exports = {
2 "presets": ["@babel/preset-env", "@babel/preset-react"]
3
4 };
webpack.config.js
① We added the module and loaders objects that tell webpack to pass the index.jsx file defined in
our entry property to turn into JavaScript through Babel.
package.json
1 {
2 "dependencies": {
3 "@babel/core": "^7.19.1",
4 "@babel/preset-env": "^7.19.1",
5 "@babel/preset-react": "^7.18.6",
6 "babel-loader": "^8.2.5",
7 "react": "^18.2.0",
8 "react-dom": "^18.2.0",
9 "react-scripts": "^5.0.1",
10 "webpack": "^5.74.0",
11 "webpack-cli": "^4.10.0",
12 "webpack-dev-server": "^4.11.1"
13 },
14 "name": "myawesomeapp",
15 "version": "1.0.0",
16 "main": "index.html",
17 "scripts": {
18 "dev": "webpack serve",
19 "build": "webpack",
20 "clean": "rm -rf node_modules",
21 "reinstall": "npm run clean && npm install",
22 "rebuild": "npm run clean && npm install && npm run build",
23 "test": "echo \"Error: no test specified\" && exit 1"
24 },
25 "author": "",
26 "license": "ISC",
27 "description": "",
28 "devDependencies": {
29 "create-react-class": "^15.7.0"
30 }
31 }
Finally we can run: npx webpack --config .\webpack.config.js or npm run build
This command runs webpack and does all the things we’ve specified in our webpack.config.js and
package.json config files.