Course contentsShow
← Web Development
Lesson 1397 of 2,98225. Build Tools and BundlersFree lesson

Webpack Configuration File Structure

Organizing webpack.config.js with common patterns and splitting configs for dev and production.

Webpack Configuration File Structure

You'll learn how to organize your webpack configuration for clarity, maintainability, and environment-specific builds.

As your webpack setup grows beyond basics, your webpack.config.js can become unwieldy. A single monolithic config file mixing development and production settings creates confusion and makes changes risky. The solution is organizing your configuration with common patterns and splitting configs by environment.

Common Organizational Patterns

The most popular approach uses three separate config files:

  • webpack.common.js — shared settings (entry, loaders, plugins used everywhere)
  • webpack.dev.js — development-only settings (dev server, source maps)
  • webpack.prod.js — production-only settings (minification, optimizations)

Think of it like a restaurant kitchen: common equipment everyone uses stays in the main area, but breakfast and dinner shifts have their own specialized tools.

Merging Configurations

A utility called webpack-merge combines these files:

// webpack.dev.js
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'inline-source-map',
  // dev-specific settings
});

You then run builds pointing to the specific config: webpack --config webpack.dev.js or webpack --config webpack.prod.js.

Why This Matters

Splitting configurations prevents accidentally shipping development code to production, makes environment differences explicit, and reduces file size. You modify dev settings without touching production, and vice versa. The common file becomes your single source of truth for shared logic.

Key Takeaway: Organize webpack configs by splitting common, development, and production settings into separate files, then merge them using webpack-merge for cleaner, safer builds.