상세 컨텐츠

본문 제목

React with WEBPACK & BABEL

React

by nata_developer 2020. 8. 9. 10:35

본문

*Setup Webpack and Babel for React를 참고하였습니다.

(https://www.youtube.com/watch?v=ydDUm1yPZs0)

 

1.설치

 

dependencies

 

react

react-dom

 

devDependencies

 

webpack

webpack-cli

webpack-dev-server

 

@babel/core

@babel/preset-env

@babel/preset-react

babel-loader

style-loader

sass-loader

node-sass

 

2.파일 구조

CRA 파일 구조

1.webpack.config.js

const path = require("path")


module.exports = {
    entry: "./src/index.js",
    output: {
        path: path.join(__dirname, "public"),
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.scss$/,
                use: ["style-loader", "sass-loader"]
            }
        ]
    },
    devtool: "cheap-module-eval-source-map",
    devServer: {
        contentBase: path.join(__dirname, "public"),
        port: 9000,
    },
}

 

2.Public

 

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 App</title>
</head>
<body>
    <div id="root"></div>
    <script src="./bundle.js"></script>
</body>
</html>

3.src

styles.scss

$bg-color: #333;
$text-color: #fff;

* {
    background-color: $bg-color;
    color: $text-color

    h1 {
        color: red
    }
}

 

.babelrc

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ]
}

 

index.js

import React from "react";
import reactDOM from "react-dom";
import "./styles/style.scss"

const App = () => {
    return <h1>Hello from react</h1>
}

reactDOM.render(<App/>, document.getElementById("root"))

'React' 카테고리의 다른 글

controlled component vs uncontrolled component  (0) 2020.08.10
class component vs functional component  (0) 2020.08.10
Props  (0) 2020.08.09
React  (0) 2020.08.09
JSX  (0) 2020.08.09

관련글 더보기