Webpack Concept - ChoDragon9/posts GitHub Wiki

์›นํŒฉ ์ปจ์…‰

์š”์•ฝ

  • Entry : ์›นํŒฉ ๋นŒ๋“œ ์‹œ์ž‘์ 
  • Output : ์›นํŒฉ ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฌผ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ณณ
  • Loaders : import๋ฅผ ํ†ตํ•ด ์ฝ์€ ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • Plugins : ์ฝ”๋“œ ์ตœ์ ํ™”๋ฅผ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

Entry

  • entry point๋Š” ์›นํŒฉ ๋นŒ๋“œ์˜ ์‹œ์ž‘์ ์ด๋‹ค.
  • ๋ชจ๋“ˆ๊ณผ ์˜์กด์„ฑ์ด ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๊ณ„์‚ฐํ•ด ๋‚ด๋ถ€์ ์ธ ์ข…์†์„ฑ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ ๋‹ค
  • String์œผ๋กœ ๋‹จ์ผ ํŒŒ์ผ์„ Entry๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , Array / Object Literal๋ฅผ ํ†ตํ•ด 2๊ฐœ ์ด์ƒ์˜ Entry ๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค.

Output

  • ์›นํŒฉ ๋นŒ๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ์„ ์ง€์ •ํ•œ๋‹ค.
  • ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฌผ์˜ ํŒŒ์ผ๋ช…์„ ์ง€์ •ํ•œ๋‹ค.

Loaders

  • webpack.config.js์— ์ž‘์„ฑํ•  ๋•Œ module์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ช…์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋กœ๋”๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์ด์™ธ ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
  • import์˜ ๊ธฐ๋ณธ ํ™•์žฅ์ž๋Š” .js๋กœ ๋กœ๋”๋ฅผ ์ถ”๊ฐ€์—†์ด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ES Feature๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋กœ๋”๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์›นํŒฉ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค
module.exports = {
  entry: './entry.js',
  output: {
    path: './dist',
    filename: 'bundle.js'
  },
  module: {
    rules: [
      { test: /\.txt$/, use: 'raw-loader' }
    ]
  }
}
  • rules[].test ๋Š” ๋ณ€ํ™˜๋  ํŒŒ์ผ๋“ค์„ ์ฐพ๋Š”๋‹ค.
  • rules[].use ๋Š” ๋ณ€ํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋กœ๋”๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  • ์œ„ ์„ค์ •์€ ์ด๋ ‡๊ฒŒ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค. import๋กœ .txtํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ raw-loader๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€ํ™˜ํ•˜๋ผ

Plugins

  • ๋กœ๋”๋ณด๋‹ค ๊ด‘๋ฒ”์œ„ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค.
  • ์„ฑ๋Šฅ ์ตœ์ ํ™”์™€ ์ฝ”๋“œ ์ตœ์†Œํ™” ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • webpack.config.js ํŒŒ์ผ์— require()๋ฅผ ํ†ตํ•ด ์ฝ์–ด์˜จ ๋’ค new operator๋ฅผ ํ†ตํ•ด ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
const webpack = require('webpack')
module.exports = {
  ..., //entry, output, module
  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
}

Resolve

  • ๋ชจ๋“ˆ(import, require)์ด ์–ด๋–ป๊ฒŒ ํ•ด์„๋˜๋Š” ์ง€๋ฅผ ์„ค์ •
  • ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋Šฅ์€ alias๋กœ ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.
module.exports = {
  //...
  resolve: {
    alias: {
      Utilities: path.resolve(__dirname, 'src/utilities/'),
      Templates: path.resolve(__dirname, 'src/templates/')
    }
  }
};
import Utility from '../../utilities/utility';