[TIL] Day62 #Webpack

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

TIL

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

2022.07.25(Mon)

[TIL] Day62
[SEB FE] Day63

๐ŸŽย ๋ฒˆ๋“ค๋ง

: ์‚ฌ์šฉ์ž์—๊ฒŒ ์›น์•ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํŒŒ์ผ ๋ฌถ์Œ ๐Ÿ‘‰ ๋ชจ๋“ˆ ๊ฐ„ ์˜์กด์„ฑ ๊ด€๊ณ„ ํŒŒ์•… ํ›„ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ์ž‘์—…

๐Ÿ—‚ย Webpack

: ์—ฌ๋Ÿฌ ํŒŒ์ผ โ†’ ํ•˜๋‚˜ ํŒŒ์ผ๋กœ ํ•ฉ์ณ์ฃผ๋Š” ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ๋กœ, ํ˜„์žฌ Front-end ์•ฑ ๋ฐฐํฌ์— ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ!

โœจย Webpack ํ•„์š”ํ•œ Best ์ด์œ ๋Š”? ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋น ๋ฅธ ๋กœ๋”ฉ ์†๋„ & ๋†’์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด!

โžฐย ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ
: ๊ฐ๊ฐ์˜ ๋ชจ๋“ˆ(HTML, CSS, JS ๋“ฑ์˜ ์ž์›)์„ ์กฐํ•ฉํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฌถ์Œ์œผ๋กœ ๋ฒˆ๋“ค๋ง(๋นŒ๋“œ)ํ•˜๋Š” ๋„๊ตฌ

โžฐย Build
: ๊ฐœ๋ฐœ ์™„๋ฃŒ ์•ฑ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด ํ•˜๋‚˜์˜ ํด๋”๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ์ค€๋น„ํ•˜๋Š” ์ž‘์—…

๐Ÿ“Žย Webpack ์†์„ฑ

  1. Target: Browser Compatibility์™€ ์—ฐ๊ด€๋œ ์†์„ฑ. [default-web]

  2. Entry(= Entry point): ์ž‘์„ฑ ์ฝ”๋“œ ์‹œ์ž‘์ !

    • Webpack์ด ๋‚ด๋ถ€ dependency graph ์ƒ์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋“ˆ
      [default-./src/index.js]
  3. Output: ์ƒ์„ฑ๋œ ๋ฒˆ๋“ค์„ ๋‚ด๋ณด๋‚ผ ์œ„์น˜ & ํŒŒ์ผ ์ด๋ฆ„ ์ง€์ • ๋ฐฉ๋ฒ•์„ webpack์— ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• 

    • ๊ธฐ๋ณธ ์ถœ๋ ฅ ํŒŒ์ผ โ†’ ./dist/main.js
    • ์ƒ์„ฑ๋œ ๊ธฐํƒ€ ํŒŒ์ผ โ†’ ./dist
      โœ‹ย path ์†์„ฑ ์‚ฌ์šฉ์‹œ์—” path ๋ชจ๋“ˆ ์‚ฌ์šฉํ•ด์•ผ ํ•จ
  4. Loader: webpack์€ JS, JSON ํŒŒ์ผ๋งŒ ์ดํ•ด! โ†’ Loader ์‚ฌ์šฉ์‹œ ๋‹ค๋ฅธ ์œ ํ˜• ํŒŒ์ผ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

    • test: ๋ณ€ํ™˜์ด ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค ์‹๋ณ„์„ ์œ„ํ•œ ์†์„ฑ
    • use: ๋ณ€ํ™˜ ์ˆ˜ํ–‰์‹œ ์‚ฌ์šฉ๋˜๋Š” Loader๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์†์„ฑ
    • exclude: Babel๋กœ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š์„ ํŒŒ์ผ / ํด๋” ์ง€์ •
      โ†”ย include: ๋ฐ˜๋“œ์‹œ ์ปดํŒŒ์ผํ•ด์•ผ ํ•  ํŒŒ์ผ / ํด๋” ์ง€์ •
  5. Plugins: ๋ฒˆ๋“ค ์ตœ์ ํ™” / asset ๊ด€๋ฆฌ / ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ฃผ์ž… ๋“ฑ ๊ด‘๋ฒ”์œ„ํ•œ ์ž‘์—… ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ
    a. require()๋ฅผ ํ†ตํ•ด ๋จผ์ € ํ”Œ๋Ÿฌ๊ทธ์ธ ์š”์ฒญํ•˜๊ธฐ
    b. plugins ๋ฐฐ์—ด์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ ์ถ”๊ฐ€ํ•˜๊ธฐ

    • new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœ โ†’ Plugin Instance ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•จ!

    • html-webpack-plugin: ์ƒ์„ฑ๋œ ๋ชจ๋“  ๋ฒˆ๋“ค ์ž๋™ ์‚ฝ์ž… โ†’ ์•ฑ์šฉ HTML ํŒŒ์ผ ์ƒ์„ฑ

    • mini-css-extract-plugin: CSS ๋ณ„๋„ ํŒŒ์ผ ์ถ”์ถœ โ†’ CSS ํฌํ•จํ•˜๋Š” JS ํŒŒ์ผ ๋‹น CSS ํŒŒ์ผ ์ž‘์„ฑํ•ด์ฃผ๊ฒŒ๋” ์ง€์›

  6. Optimization: ์„ ํƒํ•œ ํ•ญ๋ชฉ์— ๋”ฐ๋ผ ์ตœ์ ํ™” ์‹คํ–‰

    • minimize: TerserPlugin / optimization.minimize์— ๋ช…์‹œ๋œ plugins
      • bundle ํŒŒ์ผ์„ ์ตœ์†Œํ™”(์••์ถ•)์‹œํ‚ค๋Š” ์ž‘์—… ์—ฌ๋ถ€ ๊ฒฐ์ •
    • minimizer: TerserPlugin ์ธ์Šคํ„ด์Šค ์ œ๊ณตํ•ด์„œ ์žฌ์ •์˜ ๊ฐ€๋Šฅ

๐Ÿ“Žย Webpack ์‚ฌ์šฉํ•˜๊ธฐ

agora-states-reference์— ์ ์šฉํ•ด๋ณด๊ธฐ ๐Ÿ™Œ

  1. Directory ์ƒ์„ฑ

  2. npm init -y

  3. script.js ํŒŒ์ผ ์ƒ์„ฑ
    a. data.js ์ฆ‰, ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ”๋“œ ๋งˆ์ง€๋ง‰์— module.exports = agoraStatesDiscussions; ์ฝ”๋“œ ์ถ”๊ฐ€
    b. script.js์—์„œ const agoraStatesDiscussions = require("./data.js"); ์ฝ”๋“œ ์ถ”๊ฐ€ํ•ด์„œ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ!

  4. Webpack ์„ค์น˜

    $ npm install -D webpack webpack-cli
  5. Webpack config ํŒŒ์ผ ์ž‘์„ฑ

    // webpack.config.js
    const path = require('path');
    
    module.exports = {
      entry: './script.js',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.bundle.js',
      },
    }
  6. ๋ฒˆ๋“ค๋งํ•˜๊ธฐ

    $ npm webpack
  7. npm run build ์„ค์ •

    // package.json
    
    "scripts": {
        "build": "webpack", // ์š”๊ธฐ ์ฝ”๋“œ ์ถ”๊ฐ€!
        "test": "echo \"Error: no test specified\" && exit 1"
      },
  8. index.html ํŒŒ์ผ ์ฝ”๋“œ ์ˆ˜์ •

    <!-- index.html -->
    
    <script src="data.js"></script>
    <script src="script.js"></script>
    
    <!-- ๊ธฐ์กด์˜ ์œ„ ์ฝ”๋“œ๋Š” ์ง€์šฐ๊ณ  ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฒˆ๋“ค ํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ! -->
    
    <script src="app.bundle.js"></script>
  9. style-loader, css-loader ์„ค์น˜ โ†’ css style ์ ์šฉ

    $ npm i -D css-loader style-loader
    // webpack.config.js rules ์ถ”๊ฐ€!
    
    module: {
        rules: [
          {
            // ํŒŒ์ผ๋ช…์ด .css๋กœ ๋๋‚˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์— ์ ์šฉ
            test: /\.css$/, // .css๋กœ ํŒŒ์ผ๋ช…์ด ๋๋‚˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์— ์ ์šฉ
            use: ["style-loader", "css-loader"], // ๋ฐฐ์—ด ๋‚ด์—์„œ <- ์ˆœ์œผ๋กœ ์ ์šฉ
            exclude: /node_modules/, // loader๊ฐ€ node_modules ๋‚ด์šฉ๋„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์–œ ์ œ์™ธํ•ด์•ผ ํ•จ
          },
        ],
      },
    // script.js
    
    // css ํŒŒ์ผ ์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ
    require("./style.css");
    
    // ์ดํ›„ index.html์˜ <link rel="stylesheet" href="../style.css"> ์ฝ”๋“œ ์—†์• ๋„ style ์ž˜ ์ ์šฉ๋จ!
  10. html-webpack-plugin ์„ค์น˜

    $ npm i -D html-webpack-plugin
    // ์ตœ์ข… webpack.config.js
    const path = require("path");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    
    module.exports = {
      entry: "./script.js",
      output: {
        path: path.resolve(__dirname, "dist"),
        filename: "app.bundle.js",
      },
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"],
            exclude: /node_modules/,
          },
        ],
      },
    	// html-webpack-plugin ์ ์šฉ ๋ถ€๋ถ„
      plugins: [
        new HtmlWebpackPlugin({
          template: path.resolve(__dirname, "index.html"),
        }),
      ],
    };
  11. ๋งˆ์ง€๋ง‰์œผ๋กœ npm run build ๋‹ค์‹œ ํ•ด์ฃผ๋ฉด ๋~!

  12. Github ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ ํ›„ dist ํด๋” ์ด๋ฆ„์„ docs๋กœ ๋ณ€๊ฒฝ ํ›„,
    app.bundle.js & index.html ๋‘ ํŒŒ์ผ๋งŒ ์—…๋กœ๋“œ ํ›„ github page์— ๋ฐฐํฌํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ž˜ ๋‚˜์˜ด!!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-07-25 แ„‹แ…ฉแ„’แ…ฎ 5 48 19

โœจย ๋ฐฐํฌ ์„ฑ๊ณต ๐Ÿ™Œย  https://beanxx.github.io/fe-sprint-webpack/

profile
FE developer

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