나에게 맞는 webpack 세팅하기
npm init -y
예시 코드
npm i webpack webpack-cli -D // 설치
webpack.config.js -->
// Node.js 모듈 path 불러오기
const path = require('path')
// CommonJS 방식의 모듈 내보내기
module.exports = {
// 엔트리 파일 설정
entry: './src/index.js',
// 아웃풋 파일 출력 설정
output: {
// 파일 이름
filename: 'main.js',
// 경로
path: path.resolve(__dirname, './dist'),
},
// 번들링 모드 설정
mode: 'development',
}
"scripts": {
"bundle": "webpack",
"start": "webpack-dev-server --config webpack.config.js"
},
npm i -D @babel/core @babel/cli
npm i @babel/core @babel/preset-env babel-loader -D
npm i -D @babel/plugin-proposal-class-properties
npm install -D @babel/plugin-transform-runtime
npm install --save-dev html-webpack-plugin
npm install --save-dev clean-webpack-plugin
npm i webpack-dev-server
webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./public/js/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
clean: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/html/index.html",
}),
],
devServer: {
static: "./dist",
port: 9000,
open: true,
},
devtool: "source-map",
mode: "development",
watch: true,
};