webpack은 여러 파일을 하나의 파일로 합쳐주는 번들러이다.
https://joshua1988.github.io/webpack-guide/motivation/why-webpack.html
님이 정리하신 글을 읽어보면 왜 webpack이 필요한지 알 수 있다.
내가 이해한 webpack의 필요성은 다음과 같다.
npm init
npm i react react-dom
npm i -D webpack webpack-cli // -D 옵션은 개발용
const path = require("path");
module.exports = {
entry: {
app: ["./client.jsx"],
},
output: {
path: path.join(__dirname, "dist"),
filename: "app.js"
},
};
entry : 합칠 대상이 되는 파일의 정보를 작성한다.
output : 하나로 합친 파일이 위치할 path, 이름 등의 정보를 작성한다.
npm i -D @babel/core @babel/preset-env @babel/preset-react babel-loader
//jsx 파싱하려면 preset-react가 필요
const path = require("path");
module.exports = {
entry: {
app: ["./client.jsx"],
},
module: {
rules: [
{
test: /\.jsx?/, //<-정규식문법
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
output: {
path: path.join(__dirname, "dist"),
filename: "app.js"
},
};
entry에 있는 파일들을 변환할 때 module에 정의한 rule을 기반으로 변환한다.
rule의 options에 정의하는 presets은 plugin의 모음이다.
npm i -D react-refresh @pmmmwh/react-refresh-webpack-plugin webpack-dev-server
수정중..