[TIL] Day63 #React-Webpack

Beanxxยท2022๋…„ 7์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
63/120
post-thumbnail

2022.07.25(Tues)

[TIL] Day63
[SEB FE] Day64

โ˜‘๏ธย React์— Webpack ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

  1. Install

    # webpack ์„ค์น˜
    $ npm install -D webpack webpack-cli
    
    # babel ์„ค์น˜
    $ npm install --save-dev @babel/core babel-loader @babel/preset-react @babel/preset-env
    
    # web-dev-server, ๊ฐœ๋ฐœ์šฉ ์„œ๋ฒ„ ์„ค์น˜
    $ npm install --save-dev webpack-dev-server
    
    # html-webpack-plugin
    $ npm install --save-dev html-webpack-plugin
    
    # css-loader, style-loader ์„ค์น˜
    $ npm i -D css-loader style-loader
  1. webpack.config.js

    const path = require("path");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    
    module.exports = {
      mode: "development", // ์ž‘๋™ ๋ชจ๋“œ๋ฅผ ๊ฐœ๋ฐœ์ž๋ชจ๋“œ๋กœ ์„ค์ •
      entry: "./src/index.js",
      devServer: {
        static: "./dist",
      },
      output: {
        path: path.join(__dirname, "/dist"),
        filename: "[name].js",
        clean: true,  // ๋ฒˆ๋“ค๋งํ•  ๋•Œ ๋งˆ๋‹ค dist ๋””๋ ‰ํ„ฐ๋ฆฌ ์ •๋ฆฌ
      },
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader",
            },
          },
          {
            test: /.s?css$/,
            use: [MiniCssExtractPlugin.loader, "css-loader"],
            exclude: /node_modules/,
          },
        ],
      },
    	// Asset ๊ด€๋ฆฌ
      optimization: {
        minimizer: [new CssMinimizerPlugin()],
        runtimeChunk: "single",
      },
    
      // html-webpack-plugin
      plugins: [
        new HtmlWebpackPlugin({
          template: path.resolve(__dirname, "./public/index.html"),
        }),
        new MiniCssExtractPlugin(),
      ],
    };
  1. .babelrc ํŒŒ์ผ ์ถ”๊ฐ€

    {
      "presets": [
          "@babel/env",
          "@babel/react"
      ]
    }
  1. package.json โ€scriptsโ€ ๋ณ€๊ฒฝ

    "scripts": {
        "start": "webpack-dev-server --mode development --open --hot",
        "build": "webpack --mode production",
    },
  1. import React from "react";

    โœจย React undefined ... ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ App.js, components ๋‚ด์˜ js ํŒŒ์ผ์— ์•„๋ž˜ ์ฝ”๋“œ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•จ!

    import React from "react";

๐Ÿ“Žย github page์— webpack ๋ฐฐํฌํ•˜๊ธฐ

์˜ค๋Š˜์€ ์–ด์ œ github repository์— ์ง์ ‘ docs ํด๋”๋งŒ ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ํŽ˜์–ด๋‹˜์ด ์•Œ๋ ค์ฃผ์‹  ๋ฐฉ์‹๋Œ€๋กœ ๊นƒํ—™ ํŽ˜์ด์ง€ ๋ฐฐํฌ๋ฅผ ํ•ด๋ณด์•˜๋‹ค!

$ npm install gh-pages --save-dev
// package.json

// scripts ๋ถ€๋ถ„์— predeploy, deploy ์ฝ”๋“œ ์ถ”๊ฐ€
"scripts": {
    "predeploy": "npm run build",
		"deploy": "gh-pages -d dist"
},

// homepage์— github repository ์ฃผ์†Œ ์ถ”๊ฐ€
"homepage": "https://beanxx.github.io/fe-sprint-react-webpack/",
  • ์œ„์˜ ์„ค์ •ํ•ด์ฃผ๊ณ , github ํ•ด๋‹น repository์˜ settings - pages ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€์„œ github page ์ƒ์„ฑํ•ด์ฃผ๊ณ , ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์„ค์ • ๋!
    # ์•„๋ž˜ ์ฝ”๋“œ๋กœ deloy script๋ฅผ ์‹คํ–‰
    $ npm run deploy
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค์‹œ setting - pages ์—์„œ branch๋ฅผ gh-pages๋กœ ๋ฐ”๊พธ๊ณ , /root ํด๋”๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฐฐํฌ ๋ โœจ
profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€