[๐Ÿ“š ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ] Webpack loader ์‚ฌ์šฉ๋ฒ•

dsfasdยท2022๋…„ 11์›” 23์ผ
0

loader

loader๋Š” JavaScript, JSON์ด ์•„๋‹Œ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” CSS ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ ์ž ํ•œ๋‹ค.

์ค€๋น„๋ฌผ : dist/index.js, src/index.js , src/style.css ํŒŒ์ผ


1. ํŒŒ์ผ ์ค€๋น„

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ src/index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.
์ด๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด script src="index.js"๋กœ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด
index.jsํŒŒ์ผ์˜ const _ = require('./underbar.js'); require ๋ฌธ๋ฒ• ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
(require ๋ฌธ๋ฒ•์€ node.js์˜ ๋ฌธ๋ฒ•์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Shout Lorem Ipsum</title>
</head>
<body>
  <div id="app"></div>
  <script src="index.js"></script>
</body>
</html>

๋”ฐ๋ผ์„œ index.html ํŒŒ์ผ์„ dist ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์˜ฎ๊ฒจ์„œ ๋ฒˆ๋“ค ํŒŒ์ผ์ธ app.bundle.js ํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

dist/index.js

<!-- dist/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Shout Lorem Ipsum Once</title>
</head>
<body>
  <div id="app"></div>
  <script src="app.bundle.js"></script>
</body>
</html>

src/index.js

const _ = require('./underbar.js');

const shout = (...sentences) => console.log(...sentences);
const shoutToHTML = (...sentences) => {
  const app = document.querySelector('#app');
  app.append(...sentences.map(sentence => {
    const shoutHere = document.createElement('div');
    shoutHere.className = 'shout';
    shoutHere.textContent = sentence;
    return shoutHere;
  }))
  return;
};

const loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis egestas feugiat elit, ac tincidunt neque vestibulum at. Mauris a eros sit amet urna efficitur tempus."

const shoutOnce = _.once(shout);
const shoutToHTMLOnce = _.once(shoutToHTML);

shoutOnce(loremIpsum);
shoutOnce(loremIpsum);
shoutOnce(loremIpsum);
shoutOnce(loremIpsum);

shoutToHTMLOnce(loremIpsum);
shoutToHTMLOnce(loremIpsum);
shoutToHTMLOnce(loremIpsum);
shoutToHTMLOnce(loremIpsum);

npm run build ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ฒˆ๋“ค๋ง์„ ์ง„ํ–‰ํ•˜๋ฉด app.bundle.js ํŒŒ์ผ์— ๋ฒˆ๋“ค๋ง์ด ์ ์šฉ๋˜์–ด dist/index.html ํŒŒ์ผ ์‹คํ–‰์‹œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


2. CSS ํŒŒ์ผ ์ถ”๊ฐ€ -> ์ด ๊ณผ์ •์—์„œ loader ํ•„์š”!

css ํŒŒ์ผ์„ ์ƒ์„ฑ ํ›„ index.htmlํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

src/style.css

/* dist/style.css */
* {
  box-sizing: border-box;
  border: 0;
  padding: 0;
  margin: 0;
}

main {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

div.shout {
  padding: 12px;
  margin: 4px;
  border-radius: 8px;
  border: 0.5px solid gray;
}

dist/index.js

<link rel="stylesheet" href="style.css">

node src/index.js ๋กœ ์‹คํ–‰์‹œ node.js๋Š” css ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์—†์–ด์„œ ๋ฌธ๋ฒ• ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์•„์ง ๋กœ๋”๋ฅผ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜์ด๋‹ค.


3. ๋กœ๋” ์„ค์น˜ ๋ฐ config ํŒŒ์ผ ์ž‘์„ฑ

๋กœ๋” ์„ค์น˜

npm i -D css-loader style-loader

webpack.config.js ํŒŒ์ผ์—์„œ ๋กœ๋” ์ถ”๊ฐ€

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

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.bundle.js",
  },
  module: { // ๋กœ๋” 
    rules: [
      {
				// ํŒŒ์ผ๋ช…์ด .css๋กœ ๋๋‚˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์— ์ ์šฉ
        test: /\.css$/,
				// ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ ์ˆœ์œผ๋กœ ์ ์šฉ
				// ๋จผ์ € css-loader๊ฐ€ ์ ์šฉ๋˜๊ณ , styled-loader๊ฐ€ ์ ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค.
				// ์ˆœ์„œ ์ฃผ์˜!
        use: ["style-loader", "css-loader"],
				// loader๊ฐ€ node_modules ์•ˆ์˜ ์žˆ๋Š” ๋‚ด์šฉ๋„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—
				// node_modules๋Š” ์ œ์™ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
        exclude: /node_modules/,
      },
    ],
  },
};

npm run build๋ฅผ ํ†ตํ•ด ๋ฒˆ๋“ค๋ง ํ›„, dist/index.html ํŒŒ์ผ์„ ํ†ตํ•ด CSS๊ฐ€ ์ œ๋Œ€๋กœ ์ ์šฉ๋œ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฏธ css ํŒŒ์ผ๊นŒ์ง€ ๋ฒˆ๋“ค๋ง์ด ๋˜์—ˆ์œผ๋ฏ€๋กœ css ์—ฐ๊ฒฐํ•ด์ค€ link์€ ์‚ญ์ œํ•ด๋„ ๋œ๋‹ค.
<link rel="stylesheet" href="style.css">

ํด๋” ๊ตฌ์กฐ

profile
๊ธฐ๋ก์„ ์ •๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„!

0๊ฐœ์˜ ๋Œ“๊ธ€