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

C-log

๐Ÿ”งWebpack : Loader ๋ณธ๋ฌธ

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

๐Ÿ”งWebpack : Loader

4:Bee 2024. 6. 23. 23:07
728x90

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋กœ๋”์— ๊ด€ํ•ด์„œ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์›นํŒฉ์€ ๋ชจ๋“  ํŒŒ์ผ๋“ค์„ ๋ชจ๋“ˆ๋กœ ๋ฐ”๋ผ๋ณธ๋‹ค. ๋”ฐ๋ผ์„œ import ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์•ˆ์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, import ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ๋ฐ”๋กœ ๋กœ๋” ๋•Œ๋ฌธ์ด๋‹ค. ๋กœ๋”๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๊ฑฐ๋‚˜ ์ด๋ฏธ์ง€๋ฅผ data URL ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ฟ๋งŒ์•„๋‹ˆ๋ผ CSS ํŒŒ์ผ์„ ์ž๋ฐ”์Šคํฌ๋ฆฝ์—์„œ ์ง์ ‘ ๋กœ๋”ฉํ•  ์ˆ˜  ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.


๋กœ๋”์˜ ๋™์ž‘ ์›๋ฆฌ

์šฐ์„  ์•„๋ž˜์™€ ๊ฐ™์ด ๋กœ๋”๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” js ํ•จ์ˆ˜ํ˜• ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

// my-webpack-loader.js

module.exports = function myWebpackLoader(content) {
  console.log('myWebpackLoader๊ฐ€ ๋™์ž‘ํ•จ');
  return content;
}

์ด์ œ ์ด ํŒŒ์ผ์„ webpack.config.js์—์„œ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

// webpack.config.js

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

module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          path.resolve('./my-webpack-loader.js')
        ]
      }
    ]
  }

 

  • module: Webpack ์„ค์ •์˜ ์ฃผ์š” ์„น์…˜ ์ค‘ ํ•˜๋‚˜๋กœ ๋ชจ๋“ˆ ๊ด€๋ จ ์„ค์ •์„ ํฌํ•จํ•œ๋‹ค.
  • rules: module ์„น์…˜ ๋‚ด์˜ ๋ฐฐ์—ด๋กœ ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์น™๋“ค์„ ์ •์˜ํ•œ๋‹ค. ๊ฐ ๊ทœ์น™์€ ๊ฐ์ฒด๋กœ ํ‘œํ˜„๋œ๋‹ค.
  • test: ๊ฐ ๊ทœ์น™ ๊ฐ์ฒด ๋‚ด์˜ ์†์„ฑ์œผ๋กœ ํŒŒ์ผ ์ด๋ฆ„ ๋˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ด ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š” ํŒŒ์ผ์— ๋Œ€ํ•ด ์ด ๊ทœ์น™์„ ์ ์šฉํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” \.js$ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  .js ํŒŒ์ผ์„ ๋Œ€์ƒ์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.
  • use: use ์†์„ฑ์€ ํ•ด๋‹น ํŒŒ์ผ์— ์ ์šฉํ•  ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ๋กœ๋”๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. path.resolve('./my-webpack-loader.js')๋Š” ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” my-webpack-loader.js ํŒŒ์ผ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋กœ๋”๋Š” my-webpack-loader.js ํŒŒ์ผ์—์„œ ์ •์˜ํ•œ ๋Œ€๋กœ .js ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑ์„ ํ•˜๊ณ  yarn run build๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

:: CMD
:: npm run build
:: yarn run build

myWebpackLoader๊ฐ€ ๋™์ž‘ํ•จ
myWebpackLoader๊ฐ€ ๋™์ž‘ํ•จ
Hash: 83800a72d220d80c4d42
Version: webpack 4.47.0
Time: 383ms
Built at: 2024. 07. 12. ์˜คํ›„ 7:29:05
  Asset      Size  Chunks             Chunk Names
main.js  4.57 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/app.js] 126 bytes {main} [built]
[./src/math.js] 47 bytes {main} [built]

์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ์˜๋ฌธ์ด ๋“ ๋‹ค. ์™œ 'myWebpackLoader๊ฐ€ ๋™์ž‘ํ•จ'์ด ๋‘ ๋ฒˆ ์ฐํž๊นŒ? ์šฐ์„  ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ webpack.config.js์˜ Entry์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ๋ณด๋ฉด './src/app.js'๋กœ ์„ค์ •์ด ๋˜์–ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ app.js๋Š” app.js๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „, math.js๋ฅผ ์‹คํ–‰ํ•˜๊ณ  app.js๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ฆ‰, js ํŒŒ์ผ ๋กœ๋”๊ฐ€ ๋‘ ๋ฒˆ ์‹คํ–‰ ๋œ ๊ฒƒ์ด๋‹ค.


๊ฐ„๋‹จํ•˜๊ฒŒ ๋กœ๋” ์ปค์Šคํ…€ํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” math.js์— ์žˆ๋Š” console.log๋ฅผ alert๋กœ ๋™์ž‘ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ปค์Šคํ…€ ํ•ด๋ณด์ž. ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

module.exports = function myWebpackLoader(content) {
  return content.replace('console.log(', 'alert(');
}

์ด์ œ ์ž˜ ์‹คํ–‰์ด ๋˜๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ น์„ ํ•ด๋ณด์ž.

:: CMD

start index.html

์‹คํ–‰์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜จ๋‹ค.

์ด๋Š” ์ดˆ๋ฐ˜ ๊ฐ•์˜์—์„œ๋„ cors ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

Access to script at 'file:///D:/myclass/0_Programming-content/Frontend-Development-Environment/dist/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.Understand this error
main.js:1 
        
        
Failed to load resource: net::ERR_FAILEDUnderstand this error

์œ„ ์—๋Ÿฌ๋Š” file ๊ฒฝ๋กœ๋กœ ํŒŒ์ผ์„ ์—ด๋ ค๊ณ  ์‹œ๋„ํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ณด์•ˆ ์˜ค๋ฅ˜์ด๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  Live Server๋กœ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด console.log๋กœ ์ฐํ˜„๋˜ '3'์ด alert๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.


๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋กœ๋”์— ๊ด€ํ•ด์„œ ๋‹ค๋ฃจ์–ด ๋ณผ ๊ฒƒ์ด๋‹ค.

728x90
Comments