Webpack: Config, Entry & Output
Webpack is the bundler most large existing applications are built with: Create React App, Angular CLI (until v17), Next.js (until Turbopack), and countless in-house setups. It is slower than esbuild and more verbose than Rollup, but it is extremely configurable, and its ideas (loaders, plugins, chunk graphs, a module runtime) shaped every tool after it. If you join a team with a five-year-old frontend, you'll probably meet a webpack.config.js.
The core concepts
| Concept | Meaning |
|---|---|
entry |
where the graph starts; one or several named entries |
output |
where and how files are written (path, filename with [name], [contenthash]) |
mode |
development or production: sets dozens of defaults at once |
module.rules |
loaders: how non-JS files (or JS needing compilation) become modules |
plugins |
hooks into the whole compilation: HTML generation, defines, banners, analysis |
resolve |
how imports are found: extensions, alias, modules |
devtool |
which kind of source map |
target |
web (default), node, browserslist… |
A first build
mkdir -p wp/src && cd wp
npm init -y > /dev/null && npm pkg set type=module
npm install -D webpack@5.112.2 webpack-cli@7.2.3 > /dev/null 2>&1
cat > src/cart.js <<'JS'
export const total = (items) => items.reduce((sum, i) => sum + i.price * i.qty, 0);
export const count = (items) => items.reduce((n, i) => n + i.qty, 0);
JS
cat > src/index.js <<'JS'
import { total } from './cart.js';
const items = [{ price: 4, qty: 2 }, { price: 10, qty: 1 }];
console.log('total:', total(items));
JS
cat > webpack.config.js <<'JS'
import path from 'node:path';
export default {
mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve('dist'),
filename: 'main.js',
clean: true, // empty dist/ before each build
},
};
JS
npx webpack
cat dist/main.js; echo
node dist/main.jsasset main.js 113 bytes [emitted] [minimized] (name: main)
orphan modules 154 bytes [orphan] 1 module
./src/index.js + 1 modules 287 bytes [built] [code generated]
webpack 5.112.2 compiled successfully in 223 ms
(()=>{"use strict";console.log("total:",[{price:4,qty:2},{price:10,qty:1}].reduce((e,t)=>e+t.price*t.qty,0))})();
total: 18
In production mode Webpack concatenated the two modules, tree-shook count, and handed the result to Terser, which went further: total was used once, so it was inlined into the call, and the items constant into that. The output is one expression wrapped in an arrow function. The [orphan] line in the stats means cart.js was folded into index.js (module concatenation) instead of being emitted as a separate module.
Development mode and the runtime
mkdir -p wp/src && cd wp
npm init -y > /dev/null && npm pkg set type=module
npm install -D webpack@5.112.2 webpack-cli@7.2.3 > /dev/null 2>&1
echo 'export const total = (items) => items.reduce((s, i) => s + i.price * i.qty, 0);' > src/cart.js
echo 'import { total } from "./cart.js"; console.log("total:", total([{ price: 4, qty: 2 }]));' > src/index.js
npx webpack --mode development --devtool false --entry ./src/index.js -o dist 2>&1 | grep -v compiled
grep -c '__webpack_require__' dist/main.js
grep -n '"./src/' dist/main.js
node dist/main.jsasset main.js 3.33 KiB [emitted] (name: main)
runtime modules 614 bytes 3 modules
cacheable modules 169 bytes
./src/index.js 89 bytes [built] [code generated]
./src/cart.js 80 bytes [built] [code generated]
11
5:/***/ "./src/cart.js"
81:/* harmony import */ var _cart_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./cart.js */ "./src/cart.js");
total: 8
Without minification you can see Webpack's runtime: a module table keyed by path ("./src/cart.js"), and __webpack_require__ to load modules from it, the same design as the model bundler. The runtime is what lets Webpack support CommonJS and ES modules side by side, lazy chunks, and hot module replacement. CLI flags (--mode, --entry, -o) override or replace the config file.
Several entries, named outputs
mkdir -p wp/src && cd wp
npm init -y > /dev/null && npm pkg set type=module
npm install -D webpack@5.112.2 webpack-cli@7.2.3 > /dev/null 2>&1
echo 'export const money = (n) => "quot; + n.toFixed(2);' > src/format.js
echo 'import { money } from "./format.js"; console.log("shop", money(3));' > src/shop.js
echo 'import { money } from "./format.js"; console.log("admin", money(7));' > src/admin.js
cat > webpack.config.js <<'JS'
import path from 'node:path';
export default {
mode: 'production',
entry: { shop: './src/shop.js', admin: './src/admin.js' },
output: { path: path.resolve('dist'), filename: '[name].bundle.js', clean: true },
resolve: { extensions: ['.js', '.json'], alias: { '@': path.resolve('src') } },
devtool: 'source-map',
};
JS
npx webpack 2>&1 | grep -E '^asset|compiled'
node dist/shop.bundle.js && node dist/admin.bundle.jsasset admin.bundle.js 103 bytes [emitted] [minimized] (name: admin) 1 related asset
asset shop.bundle.js 101 bytes [emitted] [minimized] (name: shop) 1 related asset
webpack 5.112.2 compiled successfully in 226 ms
shop $3.00
admin $7.00
[name] became each entry's key, and devtool: 'source-map' wrote a .map next to each bundle (the "1 related asset" in the stats). format.js is duplicated in both bundles here; sharing it as a separate chunk is a production-build topic.
devtool |
Good for |
|---|---|
false |
no source maps |
eval-cheap-module-source-map |
fast rebuilds in development |
source-map |
full separate maps for production (upload them to your error tracker) |
hidden-source-map |
maps without the //# sourceMappingURL comment, so browsers don't fetch them publicly |
Test yourself
What does setting mode: 'production' do?
- Only enables minification
- Applies a set of defaults: minification with Terser, tree shaking, module concatenation, NODE_ENV=production
- Deploys the bundle
- Disables loaders
What is __webpack_require__?
- Node's require
- Webpack's runtime function that loads modules from the bundle's module table
- A loader
- A plugin hook
With entry: { shop: …, admin: … } and filename: '[name].bundle.js', which files are written?
- main.bundle.js
- shop.bundle.js and admin.bundle.js
- [name].bundle.js
- index.bundle.js