[μΉν©][λ°λ²¨] ν¨ν€μ§ μ΄ μ€μΉ

λ²λ€λ§ : λͺ¨λ νμΌλ€μ νλνλμ λͺ¨λλ‘ λ³΄κ³ , μ΄ λͺ¨λλ€μ λ°°ν¬μ©μΌλ‘ λ³ν©νκ³ ν¬μ₯νλ μμ .(μ΅μ νλ ν΄μ€)
μ΄λ¬ν λ²λ€λ§ μμ μ μννλ ν΄μ λ²λ€λ¬λΌκ³ νλ€.
νμ¬ μμ μμλ λ²λ€λ¬ μ€μμ 'μΉν©'μ΄ κ°μ₯ μΈκΈ°κ° λ§λ€.
μ²μ 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,
}
}]
]
};