Webpack Tutorial
Learn Webpack step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Webpack? Learn what Webpack is, which problem a module bundler solves, how the dependency graph works and when to use Webpack instead of plain script tags or newer tools.
- Install Webpack and Build Your First Bundle Install Webpack and webpack-cli with npm, write a tiny project, add npm scripts, run your first build and read the dist output and the build summary line by line.
- The Webpack Configuration File Understand webpack.config.js: its main keys, CommonJS versus ESM and TypeScript configs, exporting a function or array, passing env flags and validating your configuration.
- Build Modes: Development, Production and None Learn what Webpack's mode option changes: minification, tree shaking, process.env.NODE_ENV, source maps and module names, and how to choose between development, production and none.
- How a Bundler Works Build a tiny module bundler in Node.js to see exactly how Webpack works: dependency graph, module wrapper, the require runtime, circular imports and a tree shaking sketch.
Entry, Output and Loaders
- Entry Points Learn Webpack's entry option: string, array and object forms, named entries for multi-page apps, entry descriptors with dependOn, dynamic entries and the context folder.
- Output and Filenames Control where Webpack writes files and how they are named: output.path, filename templates, [contenthash], publicPath, clean, chunkFilename and building a library.
- Loaders Basics Learn how Webpack loaders turn CSS, images, TypeScript and any file into modules: module.rules, test and include, loader chains, options, and how to write a tiny custom loader.
- CSS and Style Loaders Import CSS in Webpack with css-loader and style-loader, add Sass and PostCSS, use CSS Modules for scoped class names and understand importLoaders, url() handling and the production alternative.
- Babel and TypeScript Loaders Compile modern JavaScript, JSX and TypeScript in Webpack with babel-loader, preset-env and browserslist, ts-loader or swc-loader, type checking with a separate plugin, and build caching.
- Resolve, Extensions and Aliases Understand how Webpack finds modules: resolve.extensions, alias, modules, mainFields, conditionNames, package exports, symlinks and fixes for the common Module not found error.
Plugins and Assets
- HTML Webpack Plugin Use HtmlWebpackPlugin to generate HTML that includes your hashed bundles automatically: templates, title, chunks, multiple pages, minification, favicon and common mistakes.
- MiniCssExtractPlugin Move CSS out of JavaScript into real .css files with MiniCssExtractPlugin, switch between style-loader and the plugin by mode, add CssMinimizerPlugin, hash names and split CSS by chunk.
- Asset Modules: Images, Fonts and Files Handle images, fonts, SVG and text files with Webpack 5 asset modules: asset/resource, asset/inline, asset/source and the automatic asset type, size limits, generator filenames and CSS url().
- Define, Copy and Clean Plugins Use everyday Webpack plugins: DefinePlugin and EnvironmentPlugin for build-time constants, dotenv for env files, CopyWebpackPlugin for static files, ProvidePlugin, and output.clean.
- Writing a Webpack Plugin Learn how Webpack plugins work and write your own: the plugin class and apply method, compiler and compilation hooks, tapable, emitting a file, processAssets, options and testing ideas.
Dev Server and HMR
- Webpack Dev Server Run webpack serve with webpack-dev-server: install and configure port, host, open, static, client overlay and compression, understand in-memory builds and how live reload works.
- Hot Module Replacement Understand Hot Module Replacement in Webpack: how updates bubble through module.hot.accept, writing accept and dispose handlers, CSS hot updates, framework refresh and common pitfalls.
- Source Maps Debug bundled code with Webpack source maps: what a source map is, how mappings work, choosing devtool values for development and production, hidden maps, error tracking and security.
- Proxy and History Fallback Fix CORS and deep-link 404s in development: configure devServer.proxy for API calls, pathRewrite, WebSocket proxying, historyApiFallback for single-page apps, headers and HTTPS.
Code Splitting and Lazy Loading
- Code Splitting Learn webpack code splitting: why one giant bundle hurts, how entry points, dynamic import() and splitChunks create chunks, and how to read the chunk graph.
- Dynamic Imports and Lazy Loading Master webpack dynamic import(): lazy-load routes and heavy features, name chunks with magic comments, use prefetch and preload, and handle loading errors.
- The SplitChunksPlugin Configure webpack optimization.splitChunks: chunks, minSize, minChunks, cacheGroups for vendor code, naming, runtimeChunk, and how to avoid too many requests.
Optimization and Caching
- Production Mode and Minification Learn what webpack mode production switches on: minification with Terser, DefinePlugin constants, dead-code removal, CSS minification and the right devtool setting.
- Tree Shaking and Side Effects Understand webpack tree shaking: how usedExports and the sideEffects flag remove unused code, why ES modules matter, and the mistakes that keep dead code in your bundle.
- Long-Term Caching with Content Hashes Make returning visitors download less: webpack contenthash file names, deterministic module ids, runtimeChunk, cache headers, and the persistent build cache.
- Bundle Analysis and Performance Budgets Find what bloats your webpack bundle: stats.json, webpack-bundle-analyzer, performance hints and budgets, duplicate packages, and a repeatable size-reduction routine.
Webpack with Frameworks
- Webpack with React Set up webpack for a React app: babel-loader with preset-react, the automatic JSX runtime, CSS, React Fast Refresh, React.lazy code splitting and a production config.
- Webpack with Vue and Svelte Configure webpack for Vue single-file components with vue-loader and VueLoaderPlugin, and for Svelte with svelte-loader. Learn how SFC blocks are split, styled and hot reloaded.
- Webpack with TypeScript Compile TypeScript with webpack using ts-loader, babel-loader or swc-loader, set up tsconfig for bundlers, run type checks in parallel with fork-ts-checker, and resolve path aliases.
- Module Federation Learn webpack 5 Module Federation: hosts, remotes, exposes, shared dependencies and version negotiation, with a config walkthrough and the pitfalls of micro-frontends.
Migration and Alternatives
- Upgrading from Webpack 4 to 5 A step-by-step migration guide from webpack 4 to 5: asset modules replace file-loader, Node polyfills are gone, persistent caching, new ids and the removed options.
- Webpack vs Vite, Rollup, esbuild and Rspack Compare webpack with Vite, Rollup, esbuild, Rspack, Parcel and Turbopack: how each works, where each is strongest, and a practical decision guide for new and existing projects.
- Migrating from Webpack to Vite Move a webpack project to Vite step by step: entry HTML, aliases, env variables, DefinePlugin, dev server proxy, asset imports, require calls and CommonJS gotchas.
Projects and Practice
- Project: Build a Mini Bundler Project: write a tiny JavaScript bundler in Node.js that reads a dependency graph, wraps modules in a runtime and shakes unused exports. A hands-on way to understand webpack.
- Project: A Production-Ready Webpack Config Project: assemble a complete, production-ready webpack 5 setup with common, dev and prod configs, merging, content hashes, split chunks, CSS extraction, budgets and a config audit.
- Project: A Lazy-Loaded Dashboard Project: design a dashboard whose routes and widgets load on demand with webpack dynamic imports, hover prefetch, loading and error states, retries and a size budget per route.
- Troubleshooting and Practice Fix common webpack errors fast: module not found, loader and export errors, HMR and cache problems, slow builds and oversized bundles, plus practice exercises and interview questions.