In a world obsessed with fast, zero-config tools like Vite and Turbopack, you might
think Webpack is outdated. But hold up โ if you're building enterprise-scale apps, complex micro-frontends, or
require laser-sharp control over your build pipeline, Webpack still slaps.
Webpack isn't going anywhere โ it's stable, battle-tested, and customizable to the bone. Let's break down Webpack in its full glory: how it works, how to set it up from scratch, and how to wield its power like a true frontend engineer.
Webpack is a static module bundler for modern JavaScript applications. It bundles JS, CSS, images, fonts, and even Markdown into optimized chunks that can be shipped to the browser efficiently.
webpack.config.js# Create a new app folder
mkdir webpack-app && cd webpack-app
# Initialize project
npm init -y
# Install core packages
npm install webpack webpack-cli webpack-dev-server --save-dev
# Add Babel for transpilation
npm install babel-loader @babel/core @babel/preset-env --save-dev
webpack-app/
โโโ dist/
โ โโโ index.html
โโโ src/
โ โโโ index.js
โโโ webpack.config.js
โโโ package.json
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true, // clean old builds
},
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
}
]
},
devServer: {
static: './dist',
port: 3000,
hot: true,
},
};
// src/index.js
import './style.css';
const app = document.createElement('div');
app.innerHTML = '<h1>Hello Webpack ๐</h1>';
document.body.appendChild(app);
<!-- dist/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Webpack App</title>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>
/* src/style.css */
body {
background: #121212;
color: white;
font-family: sans-serif;
}
Just import the CSS file in your index.js and Webpack will inject it at runtime. โ
Change mode to production in config:
mode: 'production'
This enables:
clean-webpack-plugin - auto-clean dist/html-webpack-plugin - injects your JS in HTMLdotenv-webpack - env variable supportfile-loader, url-loader - for assetswebpack-bundle-analyzer - inspect your bundleoutput: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
clean: true,
},
optimization: {
splitChunks: {
chunks: 'all',
},
}
That config ensures vendor code (like React) is split out and cached efficiently. ๐
| Feature | Webpack | Vite | Turbopack |
|---|---|---|---|
| Startup Time | ๐ข Slow | โก Fast | ๐ Instant |
| Customization | ๐ช Extreme | Moderate | Limited (Beta) |
| Ecosystem | ๐ Massive | Growing | New |
webpack-merge for separate dev/prod configssource-map only in devnode_modules in CI/CD for faster buildsWebpack isn't "old" โ it's mature. And it's still the only bundler that lets you twist every knob in the engine room. If you're building at scale, you owe it to yourself to understand Webpack. Even if you switch to Vite or Turbopack, mastering Webpack sharpens your dev edge.
โ Blog by Aelify (ML2AI.com)
๐ Documentation Index