For the complete documentation index, see llms.txt. This page is also available as Markdown.

CSS 로더

Style Loader + CSS Loader 구성

style-loader, css-loader 패키지를 프로젝트에 설치한 후, Webpack 구성 파일의 모듈 규칙 안에 로더를 설정합니다.

npm i style-loader css-loader -D
module.exports = {
  module: {
    rules: [
      // CSS 파일 로더 설정
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader']
      }
    ]
  }
}

Button 컴포넌트

간단한 Button 컴포넌트를 만들어 스타일링 하는 실습을 진행해봅니다. 컴포넌트 클래스, CSS 스타일 파일을 작성합니다.

import { createElement, render } from '../../modules/DOM'
import './style.css'

/* -------------------------------------------------------------------------- */

class Button {
  constructor(props) {
    this.props = { ...props }
  }

  render(domNode = document.body) {
    const buttonNode = createElement('button', this.props)
    render(buttonNode, domNode)
  }
}

export default Button
.button {
  cursor: pointer;
  border: 0;
  border-radius: 4px;
  padding: 0.6em 0.94em;
  background: #111;
  color: #fff;
}

엔트리 파일(src/index.js)에서 Button 컴포넌트 모듈을 불러온 후, Button 생성/렌더링 하도록 작성합니다.

모듈 번들링

bundle NPM 명령을 실행해 모듈을 번들링 합니다.

오류 없이 번들링이 완료되어도 배포 디렉토리(dist) 안에는 CSS 파일이 보이지 않습니다. 이유가 뭘까요?

웹 브라우저 개발 도구 Elements 패널을 확인해보면 이유를 알 수 있습니다. CSS 파일 로더는 기본적으로 HTML 문서의 <head> 영역에 인터널(internal) 스타일 방식으로 스타일 코드를 추가합니다.

HTML 문서에 포함시키지 않고 별도의 CSS 파일로 내보내길 원한다면 CSS 파일 추출을 참고하세요.

참고

Last updated