'프론트엔드 개발자' 에게 번들은?
-> "사용자 에게 웹애플리케이션을 제공하기 위한 파일 묶음"
모듈 번들러
HTML , CSS< Javascript 등의 자원을 전부 각각의 모듈로 보고 이를 조합해 하나의 묶음으로 번들링(빌드)하는 도구
webpack.config.js
예시 파일
module.exports = {
target: ["web", "es5"],
entry: "./src/script.js",
output: {
path: path.resolve(__dirname, "docs"), // './dist' 의 절대 경로를 리턴
filename: "app.bundle.js",
clean: true
},
module: {
rules: [
{
test: /\.css$/, // 변환이 필요한 파일들을 식별하기 위한 속성 , CSS 파일명으로 끝나는 모든 파일에 적용
use: [MiniCssExtractPlugin.loader, "css-loader"],
// use : 변환을 수행하는데 사용되는 로더를 가리키는 속성
exclude: /node_modules/, // 바벨로 제외할 파일이나 폴더 지정.
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src", "index.html"),
}),
new MiniCssExtractPlugin(),
],
optimization: {
minimizer: [
new CssMinimizerPlugin(),
]
}
};
폴더 생성
npm init -y // npm을 활용할 준비
src/index.js 파일 생성
src/underbar.js 파일 생성
웹팩 설치하기
npm install -D webpack webpack-cli
웹팩 config 파일 작성
폴더 내부에 webpack.config.js 파일 생성후 작성
번들링 하기
npx webpack
스트립트에 "build": "webpack" 을 추가하여
npm run build로 활용
index파일을 dist디렉토리로 옮기기 (브라우저에서는 node명령 X)
src/style.css파일 작성 (에러발생)
두 로더 설치
npm i -D css-loader style-loader
webpack.config.js파일을 조정
loader 작성
npm run build를 이용해 적용되었는지 확인
HTML도 번들로 포함하는 작업
index.html을 src로 옮기기
html-webpack-plugin을 설치
npm i -D html-webpack-plugin
webpack.config.js에 플러그인을 적용
npm run build를 통해 결과를 확인
html 파일을 번들링되어 새로운 dist/index.html
생성