버전

  • pnpm 10.6.4
  • node v22.14.0

 

폴더구조

project/
├── public/
│   ├── index.html
├── src/
│   ├── index.js
│   ├── App.js
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .prettierrc.js
├── babel.config.json
├── package.json
├── pnpm-lock.yaml
├── webpack.config.js

 

프로젝트 초기화

pnpm init

 

React 및 ReactDOM 설치

pnpm add react react-dom

 

webpack 등 필요한 패키지 설치

pnpm add -D webpack webpack-cli webpack-dev-server babel-loader @babel/core @babel/preset-env @babel/preset-react html-webpack-plugin clean-webpack-plugin
패키지 설명
webpack Webpack 자체
webpack-cli 명령 줄 인터페이스(CLI, Command line interface) 명령어 사용 가능하게 함
webpack-dev-server 개발 서버 실행
babel-loader Babel을 Webpack에서 사용 가능하게 함
@babel/core Babel 핵심 기능
@babel/preset-env 최신 JavaScript 문법을 변환
@babel/preset-react JSX 문법을 변환
html-webpack-plugin HTML 파일을 자동으로 생성 및 연결
clean-webpack-plugin 빌드할 때 dist 폴더를 자동으로 정리

 

Webpack 설정

webpack.config.js 생성

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.jsx',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html',
    }),
    new CleanWebpackPlugin(),
  ],
  devServer: {
    static: path.resolve(__dirname, 'dist'),
    compress: true,
    port: 3000,
    hot: true,
    historyApiFallback: true,
  },
};

 

babel.config.js 생성

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-react'],
};

 

 

파일 생성

1. ./public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

 

2. src/index.jsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from "./App";

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

 

3. src/app.jsx

import React from 'react';

const App = () => {
  return <h1>Hello, React without CRA or Vite!</h1>;
};

export default App;

 

실행 스크립트 수정

// ./package.json

{
  // 생략
  "scripts": {
    "start": "webpack serve --config webpack.config.js"
  }
  // 생략
}

 

ESLint & Prettier 설정

패키지 설치

pnpm add -D eslint eslint-plugin-react eslint-plugin-react-hooks prettier eslint-config-prettier
패키지 설명
eslint 코드 품질을 검사하는 린터(Linter)
eslint-plugin-react React 관련 린트 규칙 추가 (.jsx, .tsx 지원)
eslint-plugin-react-hooks React Hooks 관련 린트 규칙 추가 (useEffect, useState 등)
prettier 코드 스타일을 자동으로 정리하는 포맷터(Formatter)
eslint-config-prettier Prettier와 ESLint 규칙 충돌 방지 (eslint --fix 사용 시 Prettier 스타일 유지)

 

.eslintrc.js 생성

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.jsx',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
    }),
    new CleanWebpackPlugin(),
  ],
  devServer: {
    static: path.resolve(__dirname, 'dist'),
    compress: true,
    port: 3000,
    hot: true,
    historyApiFallback: true,
  },
};

 

.prettierrc.js 생성

module.exports = {
  semi: true,
  trailingComma: 'all',
  printWidth: 100,
  tabWidth: 2,
  singleQuote: true,
  bracketSpacing: true,
  arrowParens: 'always',
  endOfLine: 'auto',
};

 

 

기본 설정은 끝났다.

 

실행해보면 된다!

pnpm start

실행 확인

 

 

겸사겸사 세팅한 내용이라 tag로 남겼다.

끝!

+ Recent posts