React Template App (w/o create-react-app)

  1. mkdir
  2. cd to newly created
  3. npm init -y

     [2007][react-from-scratch]$ npm init -y
     Wrote to /home/brigzz/webdev/react-from-scratch/package.json:
    
     {
       "name": "react-from-scratch",
       "version": "1.0.0",
       "description": "",
       "main": "index.js",
       "scripts": {
         "test": "echo \"Error: no test specified\" && exit 1"
       },
       "keywords": [],
       "author": "",
       "license": "ISC"
     }
    
  4. Install react and react-dom libraries:
    • npm install react react-dom
     [2008][react-from-scratch]$ npm install react react-dom
     npm notice created a lockfile as package-lock.json. You should commit this file.
     npm WARN react-from-scratch@1.0.0 No description
     npm WARN react-from-scratch@1.0.0 No repository field.
    
     + react@16.13.1
     + react-dom@16.13.1
     added 8 packages from 3 contributors and audited 22 packages in 10.993s
     found 0 vulnerabilities
    

    You should see some updates in package.json as well as libraries and dependencies in node_modules folder:

     [2019][react-from-scratch]$ ls node_modules
     js-tokens     object-assign  react      react-is
     loose-envify  prop-types     react-dom  scheduler
    
  5. At this point, you might want to create a .git-ignore file and add node_modules and dist to the list so they will be updated in your git repository.

     [2021][react-from-scratch]$ cat .gitignore
     node_modules
     dist
    
  6. Create folder app. Add index.html, index.css and index.js inside the folder.

     [2022][react-from-scratch]$ mkdir app
     [2023][react-from-scratch]$ ls
     app  node_modules  package.json  package-lock.json
     [2024][react-from-scratch]$ touch index.css index.js index.html
     [2030][react-from-scratch]$ ls app
     index.css  index.js	 index.html
    
  7. Boilerplate for index.html

     <!DOCTYPE html>
     <html lang="en">
     <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <title>React from Scratch</title>
     </head>
     <body>
         <div id="app"></div>
     </body>
     </html>
    
  8. Add to index.js

     const React = require('react');
     const ReactDOM = require('react-dom');
     require('./index.css');
    
     class App extends React.Component {
         render() {
             return(
                 <div>Hello World</div>
             )
         }
     }
    
     ReactDOM.render(
         <App />, document.getElementById('app')
     )
    
  9. index.css

     body {
         background-color: bisque;
     }
    
     #app {
         color: lightseagreen;
     }
    
  10. There are a lot of variations as to what to install for babel and webpack. For now I will be installing these:

    npm install --save-dev @babel/core @babel/preset-env @babel/preset-react webpack webpack-cli webpack-dev-server babel-loader css-loader style-loader html-webpack-plugin
    
    //not installed
    html-loader 
    
  1. Create webpack.config.js file in root folder of the application.

    [2033][react-from-scratch]$ touch webpack.config.js
    [2034][react-from-scratch]$ ls
    app  node_modules  package.json  package-lock.json  webpack.config.js
    
  2. Add below code into file:

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    
    module.exports = {
        mode: 'development',
        entry: './app/index.js',
        output: {
            //location of bundle directory/file
            path: path.resolve(__dirname, 'dist'),
            filename: 'index_bundle.js'
        },
        module: {
            rules: [
                { test: /\.js$/, use: 'babel-loader'},
                { test: /\.css$/, use:[ 'style-loader', 'css-loader']}
            ]
        },
        plugins: [
            new HtmlWebpackPlugin({template: './app/index.html'})
        ]
    };
    
  3. Add the following to package.json for babel and scripts:

      "babel" : {
        "presets": ["@babel/preset-env", "@babel/preset-react"]
      },
      "scripts": {
        //for bundling into dist folder
        "bundle": "webpack",
        //for dev
        "start-dev" : "webpack-dev-server --open"
      },
    
  4. To bundle:

    [2038][react-from-scratch]$ npm run bundle
    

    To run as dev server:

    [2038][react-from-scratch]$ npm run start-dev