๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

๐Ÿ”งWebpack : file-loader ๋ณธ๋ฌธ

โš›๏ธReact/๐Ÿ”งFront-Dev-Environment

๐Ÿ”งWebpack : file-loader

4:Bee 2024. 7. 17. 23:13
728x90

์šฐ์„  ์ด๋ฏธ์ง€๋ฅผ importํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด css๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค.

body {
  background-image: url(git-logo-icon.png);
}

  ์šฐ์„  yarn์˜ pnp๋กœ ๋™์ž‘์„ ์„ค์ •ํ•œ ๋‚˜์˜ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์—์„  ์˜ค๋ฅ˜๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ๋นŒ๋“œ๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ๋‚˜์˜ค๊ณ  ์žˆ๋‹ค. ๊ฑฐ๊ธฐ๋‹ค ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ๋‚ด๊ฐ€ ์„ค์ •ํ•œ image๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋‹ค. ์™œ๊ทธ๋Ÿฐ๊ฐ€ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋‚ด๊ฐ€ ์ด๋ฏธ node_modules๋ฅผ pnp๋กœ ์ „ํ™˜ํ•ด์„œ yarn-berry์˜ ํŠน์ง•์ธ cache ํด๋”์•ˆ์—์„œ ์ด๋ฏธ file-loader๊ฐ€ ์กด์žฌํ•ด์„œ ํ•ด๊ฒฐ์ด ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ง์ž‘์„ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‚˜์˜ ์ถ”์ธก๊ณผ๋Š” ์ „ํ˜€ ๋ฌด๊ด€ํ–ˆ๋‹ค. file-loader๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋ฐ”๋กœ webpack.configํŒŒ์ผ์„ ์„ค์ •ํ•˜๋‹ˆ ๊ทธ์ œ์„œ์•ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด file-loader๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.

::CMD

npm install file-loader

yarn install file-loader

 

 

  ํ•˜์ง€๋งŒ webpack.cofing ์„ค์ • ๊ณผ์ •์—์„œ file-loader๋ฅผ ์ง์ ‘ ๋‹ค๋ฃจ๋ ค๊ณ  ํ•˜๋‹ˆ file-loader์—†์ด ์ž˜ ๋™์ž‘ํ•˜๋˜ ๋นŒ๋“œ๊ฐ€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ํ™•์ธ์„ ํ•ด๋ณด๋‹ˆ webpack๊ณผ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋“ค์˜ ๋ฒ„์ „ ์ •๋ณด๊ฐ€ ์ˆ˜์—…์—์„œ ํ•˜๋Š” ๋ฒ„์ „๊ณผ ๋‹ฌ๋ผ์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์˜€๋‹ค. webpack 5์ด์ƒ ๋ถ€ํ„ฐ๋Š” file-loader๋ฅผ ํฌํ•จ์‹œ์ผฐ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด์— ๋”ฐ๋ผ webpack๊ณผ ๋‚˜๋จธ์ง€ ํŒจํ‚ค์ง€๋“ค์„ ๋ชจ๋‘ ์ˆ˜์—…์—์„œ ๋‹ค๋ฃจ๋Š” ๋ฒ„์ „์œผ๋กœ ๋‹ค์‹œ ๋งž์ถ”๊ณ  ์‹คํ–‰ํ•ด ๋ณด๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ์ด์ „๊ณผ ๊ฐ™์ด style-loader๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๋ถˆ์ƒ์‚ฌ๊ฐ€ ๋ฐ˜๋ณต๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆ˜์—…๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ ์ตœ์‹  ๋ฒ„์ „์— ๋งž์ถฐ์„œ webpack.config๋ฅผ ์„ค์ •ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

//webpack.config.js (5 ์ด์ƒ ๋ฒ„์ „)

  {
    test: /\.png$/,
    type: 'asset/resource'
  }
//webpack.config.js (5 ์ดํ•˜ ๋ฒ„์ „)

  {
    test: /\.png$/,
    use: [
      'file-loader'
    ],
  },

  ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค๋ช…ํ•˜์ž๋ฉด  ์›นํŒฉ 5๋ฒ„์ „ ์ด์ƒ์—์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋‹ค. file-loader, url-loader, ๋ฐ raw-loader์™€ ๊ฐ™์€ ๋กœ๋”๋“ค์€ ์›นํŒฉ 5๋ถ€ํ„ฐ๋Š” ์ž์ฒด์ ์œผ๋กœ ํฌํ•จ๋œ ๊ธฐ๋Šฅ์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํŠน์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ์—ฌ์ „ํžˆ file-loader์™€ ๊ฐ™์€ ๋กœ๋”๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ ค๋ฉด ์œ„์™€ ๊ฐ™์ด type: 'asset/resource'๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

์ด์ œ ์›นํŒฉ ๋ฒ„์ „๊ณผ yarn-berry์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๋Š” ๋’ค๋กœํ•˜๊ณ  distํด๋”๋กœ ๊ฐ€๋ณด๋ฉด ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

dist
โ”œโ”€โ”€ e6cb70b6c44cc2e8835a.png
โ””โ”€โ”€ main.js

  ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•ด ๋†“์€ ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ํŒŒ์ผ๋ช…์ด ์•„๋‹Œ ํ•ด์‹œ ๊ฐ’์œผ๋กœ ์ด๋ฆ„์ด ๋ณ€๊ฒฝ ๋˜์–ด ์žˆ๋‹ค. ์›นํŒฉ์€ ๋นŒ๋“œ๊ฐ€ ๋  ๋•Œ ๋งˆ๋‹ค ์ด๋ ‡๊ฒŒ ํ•ด์‹œ ๊ฐ’์œผ๋กœ ํŒŒ์ผ๋ช…์ด ๋ณ€๊ฒฝ๋œ๋‹ค. ์ •์ ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ์บ์‹ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•ด์‹œ๊ฐ’์„ ์ด์šฉํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ „์—์„œ ์บ์‹ฑ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ด๋ฏธ์ง€ ์™ธ์—๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋‚˜ css, ์ด๋ฏธ์ง€ ํฐํŠธ๊ฐ€ ์žˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋Š” ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ํŒŒ์ผ์„ ๋‹ค์‹œ ๋‹ค์šด๋กœ๋“œํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํŒŒ์ผ์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ตœ์‹  ๋ฒ„์ „์„ ๋กœ๋“œํ•˜๋„๋ก ํŒŒ์ผ ์ด๋ฆ„์— ํ•ด์‹œ ๊ฐ’์„ ํฌํ•จ์‹œํ‚ค๋ฉด ํŒŒ์ผ ๋‚ด์šฉ์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํŒŒ์ผ ์ด๋ฆ„์ด ์ƒ์„ฑ๋˜๋ฏ€๋กœ ๋ธŒ๋ผ์šฐ์ €๋Š” ํ•ญ์ƒ ์ตœ์‹  ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๊ฒŒ ๋œ๋‹ค.

ํŒŒ์ผ์„ ์ด๋ฆ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ webpack.config์—์„œ ์„ค์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 //webpack.config.js (5 ๋ฒ„์ „ ์ดํ•˜)
 
 {
    test: /\.png$/,
    loader: 'file-loader',
    options: {
    	publicPath: './dist/',
        name: '[name].[ext]?[hash]'
    }
  },

 

//webpack.config.js (5 ๋ฒ„์ „ ์ด์ƒ)

 {
    test: /\.png$/,
    type: 'asset/resource',
    generator: {
      filename: 'assets/[name].[hash][ext]',
      publicPath: './dist/',
    },
  }

  ์ž‘์„ฑ์ž๋Š” assetsํด๋” ์•„๋ž˜์— ๋ณธ๋ž˜ ํŒŒ์ผ๋ช…๊ณผ ํ•จ๊ป˜ ํ•ด์‹œ๊ฐ’์„ ๋ถ€์—ฌํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๋กœ distํด๋”์— ํŒŒ์ผ๋“ค์ด ์„ค์ • ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

dist
โ”œโ”€โ”€ assets
โ”‚   โ””โ”€โ”€ git-logo-icon.e6cb70b6c44cc2e8835a.png
โ”‚
โ”œโ”€โ”€ e6cb70b6c44cc2e8835a.png
โ””โ”€โ”€ main.js

main.js์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

//main.js

/** ์ดํ•˜์ƒ๋žต */

...("module.exports = \"./dist/assets/git-logo-icon.e6cb70b6c44cc2e8835a.png\";\n\n//#...

/** ์ดํ•˜์ƒ๋žต */

  ๊ตฌ๋ฒ„์ „์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ์ ์ด ๋งค์šฐ ์•„์‰ฝ์ง€๋งŒ ์ตœ์‹  ๋ฒ„์ „์— ์ ์‘์„ ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ด ๊ธ‰์„ ๋ฌด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋˜์–ด์„œ ์ด๋ ‡๊ฒŒ ์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ์ˆ˜ํ–‰ํ•ด ๋ณด์•˜๋‹ค. yarn-berry๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ธํŒ…ํ•ด์„œ yarn-berry์˜ pnp ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๊ฐ€ ๊ฐ€์žฅ ํด ๊ฒƒ์œผ๋กœ ์ถ”์ธก์ด ๋œ๋‹ค. ์•„๋ฌด๋ž˜๋„ yarn-berry์˜ ๋‹จ์ ์ด ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์‹  ๋ฒ„์ „์— ๋งž๊ฒŒ ์„ค์ •์„ ํ•  ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค.

REFERCNE

728x90

'โš›๏ธReact > ๐Ÿ”งFront-Dev-Environment' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๐Ÿ”งWebpack : Plugin  (0) 2024.08.20
๐Ÿ”งWebpack : url-loader  (0) 2024.07.19
๐Ÿ”งWebpack : css-loader, style-loader  (0) 2024.07.17
๐Ÿ”งWebpack : Loader  (0) 2024.06.23
๐Ÿ”งWebpack : Entry  (0) 2024.06.21
Comments