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

C-log

๐Ÿ”งWebpack : css-loader, style-loader ๋ณธ๋ฌธ

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

๐Ÿ”งWebpack : css-loader, style-loader

4:Bee 2024. 7. 17. 21:17
728x90

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” loader์— ๊ด€ํ•ด์„œ ๋‹ค๋ฃจ์–ด ๋ณผ ๊ฒƒ์ด๋‹ค. ๋จผ์ € css-loader์™€ style-loader๋ฅผ ๋‹ค๋ฃจ์–ด ๋ณผ๊ฒƒ์ด๋‹ค. ๊ฐ•์˜๋Š” ์งง์ง€๋งŒ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ๋‚ด์šฉ์ด ๋งŽ์•„์„œ ๋‘๋ฒˆ์— ๋‚˜๋ˆ„์–ด์„œ ํฌ์ŠคํŒ…์„ ํ•  ๊ฒƒ์ด๋‹ค.


//app.js

import * as math from './math.js'
import './app.css';
// import { sum } from './math.js';
console.log(math.sum(1, 2));
// console.log(sum(1,2));
//app.css

body {
  background-color: green;
}

์œ„์™€ ๊ฐ™์ด cssํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  app.js์— importํ•˜๊ณ  build๋ฅผ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

D:\myclass\0_Programming-content\Frontend-Development-Environment>yarn run build
Hash: b21c535998629576ab9e
Version: webpack 4.47.0
Time: 515ms
Built at: 2024. 07. 17. ์˜คํ›„ 6:26:18
  Asset      Size  Chunks             Chunk Names
main.js  5.34 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/app.css] 272 bytes {main} [built] [failed] [1 error]
[./src/app.js] 142 bytes {main} [built]
[./src/math.js] 47 bytes {main} [built]

ERROR in ./src/app.css 1:5
Module parse failed: Unexpected token (1:5)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> body {
|   background-color: green;
| }
 @ ./src/app.js 2:0-19

์™œ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ ํ–ˆ์„๊นŒ? ์ด๋Š” ์ž๋ฐ”์ŠคํŠธ๋ฆฝํŠธ๋กœ๋ถ€ํ„ฐ css๋ฅผ ๋ชจ๋“ˆ๋กœ ์ฒ˜๋ฆฌํ•จ์— ์žˆ์–ด์„œ ๋ฐœ์ƒ๋œ ๋ฌธ์ œ์ด๋‹ค. ์ด๋ฅผ css-loader์ธ๋ฐ ์ด๋ฒˆ ์‹œ๊ฐ„์— ์šฐ๋ฆฌ๋Š” ์ด๋ฅผ ๋‹ค๋ฃจ์–ด ๋ณผ ๊ฒƒ์ด๋‹ค. ๋จผ์ € loader๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

::CMD

npm install css-loader

yarn add css-loader

์ด๋ ‡๊ฒŒ css-loader๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๊ธฐ์กด package.json๊ณผ ๋‹ฌ๋ฆฌ dependcies์— css-loader๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๋ฐฐํฌ๊ฐ€ ๋  ๋•Œ ์‚ฌ์šฉ๋  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

//(์ „)-package.json
{
  "name": "Frontend-Development-Environment",
  "version": "1.0.0",
  "packageManager": "yarn@4.3.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "echo \"์—ฌ๊ธฐ์— ์Šคํƒ€ํŠธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.\""
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10"
  }
}
//(ํ›„)-package.json
{
  "name": "Frontend-Development-Environment",
  "version": "1.0.0",
  "packageManager": "yarn@4.3.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "echo \"์—ฌ๊ธฐ์— ์Šคํƒ€ํŠธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.\""
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10"
  },
  "dependencies": {
    "css-loader": "^7.1.2"
  }
}

์ด์ œ webpack.config ํŒŒ์ผ๋กœ ๋Œ์•„๊ฐ€์„œ css๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ • ํ•  ๊ฒƒ์ด๋‹ค. 

const path = require('path');

module.exports = {
  mode: 'development',
  entry: {
    main: './src/app.js'
  },
  output: {
    path: path.resolve('./dist'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          path.resolve('./my-webpack-loader.js')
        ]
      }
      {
        test: /\.css$/,
        use: [
          'css-loader'
        ]
      }
    ]
  }
}

์œ„์™€ ๊ฐ™์ด ์„ค์ •์„ ํ•˜๋ฉด Entry ํฌ์ธํ„ฐ ๋ถ€ํ„ฐ ๋ชจ๋“  ํŒŒ์ผ๋“ค์„ ํ™•์ธํ•˜๋ฉด์„œ ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ css ํŒŒ์ผ์„ ํ™•์ธํ•˜๊ณ  ์ด๋ฅผ css-loader๊ฐ€ css ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋‹ค์‹œ yarn run build๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ๋นŒ๋“œ๊ฐ€ ๋˜๊ณ  dist ํด๋” ์•„๋ž˜๋ฅผ ํ™•์ธํ•˜๋ฉด main.jsํด๋” ๋‚ด๋ถ€์— ์•„๋ž˜์™€ ๊ฐ™์ด css์ฝ”๋“œ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

//main.js

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

{
  ...body {\\n|   background-color: green;\\n| }\");\n\n//# sourceURL=webpack:///./src/app.css?
}

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

์ด์ œ liver-server๋กœ ํ™•์ธ์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Live reload enabled.
app.css:1 Uncaught Error: Module parse failed: Unexpected token (1:5)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> body {
|   background-color: green;
| }
    at eval (app.css:1:7)
    at ./src/app.css (main.js:96:1)
    at __webpack_require__ (main.js:20:30)
    at eval (app.js:3:66)
    at ./src/app.js (main.js:108:1)
    at __webpack_require__ (main.js:20:30)
    at main.js:84:18
    at main.js:87:10

๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰๋œ ์›น์˜ ๊ฐœ๋ฐœ๋„๊ตฌ๋กœ body๋ฅผ ํƒ์ƒ‰ํ•ด๋ณด๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ css๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

body์— ์•„๋ฌด๊ฒƒ๋„ ์„ค์ •๋œ ๊ฒƒ์ด ์—†๋‹ค. ํ•˜์ง€๋งŒ NetWork์—์„œ JS์˜ preview๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด backgroud-color๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์ž˜ ์„ค์ •๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ์ด๋ ‡๊ฒŒ ๋œ ๊ฒƒ์ผ๊นŒ? ๊ทธ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ DOM์„ ๊ฐ„๊ณผํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ ๋ง์€ HTML์ฝ”๋“œ๊ฐ€ DOM์ด๋ผ๋Š” ๋ชจ์Šต์œผ๋กœ ๋ณ€ํ™˜์ด ๋˜์ง€ ์•Š์•„์„œ ๊ทธ๋ ‡๋‹ค. ์ฆ‰, JS๊ฐ€ Document ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜์ด ๋˜์–ด์•ผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ฌธ์„œ๋กœ ํ™•์ธ์ด ๋  ์ˆ˜ ์žˆ๋“ฏ css๋Š” CSS-OM์ด๋ผ๋Š” ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” HTMLํŒŒ์ผ์—์„œ cssํŒŒ์ผ์„ ์ง์ ‘ ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ inline-Style๋กœ ์ง์ ‘ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.(css๊ฐ€ css ํŒŒ์ผ๋ณด๋‹ค css-inline-Style์„ ๋จผ์ € ์ฝ๋Š” ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์— ํฌํ•จ๋œ๋‹ค.) ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” JSํŒŒ์ผ์—์„œ ๋ชจ๋“ˆ๋กœ css๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ํŒจํ‚ค์ง€๋ฅผ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€์ ์ด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์ž.

::CMD

yarn add style-loader

npm install style-loader

Style-loader๋Š” JS๋กœ ๋ณ€ํ™˜๋œ css๋ฅผ HTML์— ๋„ฃ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์—ญ์‹œ๋‚˜ pacakge.json์˜ dependenies์— style-loader๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ webpack.config ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

const path = require('path');

module.exports = {
  mode: 'development',
  entry: {
    main: './src/app.js'
  },
  output: {
    path: path.resolve('./dist'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          path.resolve('./my-webpack-loader.js')
        ]
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }
    ]
  }
}

์ถ”๊ฐ€ ์„ค์น˜๊ฐ€ ๋˜์—ˆ์œผ๋ฉด ๋‹ค์‹œ run build๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

D:\myclass\0_Programming-content\Frontend-Development-Environment>yarn run build        
Hash: 538273a0ce3b6faa1797
Version: webpack 4.47.0
Time: 173ms
Built at: 2024. 07. 17. ์˜คํ›„ 7:10:02
  Asset     Size  Chunks             Chunk Names
main.js  4.7 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/app.js] 142 bytes {main} [built]
[./src/math.js] 47 bytes {main} [built]

ERROR in ./src/app.js
Module not found: Error: Can't resolve 'style-loader' in 'D:\myclass\0_Programming-content\Frontend-Development-Environment'
 @ ./src/app.js 2:0-19

ํ˜„์žฌ style-loader๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๋ถˆ์ƒ์‚ฌ๊ฐ€ ๋ฒŒ์–ด์กŒ๋Š”๋ฐ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด yarn์˜ cacheํŒŒ์ผ์„ ์‚ญ์ œํ•ด ์ฃผ์—ˆ๋‹ค. ์ดํ›„ yarn install๋กœ ๋‹ค์‹œ yarn์„ ์„ค์น˜ํ•˜๊ณ  ๋‹ค์‹œ run build๋ฅผ ํ•ด์ฃผ์—ˆ์ง€๋งŒ ๊ณ„์†ํ•ด์„œ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ webpack๋ฅผ ์†์‹คํ•˜๊ฒŒ ๋˜๋Š” ๋ถˆ์ƒ์‚ฌ๊ฐ€ ์ผ์–ด๋‚ฌ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๋‹ค์‹œ webpack์„ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

webpack ๋ฎ์–ด์„œ ์žฌ์„ค์น˜

๋”๋ณด๊ธฐ

yarn add webpack --dev

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๊ณ„์†ํ•ด์„œ ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐ˜๋ณต์ ์ผ๊นŒ? ํ™•์ธํ•ด ๋ณด๋‹ˆ ํ˜„์žฌ ๋‚˜๋Š” node_modules ๊ธฐ๋ฐ˜์ด ์•„๋‹Œ pnp๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ pnp๊ฐ€ ์ผ๋ถ€ ํ˜ธํ™˜๋˜์ง€ ์•Š์•„์„œ ์ผ์–ด๋‚œ ๋ถˆ์ƒ์‚ฌ์˜€๋˜ ๊ฒƒ์ด๋‹ค. pnpFallbackMode: all์€ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” yarnrc.ymlํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

//yarnrc.yml

pnpFallbackMode: all

์ดํ›„ yarn cache clear๋ฅผ ์‹คํ–‰ํ•˜๊ณ  yarn install ํ›„ yarn run build๋ฅผ ํ•˜๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘์„ ํ–ˆ๋‹ค. 

::CMD

yarn cache clear
yarn install
yarn run build

ํ˜น์‹œ๋‚˜ ํ•ด์„œ package.json์„ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋ชจ๋“  ํŒจํ‚ค์ง€์™€ ์›นํŒฉ๋“ค ๋ฒ„์ „์ด ์ƒ์œ„ ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ ๋˜์–ด ์žˆ์—ˆ๋‹ค.

//package.json

  "devDependencies": {
    "webpack": "^5.75.0",
    "webpack-cli": "^4.10.0"
  },
  "dependencies": {
    "css-loader": "^6.7.1",
    "style-loader": "^3.3.1"
  }

์ˆ˜์—…์—์„œ ํ˜„์žฌ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๋ฒ„์ „๊ณผ๋Š” ๋งž์ง€ ์•Š์ง€๋งŒ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์šฐ์„ ์€ ์ด๋ ‡๊ฒŒ ๋ฒ„์ „์„ ๋งž์ถฐ์„œ ์ง„ํ–‰์„ ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

REFERNCE

728x90

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

๐Ÿ”งWebpack : url-loader  (0) 2024.07.19
๐Ÿ”งWebpack : file-loader  (0) 2024.07.17
๐Ÿ”งWebpack : Loader  (0) 2024.06.23
๐Ÿ”งWebpack : Entry  (0) 2024.06.21
๐Ÿ”งWebpack : module  (1) 2024.06.17
Comments