Skip to content

How to understand a Webpack-configured app codebase

In webpack apps, a lot of behaviour comes from configuration: which files are bundled, how imports resolve, what gets replaced at build time. Many long-lived codebases split config across common, development and production files merged together.

How to tell it's Webpack-configured app

  • webpack.config.js or a webpack/ folder with webpack.common.js, webpack.dev.js and webpack.prod.js
  • Loaders such as babel-loader, ts-loader and css-loader
  • DefinePlugin, HtmlWebpackPlugin and webpack-merge

Where things usually live

webpack.config.js
Entry points, output, rules, plugins, aliases, dev server.
babel.config.js or .babelrc
Transpilation settings used by babel-loader.
src/index.js
The usual entry; check entry in the config.
public/ or static/
HTML template and static assets.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. entry in the config, which may list several bundles
  2. resolve.alias, to map strange import paths to folders
  3. DefinePlugin, for compile-time constants such as process.env.API_URL

How a request moves through the code

  1. 1Webpack starts from each entry and follows imports.
  2. 2Each file passes through matching loaders in module.rules.
  3. 3Aliases and resolve.extensions decide which file an import points to.
  4. 4Plugins inject constants, generate HTML and split chunks.
  5. 5The dev server serves bundles from memory and proxies API calls.

Questions to ask about the repository

  • What are the entry points, and what does each one bundle?
  • Which aliases exist, and where do they point?
  • What changes between the development and production configs?

What trips people up

  • Constants from DefinePlugin are replaced at build time and don't exist at runtime.
  • Loaders in a rule run from last to first, so their order matters.