Webpack 5 Config Cheat Sheet
Quick reference guide for Webpack 5: Entry/output, loaders, plugins, code splitting, and dev server.
Build Tools
webpack
bundler
javascript
Webpack is a static module bundler for modern JavaScript applications that builds a dependency graph mapping every module your project needs.
Configuration Structure Example (`webpack.config.js`)
javascript
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
mode: "production",
entry: "./src/index.ts",
output: {
filename: "[name].[contenthash].js",
path: path.resolve(__dirname, "dist"),
clean: true
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
plugins: [
new HtmlWebpackPlugin({ template: "./public/index.html" }),
new MiniCssExtractPlugin({ filename: "[name].[contenthash].css" })
]
};
Essential Core Concepts
Table
| Concept | Purpose | Configuration Key |
|---|---|---|
| Entry | Point where Webpack starts building internal dependency graph | entry: './src/index.js' |
| Output | Tells Webpack where to emit the bundled assets and files | output: { path, filename } |
| Loaders | Transform non-JS files (CSS, TypeScript, Images) into valid modules | module: { rules: [...] } |
| Plugins | Perform wide-range tasks like bundle optimization, asset management | plugins: [...] |
| Optimization | Configure code splitting (splitChunks) and minification | optimization: { splitChunks } |
Essential CLI Commands
Table
| Command | Action / Purpose |
|---|---|
npx webpack | Build project using default webpack.config.js |
npx webpack --mode=development | Run build in development mode with sourcemaps |
npx webpack serve | Start local webpack-dev-server with HMR enabled |
Common Pitfalls & Tips
[!WARNING] Webpack loader order matters! Rules in
use: ['style-loader', 'css-loader', 'sass-loader']are evaluated right-to-left (or bottom-to-top).
[!TIP] Use
output.contenthashin production filenames ([name].[contenthash].js) to enforce aggressive browser caching with automatic cache-busting.