Javascript/FE κ°œλ°œν™˜κ²½μ„€μ •

[μ›ΉνŒ©][바벨] νŒ¨ν‚€μ§€ 총 μ„€μΉ˜

Rainbow🌈Coder 2022. 5. 10. 02:50
728x90

 

λ²ˆλ“€λ§ : λͺ¨λ“  νŒŒμΌλ“€μ„ ν•˜λ‚˜ν•˜λ‚˜μ˜ λͺ¨λ“ˆλ‘œ 보고, 이 λͺ¨λ“ˆλ“€μ„ 배포용으둜 λ³‘ν•©ν•˜κ³  포μž₯ν•˜λŠ” μž‘μ—….(μ΅œμ ν™”λ„ ν•΄μ€Œ)

 

μ΄λŸ¬ν•œ λ²ˆλ“€λ§ μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” νˆ΄μ„ λ²ˆλ“€λŸ¬λΌκ³  ν•œλ‹€.

 

ν˜„μž¬ μ‹œμ μ—μ„œλŠ” λ²ˆλ“€λŸ¬ μ€‘μ—μ„œ 'μ›ΉνŒ©'이 κ°€μž₯ 인기가 λ§Žλ‹€.

 

처음 package.json μƒνƒœ

{
  "name": "webpack-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --progress",
    "start": "webpack serve --progress"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");

const webpackMode = process.env.NODE_ENV || 'development';

module.exports = {
	mode: webpackMode,
	entry: {
		main: './src/main.js',
	},
	output: {
		path: path.resolve('./dist'),
		filename: '[name].min.js'
	},
	// es5둜 λΉŒλ“œ ν•΄μ•Ό ν•  경우 주석 제거
	// 단, 이거 μ„€μ •ν•˜λ©΄ webpack-dev-server 3λ²ˆλŒ€ λ²„μ „μ—μ„œ live reloading λ™μž‘ μ•ˆν•¨
	// target: ['web', 'es5'],
	devServer: {
		liveReload: true
	},
	optimization: {
		minimizer: webpackMode === 'production' ? [
			new TerserPlugin({
				terserOptions: {
					compress: {
						drop_console: true
					}
				}
			})
		] : [],
		splitChunks: {
			chunks: 'all'
		}
	},
	module: {
		rules: [
			{
				test: /\.js$/,
				loader: 'babel-loader',
				exclude: /node_modules/
			},
			{
				test: /\.js$/,
				enforce: 'pre',
				use: ['source-map-loader'],
			}
		]
	},
	plugins: [
		new HtmlWebpackPlugin({
			template: './src/index.html',
			minify: process.env.NODE_ENV === 'production' ? {
				collapseWhitespace: true,
				removeComments: true,
			} : false
		}),
		new CleanWebpackPlugin(),
		// CopyWebpackPlugin: κ·ΈλŒ€λ‘œ 볡사할 νŒŒμΌλ“€μ„ μ„€μ •ν•˜λŠ” ν”ŒλŸ¬κ·ΈμΈ
		// μ•„λž˜ patterns에 μ„€μ •ν•œ 파일/ν΄λ”λŠ” λΉŒλ“œ μ‹œ dist 폴더에 μžλ™ 생성
		// patterns에 μ„€μ •ν•œ κ²½λ‘œμ— ν•΄λ‹Ή 파일이 μ—†μœΌλ©΄ μ—λŸ¬ λ°œμƒ
		// μ‚¬μš©ν•˜λŠ” νŒŒμΌμ΄λ‚˜ 폴더 이름이 λ‹€λ₯΄λ‹€λ©΄ λ³€κ²½
		// κ·ΈλŒ€λ‘œ μ‚¬μš©ν•  νŒŒμΌλ“€μ΄ μ—†λ‹€λ©΄ CopyWebpackPlugin 톡째 주석 처리
		new CopyWebpackPlugin({ //μ—¬κΈ°μ—λŠ” dist 폴더에 κ·ΈλŒ€λ‘œ λ³΅μ œμ‹œν‚¬ νŒŒμΌλ“€μ„ μž‘μ„±
			patterns: [
				{ from: "./src/style.css", to: "./style.css" },
				{ from: "./src/images", to: "./images" },
			],
		})
	]
};

 

 

<1> μ΄λ ‡κ²Œ ν•  μˆ˜λ„ μžˆμ§€λ§Œ μ•„λž˜ <2>λ₯Ό 터미널에 적용

JS만 κ°„λ‹¨νžˆ λΉŒλ“œν•˜λŠ” μ›ΉνŒ© μ„ΈνŒ…

1. νŒ¨ν‚€μ§€ μ„€μΉ˜
npm i -D @babel/cli @babel/core @babel/preset-env babel-loader clean-webpack-plugin core-js cross-env terser-webpack-plugin webpack webpack-cli


2. λΉŒλ“œ(배포용 파일 생성) : 개발 ν›„ μ΅œμ’… μ‹€μ œλ‘œ μ„œλΉ„μŠ€ν•  배포용 파일. 사싀상 λ²ˆλ“€λ§, μ΅œμ’… νŒŒμΌμ„ λ§Œλ“œλŠ” μž‘μ—…
npm run build

(!)
npm run build μ‹€ν–‰ μ‹œ μ—λŸ¬κ°€ λ‚œλ‹€λ©΄ Node.jsλ₯Ό LTS 버전(μž₯κΈ° 지원 버전)으둜 μ„€μΉ˜ ν›„ λ‹€μ‹œ μ‹œλ„
n lts

 

<2> 적용 ν›„ κ²°κ³ΌλŠ”

 

1. νŒ¨ν‚€μ§€ μ„€μΉ˜ : -D λŠ” κ°œλ°œμ‹œμ—λ§Œ μ“΄λ‹€λŠ” 것을 λͺ…μ‹œ
npm i -D @babel/cli @babel/core @babel/preset-env babel-loader clean-webpack-plugin copy-webpack-plugin core-js cross-env html-webpack-plugin source-map-loader terser-webpack-plugin webpack webpack-cli webpack-dev-server


2. 개발용 μ„œλ²„ ꡬ동
npm start


3. λΉŒλ“œ(배포용 파일 생성)
npm run build


(!)
npm start λ˜λŠ” npm run build μ‹€ν–‰ μ‹œ μ—λŸ¬κ°€ λ‚œλ‹€λ©΄ Node.jsλ₯Ό LTS 버전(μž₯κΈ° 지원 버전)으둜 μ„€μΉ˜ ν›„ λ‹€μ‹œ μ‹œλ„
n lts


(!)
ERROR in unable to locate '경둜...'
μœ„μ™€ 같은 μ—λŸ¬κ°€ λ°œμƒν•œλ‹€λ©΄, webpack.config.js의 CopyWebpackPlugin에 μ„€μ •λœ 파일이 μžˆλŠ”μ§€ 확인
CSSλ‚˜ 이미지 폴더 등이 ν•„μš”ν•˜μ§€ μ•Šλ‹€λ©΄ webpack.config.jsμ—μ„œ CopyWebpackPlugin λΆ€λΆ„ 전체λ₯Ό 주석 처리

 

<2>μ—μ„œ 1 터미널 μž…λ ₯ ν›„ package.json μƒνƒœ

{
  "name": "webpack-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --progress",
    "start": "webpack serve --progress"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.17.10",
    "@babel/core": "^7.17.10",
    "@babel/preset-env": "^7.17.10",
    "babel-loader": "^8.2.5",
    "clean-webpack-plugin": "^4.0.0",
    "copy-webpack-plugin": "^10.2.4",
    "core-js": "^3.22.4",
    "cross-env": "^7.0.3",
    "html-webpack-plugin": "^5.5.0",
    "source-map-loader": "^3.0.1",
    "terser-webpack-plugin": "^5.3.1",
    "webpack": "^5.72.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.9.0"
  }
}

ν˜‘μ—…μ‹œ, μœ„ νŒ¨ν‚€μ§€ 파일만 μžˆλ‹€λ©΄

npm iλ₯Ό μž…λ ₯ν•΄μ£Όλ©΄, ν˜‘μ—… μƒλŒ€λ°©λ„ νŒ¨ν‚€μ§€λ“€μ„ μžλ™ μ„€μΉ˜λ°›μ•„ λ³Ό 수 μžˆλ‹€.

 

μ›ΉνŒ©μ€ κ°œλ°œν•  λ•Œλ§Œ μ“°λŠ” 것이기 λ•Œλ¬Έμ— -D μ˜΅μ…˜μ„ 넣어주도둝 ν•˜μž

 

 

<2>2. 개발용 μ„œλ²„ ꡬ동 : μ’…λ£ŒλŠ” 컨트둀 c
npm start : μ„±κ³΅μ μœΌλ‘œ ꡬ동이 되면 터미널에 μ£Όμ†Œκ°’μ΄ λ‚˜μ˜¨λ‹€. ν•΄λ‹Ή μ£Όμ†Œμ— λ“€μ–΄κ°€λ©΄ 웹을 λ³Ό 수 μžˆλ‹€.
톡상 src 폴더에 ν”„λ‘œμ νŠΈ νŒŒμΌλ“€μ„ λ‹΄μ•„ λ‘”λ‹€.

<3>3. λΉŒλ“œ(배포용 파일 생성)
npm run build

dist λΌλŠ” μƒˆλ‘œμš΄ 폴더가 μƒμ„±λ˜κ³ , μ΅œμ’… 배포용 νŒŒμΌλ“€μ„ 확인할 수 μžˆλ‹€(λ‹€ μ΅œμ ν™”, μ••μΆ•λœ  μƒνƒœ)

 

 

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");

const webpackMode = process.env.NODE_ENV || 'development';

module.exports = {
mode: webpackMode,
entry: {
main: './src/main.js', //μžλ°”μŠ€ν¬λ¦½νŠΈ λͺ¨λ“ˆμ„ λΉŒλ“œν•  λ•Œ, μ‹œμž‘μ μ΄ λ˜λŠ” 파일(μ—”νŠΈλ¦¬ κΈ°μ€€ 파일)
},
output: { //λΉŒλ“œν–ˆμ„ λ•Œ dist 파일이 μžλ™ μƒμ„±λ˜λ©΄μ„œ κ·Έ μ•ˆμ— λ²ˆλ“€λ§λœ νŒŒμΌλ“€μ΄ 생겨남
path: path.resolve('./dist'),
filename: '[name].min.js' //파일 μ΄λ¦„κΉŒμ§€ μ§€μ • κ°€λŠ₯
},
// es5둜 λΉŒλ“œ ν•΄μ•Ό ν•  κ²½μš° μ£Όμ„ μ œκ±°
// λ‹¨, μ΄κ±° μ„€μ •ν•˜λ©΄ webpack-dev-server 3λ²ˆλŒ€ λ²„μ „μ—μ„œ live reloading λ™μž‘ μ•ˆν•¨
// target: ['web', 'es5'],
devServer: {
liveReload: true //μžλ™ μƒˆλ‘œκ³ μΉ¨ μ„€μ •
},
optimization: {
minimizer: webpackMode === 'production' ? [
new TerserPlugin({ //λΉŒλ“œν•  λ•Œ μ½”λ“œ μ••μΆ•λ˜λ„λ‘
terserOptions: {
compress: {
drop_console: true //찍어놓은 μ½˜μ†”λ‘œκ·Έ μ§€μš°κΈ°
}
}
})
] : [],
splitChunks: {
chunks: 'all'
}
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.js$/,
enforce: 'pre',
use: ['source-map-loader'],
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
minify: process.env.NODE_ENV === 'production' ? {
collapseWhitespace: true,
removeComments: true,
} : false
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: "./src/main.css", to: "./main.css" },
{ from: "./src/images", to: "./images" },
],
})
]
};

<μ›ΉνŒ©μœΌλ‘œ μžλ°”μŠ€ν¬λ¦½νŠΈ λͺ¨λ“ˆ λ²ˆλ“€λ§ν•΄λ³΄κΈ°>

babel.config.js : 바벨은 μ΅œμ‹  λ¬Έλ²•μœΌλ‘œ μž‘μ„±λœ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 버전을 λ‹€μš΄κ·Έλ ˆμ΄λ“œ μ‹œμΌœμ£ΌλŠ” 역할을 ν•œλ‹€.

μ˜ˆμ „ λΈŒλΌμš°μ €μ—λ„ 잘 λŒμ•„κ°€κ²Œλ” ν•˜κΈ° μœ„ν•¨ ( 트랜슀파일러)

특히 바벨은 μ›ΉνŒ©κ³Όμ˜ 쑰합이 쒋기에 보톡 μ›ΉνŒ©μ„ μ“°λ©΄ μ›ΉνŒ©μ— 바벨을 λ‘œλ”λ‘œ λΆˆλŸ¬μ™€μ„œ 같이 μ‹€ν–‰ν•΄μ€€λ‹€.

module.exports = {
	presets: [
		['@babel/preset-env', {
			targets: {
				chrome: '58',
				ie: '11',
			},
			useBuiltIns: 'usage',
			corejs: {
				version: 3,
			}
		}]
	]
};

 

728x90