webpack output ์†์„ฑ

6203 ๋‹จ์–ด webpack

output ์†์„ฑ์ด๋ž€?



๋ฒˆ๋“ค ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์™€ ์ด๋ฆ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

webpack.config.js
const path = require('path');

module.exports = {
  entry: './src/index.js'
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  }
};

// ./dist/main.jsใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹
__dirname โ‹ฏ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ํŠน์ˆ˜ ๋ณ€์ˆ˜.
path โ‹ฏNode.js ๋ชจ๋“ˆ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜์™€ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜์˜ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์ƒˆ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
์ง€์ •ํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ OS์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค./hoge/dist ์™€ ๊ฐ™์ด ์ „๊ฐœ๋œ๋‹ค. ์ฐธ๊ณ 
( path: `${__dirname}/dist` ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•ด๋„ ์ข‹๋‹ค)

path ์†์„ฑ ์ƒ๋žต


path ์†์„ฑ์„ ์ƒ๋žตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
=> ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๋ฐ”๋กœ ์•„๋ž˜ dist ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

webpack.config.js
module.exports = {
  output: {
    filename: 'foo.js'
  }
};

// ./dist/foo.jsใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹

์—ฌ๋Ÿฌ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ๋ฅผ ๊ฐ๊ฐ ์ถœ๋ ฅํ•˜๋ ค๋Š” โ€‹โ€‹๊ฒฝ์šฐ



webpack.config.js
const path = require("path");

module.exports = {
  entry: {
    foo: './src/foo.js',
    bar: './src/bar.js'
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].js"
  }
};

// ./dist/foo.jsใจ./dist/bar.jsใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹
[name] ํ”Œ๋ ˆ์ด์Šคํ™€๋”์—๋Š” entry ์˜ ํ‚ค๊ฐ€ ๊ฐ๊ฐ ๋“ค์–ด๊ฐ„๋‹ค.
(์ด ๊ฒฝ์šฐ output ์†์„ฑ์„ ์ƒ๋žตํ•ด๋„ ๊ฒฐ๊ณผ๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ)

๊ทธ ์™ธ์—๋Š” ์ดํ•˜์™€ ๊ฐ™์€ ํ”Œ๋ ˆ์ด์Šคํ™€๋”๊ฐ€ ์žˆ๋‹ค.
  • [id] โ‹ฏ entry ๊ฐ์ฒด ๋‚ด์˜ ์ธ๋ฑ์Šค ๊ฐ’
  • [hash] โ‹ฏ ๋ฒˆ๋“ค ์ „์ฒด์—์„œ ์ƒ์„ฑ ๋œ ํ•ด์‹œ ๊ฐ’
  • [chunkhash] โ‹ฏ ๊ฐ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ์— ๋Œ€ํ•ด ์ƒ์„ฑ ๋œ ํ•ด์‹œ ๊ฐ’
  • more

  • ์ฆ‰, ์ด๋Ÿฌํ•œ ์„ค์ • ํŒŒ์ผ

    webpack.config.js
    const path = require("path");
    
    module.exports = {
      entry: {
        foo: "./src/bar.js",
        baz: "./src/qux.js"
      },
      output: {
        path: path.resolve(__dirname, "dist"),
        filename: "[id]-[name]-[hash].js"
      }
    };
    

    ์ด์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค.


    [hash]์™€ [chunkhash]๋Š” ๋™์‹œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ