React Setup From the Scratch

Ji Kim·2021년 4월 19일
0

React

목록 보기
1/1

Aside from simply installing the React development set-up by the command,

npm create-react-app

We will try to manually create the development setting. Before starting the set-up, the directory will look as below

-- React_Practice
----- dist
--------- index.html
----- src
--------- App.js
--------- index.js
----- .gitignore
----- package-lock.json
----- package.json
----- webpack.config.js

dist / index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
    <script src="bundle.js"></script>
</body>
</html>

src / App.js

import React from 'react';

function App() {
    return (
        <div>
            <h1> React Component </h1>
        </div>
    );
}

export default App;

src / index.js

import React from 'react';
import ReactDom from "react-dom";
import App from './App';

ReactDom.render(<App />, document.getElementById('root'));

webpack.config.js

module.exports = {
    entry : [
        "./src/index.js"
    ],
    output : {
        path : __dirname + '/dist', 
        publicPath : "/", 
        filename : "bundle.js",
    }, 
    module : {
        rules : [
            {
                test : /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options : {
                        presets : ['@babel/preset-env']
                    }
                }
            }
        ]
    },
    resolve : {
        extensions : ["*", ".js", ".jsx"]
    },
    devServer : {
        contentBase : "./dist", 
        port : 3000, 
        historyApiFallback : true 
    }
}

Commands

  1. Initialize NPM (creates package.json)
npm init -y
  1. Create package-lock.json
npm i -s react react-router-dom react-dom
  1. Install Webpack
npm i -D webpack webpack-cli webpack-dev-server
  1. Install Babel Loader
npm i -D babel-loader @babel/core @babel/preset-env @babel/preset-react

webpack.config.js

module.exports = {
    entry : [
        "./src/index.js" // check directory 
    ],
    output : {
        path : __dirname + '/dist', 
        publicPath : "/", 
        filename : "bundle.js",
    }, 
    module : {
        rules : [
            {
                test : /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options : {
                        presets : ['@babel/preset-env']
                    }
                }
            }
        ]
    },
    resolve : {
        extensions : ["*", ".js", ".jsx"]
    },
    devServer : {
        contentBase : "./dist",  // check directory 
        port : 3000, 
        historyApiFallback : true 
    }
}

package.json

{
  "name": "start",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack serve --config ./webpack.config.js --mode development"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ]
  },
  "dependencies": {
    "axios": "^0.21.1",
    "dotenv-webpack": "^7.0.2",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.13.10",
    "@babel/preset-env": "^7.13.10",
    "@babel/preset-react": "^7.12.13",
    "babel-loader": "^8.2.2",
    "webpack": "^5.26.0",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.2"
  }
}

Output

profile
if this then that

0개의 댓글