mkdir react-webpack
yarn init -y
git init
.gitignore
build/
node_modules/
.DS_Store
yarn.lock
git remote add origin https://github.com/yungblud/react-webpack
git add .
git commit -m "init"
git push origin master
yarn add -D @babel/core @babel/preset-env @babel/preset-react
yarn add -D webpack webpack-cli webpack-dev-server babel-loader css-loader style-loader html-webpack-plugin
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 config = mode => {
const isDevelopMode = mode === 'development'
return {
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],
}
}
module.exports = (env, argv) => {
const { mode } = argv
return config(mode)
}
기본적인 리액트를 웹팩으로 띄울수 있는 설정만 하였다.
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로 하는 태그생성.
.babelrc 파일 생성
.babelrc
{
"presets": ["@babel/env", "@babel/react"]
}
yarn add react react-dom
src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
const Root = () => <div>Hello World!</div>
ReactDOM.render(<Root />, document.getElementById('root'))
package.json
"scripts": {
"build:prod": "webpack --mode=production",
"build:dev": "webpack --mode=development"
},
이제 yarn build:prod 혹은 yarn build:dev 를 하면 build 폴더가 생성되면서, 번들링이 잘 된것을 볼수있다.
package.json
"scripts": {
"start:dev": "webpack-dev-server --mode=development --open --hot",
...
}
start:dev 를 추가.
yarn start:dev 를 해보면 개발용 서버가 8080포트에 띄워진다.