Contents
  1. The Problem Webpack Exists to Solve
  2. What Webpack Actually Is
  3. The Four Core Concepts
  4. Where People Go Wrong
  5. What to Do Now
← All posts

Understanding Webpack

Webpack is a static module bundler. Understanding its four core concepts — entry, output, loaders, and plugins — demystifies every config file you will ever open.

At some point in a JavaScript developer’s life, you open a project, see a file called webpack.config.js and feel a quiet dread. The file has keys you do not recognise, paths that go nowhere obvious, and rules that read like configuration for a machine you have never seen. Most people at that point copy the config from somewhere, confirm it works, and move on without understanding what happened.

That is a reasonable short-term move. It becomes a problem the moment something breaks.

The Problem Webpack Exists to Solve

There are two ways to run JavaScript in a browser. First, include a script for each functionality, but this solution is hard to scale because loading too many scripts can cause a network bottleneck. The second option is to use one large .js file containing all your project code, but this leads to problems in scope, size, readability and maintainability.

Neither option scales. This is why webpack exists. It is a tool that lets you bundle your JavaScript applications, supporting both ESM and CommonJS, and it can be extended to support many different assets such as images, fonts and stylesheets.

What Webpack Actually Is

At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.

The phrase “dependency graph” is the key one. Webpack does not blindly throw files together. It traces what imports what, builds a map of every relationship, and produces an output that only includes what is actually used.

The Four Core Concepts

Everything in webpack traces back to four things.

Entry is where webpack starts reading your application. The entry point instructs the webpack compiler where to start building its internal dependency graph. ./src/index.js is the default entry value.

Output is where the result goes. The output determines where webpack is supposed to emit the bundles it creates and how to name them. ./dist/main.js is the default output value.

Loaders handle the fact that webpack on its own only speaks JavaScript. Loaders are like translators that let webpack work with other types of files, like CSS or images, and turn them into something JavaScript can work with. Without loaders, importing a .css file inside a .js file would fail. With the right loader configured, it works cleanly.

Plugins go further than loaders. While loaders transform certain types of modules, plugins can perform a wider range of tasks, from bundle optimization and minification all the way to defining environment-like variables.

A minimal webpack.config.js that reflects these four concepts:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      { test: /\.css$/, use: 'css-loader' }
    ]
  }
};

Entry, output, one loader rule. That is the minimum. Everything else is an extension of this structure.

Where People Go Wrong

The common mistake is treating webpack as a black box that frameworks configure for you. React’s create-react-app hides the webpack config entirely. Vue CLI does the same. That is convenient until you need to add a custom loader, change the output path, or debug a build error you do not understand because you have never seen the config that produced it.

Webpack is powerful and it has a real configuration cost. If your application is fairly small and you only need to build one JavaScript file to serve to the client, webpack might be more overhead than you need. Know when it is the right tool.

What to Do Now

Install webpack in a blank project and write the config yourself, without a framework scaffolding it for you:

mkdir webpack-test && cd webpack-test
npm init -y
npm install webpack webpack-cli --save-dev

Create src/index.js with any JavaScript you like. Then run:

npx webpack

Check the dist/ folder. Webpack ran with its defaults, no config file needed, and produced a bundle. Now write a webpack.config.js and change where the output goes. Change the filename. Add a rule. Build the intuition deliberately, not accidentally.

The config file will stop looking like noise once you understand the four things it is always describing.

← All posts