๐๐ปโ๏ธ
์๋ ํ์ธ์! ํ ์ผ๋ฌ์ ๋๋ค. ์ค๋์ webpack ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
https://webpack.js.org/concepts/ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ธฐ๋ณธ ๊ฐ๋ ๋ค์ ์ ๋ฆฌํด๋ณด์์ต๋๋ค.

๋ชจ๋ ๋ฒ๋ค๋ฌ webpack

webpack ์ ๋ชจ๋ JS application์ ์ํ static module bundler ์ ๋๋ค.
webpack์ application์ด ํ์๋กํ๋ ๋ชจ๋ ๋ชจ๋๋ค์ ๋ํด dependency graph๋ฅผ ๊ณ์ฐํด์ ์ด๋ฅผ ํตํด ํ๋ ์ด์์ ๋ฒ๋ค(bundle)๋ก ๋ง๋ค์ด๋ ๋๋ค.
์ด์ webpack.config.js
์ ํฌํจ๋๋ webpack์ ์ฃผ์ ๊ฐ๋
๋ค๊ณผ ๊ฐ๊ฐ์ ์ด๋ค ์ธํ
์ ํ๊ฒ๋๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
Entry
entry๋ webpack์ด dependency graph๋ฅผ ๋น๋ฉํ๊ธฐ ์ํ ์์์ (์ง์ ์ )์ ์ง์ ํฉ๋๋ค.
webpack์ ์ค์ ๋ entry point๋ฅผ ์์์ผ๋ก ์ง๊ฐ์ ์ ์ผ๋ก ์์กดํ๋ ๋ชจ๋, ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๊ณ์ฐํด์ dependency graph๋ฅผ ๋ง๋ญ๋๋ค.
- ๊ธฐ๋ณธ๊ฐ
./src/index.js
- ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ธํ
ํ ๊ฒฝ์ฐ
module.exports = { entry: './path/to/my/entry/file.js', };
Output
output ์ค์ ์ ํตํด webpack์ด ๋ง๋ค์ด๋ผ bundle์ ์ด๋์ ์์น์ํฌ๊ฑด์ง, ๊ทธ๋ฆฌ๊ณ ํด๋น ํ์ผ๋ช ๋ ์ ํ ์ ์์ต๋๋ค.
- ๊ธฐ๋ณธ๊ฐ
๋ฉ์ธ ์์ํ ํ์ผ →
./dist/main.js
๋ฉ์ธ ์์ํ ํด๋ →
./dist/
- ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ธํ
ํ ๊ฒฝ์ฐ
const path = require('path'); module.exports = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js', }, };
Loaders
๊ธฐ๋ณธ์ ์ผ๋ก webpack์ JavaScript, JSON ํ์ผ๋ง ์ดํดํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ค๋ฅธ ํ์์ ํ์ผ๋ค์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ ์์๊น์?
Loader ๋ฅผ ์ด์ฉํด์
- ๋ค๋ฅธ ํ์ผ ํ์๋ค๋ ์ฒ๋ฆฌํ์ฌ
- application์ด ์ฌ์ฉํ ์ ์๋ ๋ชจ๋๋ก ๋ฐ๊พธ๊ณ
- ์ด๋ฅผ dependency graph์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
loader์ ๊ด๋ จ๋ webpack ์ค์ ์ ๋๊ฐ์ง๊ฐ ์์ต๋๋ค.
test
: ์ด๋ค ํ์ผ๋ค์ด ๋ณํ๋์ด์ผ ํ๋์ง ๋ช ์
use
: ๋ณํ์ ์ํด ์ด๋ค loader๋ฅผ ์ฌ์ฉํด์ผ ํ๋์ง ๋ช ์
const path = require('path');
module.exports = {
output: {
filename: 'my-first-webpack.bundle.js',
},
module: {
rules: [{ test: /\.txt$/, use: 'raw-loader' }],
},
};
์ ์์๋ฅผ ๋ณด๋ฉด ํ๋์ ๋ชจ๋์ ๋ํด test
, use
์ด๋ฃจ์ด์ง rules
ํ๋กํผํฐ๋ฅผ ์ค์ ํฉ๋๋ค.
์ ์ค์ ์ ํตํด require()
, import
์์ .txt
ํ์ผ์ ์ฌ์ฉํ ๊ฒฝ์ฐ raw-loader
๋ฅผ ์ฌ์ฉํด์ ๋ณํํด์ bundle์ ํฌํจ์ํฌ ์ ์๊ฒ๋ฉ๋๋ค.
Plugins
loader๊ฐ ๋ชจ๋์ ๋ณํํ๋๋ฐ ์ฌ์ฉ๋๋ค๋ฉด,
plugin์ ํตํด webpack์ ๋ฅ๋ ฅ์ ํ์ฅํ์ฌ ์๋์ ๊ฐ์ ๋ค์ํ ์์ ์ ํ์ฉ ํ ์ ์์ต๋๋ค.
- bundle optimization (๋ฒ๋ค ์ต์ ํ)
- asset management
- injection of environment
plugin์ ์ฌ์ฉํ๋ ค๋ฉด require()
์ ํตํด ์ ์ธํ๊ณ , plugins
array์ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
๋๋ถ๋ถ์ plugin์ ์ต์
์ ํตํด ์ปค์คํฐ๋ง์ด์ฆ ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ๋์ผํ ํ๋ฌ๊ทธ์ธ์ ๋ค๋ฅธ ๋ชฉ์ ์ผ๋ก ์ฌ๋ฌ๊ฐ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์, new
operator๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํฉ๋๋ค.
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
module.exports = {
module: {
rules: [{ test: /\.txt$/, use: 'raw-loader' }],
},
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })],
};
์ ์์์ ๊ฒฝ์ฐ html-webpack-plugin
์ ์ฌ์ฉํ๋ฉด ๋ฒ๋ค๋ง๋ ํ์ผ๋ค์ ์ฃผ์
ํ HTML ํ์ผ์ ๋ง๋ค์ด๋ผ ์ ์์ต๋๋ค.
- ๋ค์ํ ํ๋ฌ๊ทธ์ธ ๋ฆฌ์คํธ https://webpack.js.org/plugins/
Mode
mode
ํ๋ผ๋ฏธํฐ๋ฅผ development
, production
, none
๊ณผ ๊ฐ์ ๊ฐ์ผ๋ก ์ธํ
ํ๋ฉด, ๊ฐ ํ๊ฒฝ์ ์ ํฉํ webpack ๋นํธ์ธ ์ต์ ํ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ๊ธฐ๋ณธ๊ฐ
production
- ๊ฐ mode ๋ณ๋ก ์ค์ ๋ ์ต์ ํ๋ฅผ ํ์ธํ๊ณ ์ถ๋ค๋ฉด mode-configuration ๋ฅผ ํ์ธํ์ธ์!
module.exports = {
mode: 'production',
};
Browser Comapatibility
์นํ์ ES5-compliant(IE8 ์ดํ ์ง์๋ถ๊ฐ)์ธ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ง์ํฉ๋๋ค. webpack์ import()
, require.ensure()
๋ฅผ ์ํด Promise
๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด์ ๋ธ๋ผ์ฐ์ ๋ ๋ชจ๋ ์ง์ํด์ผํ๋ค๋ฉด, polyfill์ ๋ก๋ํด์ผํฉ๋๋ค. (์ฐธ๊ณ load-a-polyfill)
Environment
webpack5๋ Node.js 10.13.0+๋ฅผ ์ฌ์ฉํฉ๋๋ค.