Webpack
Webpack
A module bundler. Packages JS, CSS, and images. The frontend build standard.
Webpack
A module bundler. Packages JS, CSS, and images. The frontend build standard.
Webpack is a static module bundler for JavaScript applications. Starting from an entry point, it builds a dependency graph and packages all resources, including JavaScript, CSS, and images, into one or more bundles. Since its emergence in 2012, it has been the de facto standard for front-end builds.
The core concepts of Webpack are Entry (the entry point), Output, Loaders (file transformation), and Plugins (bundle optimization). Loaders convert files such as TypeScript and Sass into JavaScript, and Plugins perform optimizations such as Tree Shaking, code splitting, and minification.
Webpack's strengths are powerful code splitting and a mature ecosystem. It implements lazy loading with dynamic import() and separates common code with SplitChunksPlugin. With Hot Module Replacement (HMR), code can be updated during development while preserving state.
Although faster bundlers such as Vite and Turbopack have recently emerged, Webpack is still well suited to large-scale projects and complex build requirements. Many frameworks, including Next.js and Create React App, use Webpack internally.
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: 'production', // 'development' | 'production'
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
clean: true // 빌드 전 dist 폴더 정리
},
module: {
rules: [
// JavaScript/TypeScript
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: 'babel-loader'
},
// CSS/SCSS
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
// 이미지
{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource'
}
]
},
plugins: [
new HtmlWebpackPlugin({ template: './public/index.html' }),
new MiniCssExtractPlugin({ filename: '[name].[contenthash].css' })
],
optimization: {
splitChunks: {
chunks: 'all', // 코드 스플리팅
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
devServer: {
port: 3000,
hot: true // HMR 활성화
}
};
"The bundle size exceeds 2MB. When I analyzed it with webpack-bundle-analyzer, all of lodash was included. I think we can switch to lodash-es or apply Tree Shaking with babel-plugin-lodash, and reduce the initial load with code splitting."
"Webpack has four core concepts: Entry, Output, Loaders, and Plugins. Loaders convert non-JavaScript files into modules, and Plugins handle bundle optimization and environment variable injection, among other things. Code splitting is implemented with SplitChunksPlugin and dynamic import."
"If the dev server is slow, try changing source-map to eval-cheap-module-source-map. For production, hidden-source-map is good. Turning on build caching with cache: true cuts rebuild time significantly."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
When the Webpack configuration grows to thousands of lines, it becomes hard to maintain. Split the configuration by environment with webpack-merge, and document it.
Putting all the code into a single bundle makes the initial load slow. Split by route with code splitting, and lazy-load heavy libraries.
Take advantage of automatic optimization in production mode, turn on cache, and periodically analyze the bundle with webpack-bundle-analyzer. For new projects, also consider adopting Vite.