๊ฐœ๋ฐœ Code/์›น๊ฐœ๋ฐœ WebDev

[WebDev][Webpack] Webpack ์†Œ๊ฐœ: ํ˜„๋Œ€์ ์ธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฒˆ๋“ค๋Ÿฌ

5hr1rnp 2025. 2. 16. 15:41
๋ฐ˜์‘ํ˜•

webpack

1. Webpack์ด๋ž€?


Webpack์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ(module bundler)๋‹ค. ์›น ๊ฐœ๋ฐœ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, CSS, ์ด๋ฏธ์ง€ ๋“ฑ์„ ํ•˜๋‚˜์˜ ๋ฒˆ๋“ค ํŒŒ์ผ๋กœ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.


2. Webpack์ด ํ•„์š”ํ•œ ์ด์œ 


์›น ๊ฐœ๋ฐœ์ด ์ ์  ๋ณต์žกํ•ด์ง€๋ฉด์„œ ๋‹ค์–‘ํ•œ ํŒŒ์ผ๊ณผ ๋ชจ๋“ˆ์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋Š” ํ•„์š”์„ฑ์ด ์ƒ๊น€. Webpack์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ์ด ์žˆ์Œ.

  • ๋ชจ๋“ˆํ™” ์ง€์›: ES6+ ๋ชจ๋“ˆ, CommonJS, AMD ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ์ง€์›ํ•จ
  • ํŒŒ์ผ ํฌ๊ธฐ ์ตœ์ ํ™”: ์ฝ”๋“œ ์••์ถ•, ํŠธ๋ฆฌ ์‰์ดํ‚น(Tree Shaking) ๋“ฑ์„ ํ†ตํ•ด ์ตœ์ ํ™” ๊ฐ€๋Šฅ
  • ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ ํ–ฅ์ƒ: HMR(Hot Module Replacement) ๊ธฐ๋Šฅ์œผ๋กœ ์ฝ”๋“œ ๋ณ€๊ฒฝ ์‹œ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋จ
  • ์ž์› ๊ด€๋ฆฌ: CSS, ์ด๋ฏธ์ง€, ํฐํŠธ ๋“ฑ๋„ ๋ชจ๋“ˆ์ฒ˜๋Ÿผ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

3. Webpack์˜ ํ•ต์‹ฌ ๊ฐœ๋…


Webpack์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ๋ฒˆ๋“ค๋งํ•˜๋Š” ๊ณผ์ •์—์„œ ์ค‘์š”ํ•œ ๋ช‡ ๊ฐ€์ง€ ๊ฐœ๋…์„ ์ œ๊ณตํ•จ.

(1) Entry (์ง„์ž…์ )

์–ด๋–ค ํŒŒ์ผ๋ถ€ํ„ฐ ๋ถ„์„ํ•˜์—ฌ ๋ฒˆ๋“ค๋งํ• ์ง€ ์„ค์ •ํ•˜๋Š” ํ•ญ๋ชฉ.

module.exports = {
  entry: './src/index.js',
};
 

์œ„ ์ฝ”๋“œ๋Š” src/index.js ํŒŒ์ผ์„ ์‹œ์ž‘์ ์œผ๋กœ ํ•˜์—ฌ ์˜์กด์„ฑ์„ ๋ถ„์„ํ•˜๊ณ  ๋ฒˆ๋“ค๋งํ•จ.

(2) Output (์ถœ๋ ฅ)

๋ฒˆ๋“ค๋ง๋œ ํŒŒ์ผ์ด ์ƒ์„ฑ๋  ์œ„์น˜์™€ ํŒŒ์ผ๋ช…์„ ์ง€์ •ํ•จ.

module.exports = {
  output: {
    filename: 'bundle.js',
    path: __dirname + '/dist',
  },
};
 

์œ„ ์„ค์ •์— ๋”ฐ๋ผ ๋ฒˆ๋“ค๋œ ํŒŒ์ผ์ด dist/bundle.js๋กœ ์ €์žฅ๋จ.

(3) Loaders (๋กœ๋”)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ด์™ธ์˜ ํŒŒ์ผ(CSS, ์ด๋ฏธ์ง€ ๋“ฑ)์„ Webpack์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•จ.

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
};
 

์œ„ ์„ค์ •์„ ํ†ตํ•ด .css ํŒŒ์ผ์„ style-loader์™€ css-loader๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ.

(4) Plugins (ํ”Œ๋Ÿฌ๊ทธ์ธ)

Webpack์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜์—ฌ ์ฝ”๋“œ ์••์ถ•, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •, HTML ํ…œํ”Œ๋ฆฟ ์ƒ์„ฑ ๋“ฑ์„ ์ง€์›ํ•จ.

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
  ],
};
 

์œ„ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๋ฉด src/index.html ํŒŒ์ผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ dist/index.html์„ ์ž๋™ ์ƒ์„ฑํ•จ.

(5) Mode (๋ชจ๋“œ)

Webpack์€ development(๊ฐœ๋ฐœ)๊ณผ production(์šด์˜) ๋ชจ๋“œ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ตœ์ ํ™” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋จ.

module.exports = {
  mode: 'development', // ๋˜๋Š” 'production'
};
 
  • development: ๋น ๋ฅธ ๋นŒ๋“œ๋ฅผ ์œ„ํ•œ ์„ค์ • (์†Œ์Šค๋งต ์ƒ์„ฑ, ์ตœ์ ํ™” X)
  • production: ์ฝ”๋“œ ์ตœ์ ํ™” ๋ฐ ๋‚œ๋…ํ™” ์ ์šฉ (ํŠธ๋ฆฌ ์‰์ดํ‚น, ๋ฏธ๋‹ˆํŒŒ์ด ๋“ฑ)

728x90
๋ฐ˜์‘ํ˜•

4. Webpack ์‚ฌ์šฉ ์˜ˆ์ œ


์•„๋ž˜๋Š” Webpack์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๋ฒˆ๋“ค๋ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์„ค์ • ์˜ˆ์ œ์ž„.

(1) ์„ค์น˜

npm init -y
npm install webpack webpack-cli --save-dev
npm install style-loader css-loader html-webpack-plugin --save-dev

(2) ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

/webpack-demo
 โ”œโ”€โ”€ /src
 โ”‚   โ”œโ”€โ”€ index.js
 โ”‚   โ”œโ”€โ”€ style.css
 โ”œโ”€โ”€ /dist
 โ”‚   โ”œโ”€โ”€ index.html
 โ”œโ”€โ”€ webpack.config.js
 โ”œโ”€โ”€ package.json

(3) webpack.config.js ์„ค์ •

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
  ],
  mode: 'development',
};

(4) ์‹คํ–‰ ๋ฐ ๋ฒˆ๋“ค๋ง

npx webpack
 

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด dist/bundle.js ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋ฉฐ, ์ด๋ฅผ index.html์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Œ.


5. Webpack์˜ ๋Œ€์•ˆ


Webpack ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๋ฒˆ๋“ค๋Ÿฌ๊ฐ€ ์กด์žฌํ•จ. ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Œ.

  • Vite: ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ๋น ๋ฅด๊ณ  ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•จ.
  • Parcel: ์„ค์ •์ด ๊ฑฐ์˜ ํ•„์š” ์—†๋Š” ๊ฐ„ํŽธํ•œ ๋ฒˆ๋“ค๋Ÿฌ.
  • Rollup: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฒˆ๋“ค๋ง์— ์ตœ์ ํ™”๋œ ๋„๊ตฌ.

6. ๊ฒฐ๋ก 


Webpack์€ ํ˜„๋Œ€์ ์ธ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์—์„œ ํ•„์ˆ˜์ ์ธ ๋„๊ตฌ ์ค‘ ํ•˜๋‚˜๋กœ, ๋ฒˆ๋“ค๋ง๊ณผ ์ตœ์ ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ณด๋‹ค ํšจ์œจ์ ์ธ ์›น์‚ฌ์ดํŠธ๋ฅผ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์—ญํ• ์„ ํ•จ. ํ•˜์ง€๋งŒ Vite ๋“ฑ์˜ ์ƒˆ๋กœ์šด ๋„๊ตฌ๋“ค์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ ํ”„๋กœ์ ํŠธ์˜ ํŠน์„ฑ์— ๋งž๋Š” ๋ฒˆ๋“ค๋Ÿฌ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•จ.

๋ฐ˜์‘ํ˜•