2022.07.25(Tues)
[TIL] Day63
[SEB FE] Day64
Install
# webpack ์ค์น
$ npm install -D webpack webpack-cli
# babel ์ค์น
$ npm install --save-dev @babel/core babel-loader @babel/preset-react @babel/preset-env
# web-dev-server, ๊ฐ๋ฐ์ฉ ์๋ฒ ์ค์น
$ npm install --save-dev webpack-dev-server
# html-webpack-plugin
$ npm install --save-dev html-webpack-plugin
# css-loader, style-loader ์ค์น
$ npm i -D css-loader style-loader
webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
module.exports = {
mode: "development", // ์๋ ๋ชจ๋๋ฅผ ๊ฐ๋ฐ์๋ชจ๋๋ก ์ค์
entry: "./src/index.js",
devServer: {
static: "./dist",
},
output: {
path: path.join(__dirname, "/dist"),
filename: "[name].js",
clean: true, // ๋ฒ๋ค๋งํ ๋ ๋ง๋ค dist ๋๋ ํฐ๋ฆฌ ์ ๋ฆฌ
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /.s?css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
exclude: /node_modules/,
},
],
},
// Asset ๊ด๋ฆฌ
optimization: {
minimizer: [new CssMinimizerPlugin()],
runtimeChunk: "single",
},
// html-webpack-plugin
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "./public/index.html"),
}),
new MiniCssExtractPlugin(),
],
};
.babelrc
ํ์ผ ์ถ๊ฐ
{
"presets": [
"@babel/env",
"@babel/react"
]
}
package.json
โscriptsโ
๋ณ๊ฒฝ
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
},
import React from "react";
โจย React undefined ...
์๋ฌ๊ฐ ๋ฐ์ํ๋ฏ๋ก App.js
, components ๋ด์ js ํ์ผ์ ์๋ ์ฝ๋ ์ถ๊ฐํด์ค์ผ ํจ!
import React from "react";
์ค๋์ ์ด์ github repository์ ์ง์ docs ํด๋๋ง ์ฌ๋ฆฌ๋ ๋ฐฉ์์ด ์๋๋ผ ํ์ด๋์ด ์๋ ค์ฃผ์ ๋ฐฉ์๋๋ก ๊นํ ํ์ด์ง ๋ฐฐํฌ๋ฅผ ํด๋ณด์๋ค!
$ npm install gh-pages --save-dev
// package.json
// scripts ๋ถ๋ถ์ predeploy, deploy ์ฝ๋ ์ถ๊ฐ
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
},
// homepage์ github repository ์ฃผ์ ์ถ๊ฐ
"homepage": "https://beanxx.github.io/fe-sprint-react-webpack/",
# ์๋ ์ฝ๋๋ก deloy script๋ฅผ ์คํ
$ npm run deploy