Webpack 러닝 가이드
  • Webpack 모듈 번들러
  • Web Modules
    • Legacy 모듈 관리
    • ES 모듈 관리
  • Babel
    • Babel JavaScript 컴파일러
    • Babel 독립형 빌드
    • Babel 노드 (Node.js) ✘
    • Babel CLI 구성
    • Babel 플러그인
  • webpack
    • Webpack 개발 환경 구성
      • Webpack 설치
      • Webpack 통합
      • Webpack 구성 파일
      • Webpack 워치
      • Webpack 모드
      • Webpack 별칭 등록
      • Webpack 호환성
      • Webpack 개발 서버
      • Webpack 멀티 페이지 설정
    • Webpack 로더
      • File 로더
      • CSS 로더
      • PostCSS 로더
      • Sass 로더
      • Babel 로더
      • TypeScript 로더
    • Webpack 플러그인
      • 환경 변수 등록
      • 빌드 결과 자동 정리
      • 빌드 결과 자동 주입
      • CSS 파일 개별 추출
      • CSS 파일 크기 최적화
      • CSS 미디어쿼리 스플리팅
      • 이미지 파일 크기 최적화
  • React
    • React App 매뉴얼 구성
      • Webpack 초기 구성
      • Babel 컴파일러 구성
      • CSS 스타일 구성
      • 이미지 구성
      • 환경변수 플러그인
      • HTML 플러그인
      • 최적화 구성
      • 코드 스플리팅
      • 개발 서버
      • 이미지, 폰트 에셋 구성
      • 환경 변수 + HTML 구성
      • 빌드 최적화 구성
      • 빌드 자동 정리 구성
      • 개발 서버 구성
      • 폴리필 구성
Powered by GitBook
On this page
  • 1. Babel, babel-loader 설치
  • 2. babel-loader 구성
  • 3. Babel 프리셋
  • 4. Babel 플러그인
  • 5. React 최적화 플러그인 (배포)

Was this helpful?

  1. React
  2. React App 매뉴얼 구성

Babel 컴파일러 구성

PreviousWebpack 초기 구성NextCSS 스타일 구성

Last updated 4 years ago

Was this helpful?

1. Babel, babel-loader 설치

, , 패키지를 프로젝트에 설치합니다.

npm i babel-loader @babel/core -D

2. babel-loader 구성

resolve, module 구성을 추가한 후, Babel 컴파일러 로더(loader)를 구성합니다.

const getAbsolutePath = (pathDir) => path.resolve(__dirname, pathDir)

// ...

module.exports = (_env, argv) => {
  // ...
  return {
    // ...
    resolve: {
      extensions: ['js', 'jsx', 'json'],
      alias: {
        '@components': getAbsolutePath('src/components/'),
        '@contexts': getAbsolutePath('src/contexts/'),
        '@hooks': getAbsolutePath('src/hooks/'),
        '@pages': getAbsolutePath('src/pages/'),
      },
    },
    module: {
      rules: [
        {
          test: /\.jsx?$/i,
          exclude: /node_modules/,
          use: [
            {
              loader: 'babel-loader',
              options: {
                cacheDirectory: true,
                cacheCompression: false,
                envName: isProd ? 'production' : 'development'
              }
            }
          ]
        }
      ]
    }
  }
}

불러올 모듈 확장자를 생략할 경우 "resolve.extensions"에 추가합니다. 그리고 별칭은 "resolve.alias"에 추가합니다. (components, contexts, hooks, pages 등)

3. Babel 프리셋

npm i @babel/preset-env @babel/preset-react -D
module.exports = {
  presets: [
    [
      '@babel/preset-env',
      { modules: false }
    ],
    '@babel/preset-react'
  ]
}

4. Babel 플러그인

npm i @babel/plugin-transform-runtime \
      @babel/plugin-syntax-dynamic-import \
      @babel/plugin-proposal-class-properties -D
module.exports = {
  presets: [
    [
      '@babel/preset-env',
      { module: false }
    ],
    '@babel/preset-react'
  ],
  plugins: [
    '@babel/plugin-transform-runtime',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties',
  ]
}

5. React 최적화 플러그인 (배포)

npm i babel-plugin-transform-react-remove-prop-types \
      @babel/plugin-transform-react-inline-elements \
      @babel/plugin-transform-react-constant-elements -D
module.exports = {
  presets: [
    [
      '@babel/preset-env',
      { module: false }
    ],
    '@babel/preset-react'
  ],
  plugins: [
    '@babel/plugin-transform-runtime',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties',
  ],
  env: {
    production: {
      only: ["src"],
      plugins: [
        [
          "transform-react-remove-prop-types",
          { removeImport: true } // import PropTypes from 'prop-types' 제거
        ],
        "@babel/plugin-transform-react-inline-elements",
        "@babel/plugin-transform-react-constant-elements"
      ]
    }
  }
}

, 프리셋을 설치한 후, babel.config.js 파일을 만들어 구성합니다.

React 앱에서 , 등을 설치합니다.

React 앱 배포(production) 시 성능을 향상 시키는 , , 등을 설치합니다.

@babel/core
@babel/preset-env
babel-loader
@babel/preset-env
@babel/preset-react
"클래스 필드" 문법을 사용하기 위한 플러그인
"동적 가져오기"를 사용하기 위한 플러그인
prop-types 제거
React.createElement 컴파일 결과 인라인 처리
정적 React 요소를 상수로 출력하는 플러그인