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.jsor awebpack/folder withwebpack.common.js,webpack.dev.jsandwebpack.prod.js- Loaders such as
babel-loader,ts-loaderandcss-loader DefinePlugin,HtmlWebpackPluginandwebpack-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
entryin 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
entryin the config, which may list several bundlesresolve.alias, to map strange import paths to foldersDefinePlugin, for compile-time constants such asprocess.env.API_URL
How a request moves through the code
- Webpack starts from each entry and follows imports.
- Each file passes through matching loaders in
module.rules. - Aliases and
resolve.extensionsdecide which file an import points to. - Plugins inject constants, generate HTML and split chunks.
- The 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
DefinePluginare replaced at build time and don't exist at runtime. - Loaders in a rule run from last to first, so their order matters.