Skip to content

Instantly share code, notes, and snippets.

@yungblud
Last active April 23, 2023 01:10
Show Gist options
  • Select an option

  • Save yungblud/5b2d8a7ecf1752d4bc8f50c3ec91efc4 to your computer and use it in GitHub Desktop.

Select an option

Save yungblud/5b2d8a7ecf1752d4bc8f50c3ec91efc4 to your computer and use it in GitHub Desktop.
리액트 프로젝트를 웹팩으로 만들어보기.
  1. 프로젝트 생성 및 깃 저장소 master 푸시.
mkdir react-webpack
yarn init -y
git init

https://gitignore.io 로 .gitignore 생성 .gitignore


# Created by https://www.gitignore.io/api/node,react,macos,visualstudiocode
# Edit at https://www.gitignore.io/?templates=node,react,macos,visualstudiocode

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# react / gatsby 
public/

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

### react ###
.DS_*
**/*.backup.*
**/*.back.*

node_modules
bower_componets

*.sublime*

psd
thumb
sketch

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

# End of https://www.gitignore.io/api/node,react,macos,visualstudiocode
git remote add origin https://github.com/yungblud/react-webpack
git add .
git commit -m "init"
git push origin master
  1. 프로젝트 구조 설정 및 기초가 되는 node module 인스톨
  1. src 폴더 생성.
  2. public 폴더 생성.
  3. es6 문법을 사용하기 위한 노드 모듈 설치.
yarn add -D @babel/core @babel/preset-env @babel/preset-react
  1. 웹팩 관련 모듈 설치
yarn add -D webpack webpack-cli webpack-dev-server babel-loader css-loader style-loader html-webpack-plugin
  1. webpack.config.js 설정

webpack.config.js 파일 생성

webpack.config.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const htmlPlugin = new HtmlWebpackPlugin({
    template: './public/index.html',
    filename: './index.html',
})

const isDevelopMode = process.env.NODE_ENV === 'development'

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, '/build'),
        filename: 'index.bundle.js',
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                },
            },
            {
                test: /\.(css)$/,
                use: [
                    {
                        loader: 'style-loader',
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                localIdentName: isDevelopMode
                                    ? '[path][name]__[local]--[hash:base64:5]'
                                    : '[name]__[local]--[hash:base64:5]',
                            },
                            importLoaders: 1,
                            sourceMap: true,
                        },
                    },
                ],
            },
        ],
    },
    plugins: [htmlPlugin],
}

기본적인 리액트를 웹팩으로 띄울수 있는 설정만 하였다.

  1. public 폴더에 index.html 생성

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>React PWA Webpack</title>
    </head>
    <body>
        <div id="root"></div>
    </body>
</html>

id 를 root로 하는 태그생성.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment