1. 디렉토리 생성
2.npm init -y ---> package.json 파일 생성
3. src/index.js. 파일생성
4.웹팩을 사용하기 위해서는 npm으로 webpack, webpack-cli를 설치
npm install -D webpack webpack-cli
5.웹팩 config 파일 작성
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'), // './dist'의 절대 경로를 리턴합니다.
filename: 'app.bundle.js',
},
};
6. 번들링하기
npx webpack
번외> npm run build 설정하기 npm run build 스크립트로 언제든지 번들링
{
"name": "fe-sprint-webpack-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack", // here
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"devDependencies": {
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0"
}
}
7. 로더설치하기(JavaScript, JSON이 아닌 파일을 불러오는 역할을 한다.)
npm i -D css-loader style-loader
// webpack.config.js
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "app.bundle.js",
},
module: {
rules: [
{
// 파일명이 .css로 끝나는 모든 파일에 적용
test: /\.css$/,
// 배열 마지막 요소부터 오른쪽에서 왼쪽 순으로 적용
// 먼저 css-loader가 적용되고, styled-loader가 적용되어야 한다.
// 순서 주의!
use: ["style-loader", "css-loader"],
// loader가 node_modules 안의 있는 내용도 처리하기 때문에
// node_modules는 제외해야 합니다
exclude: /node_modules/,
},
],
},
};
8.플러그인(html포함)
npm i -D html-webpack-plugin
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "app.bundle.js",
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
exclude: /node_modules/,
},
],
},
plugins: [new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src", "index.html")
})]
};
https://serzhul.io/JavaScript/learn-webpack-in-under-10minutes/
번역) 10분 만에 웹팩 배우기 (Learn webpack in under 10minutes)
출처 : https://betterprogramming.pub/learn-webpack-in-under-10-minutes-efe2b2b10b61 웹팩이란 무엇인가? “웹팩은 현대 자바스크립트 어플리케이션을 위한 정적 모듈 번들러 입니다.” 자바스크립트 개발자로서
serzhul.io