My fastest Webpack build shipped 2.9 as much JavaScript
DEV Community

My fastest Webpack build shipped 2.9 as much JavaScript

I ran the same React component through four production Webpack configurations on my Windows machine. The fastest one compiled in 182 ms. The original took 729 ms. All four rendered the greeting I expected.

If I’d stopped at the stopwatch, I would have picked the build that shipped 3,132 bytes of gzipped entry JavaScript. Two of the other configurations shipped 1,082 bytes.

That trade-off is why I built BuildTrial, an open-source CLI for Webpack 5. Version 0.1.0 reported what happened when you switched to an esbuild loader, minifier, or both. In 0.2.0, you can also ask it to keep the smallest passing production build in dist/.

One component, four outcomes

These are medians from three runs per configuration. The warm measurement was an immediate second compilation with the same compiler and no source edits. The verification script loaded the compiled component, rendered it, and checked the resulting HTML.

Strategy Cold compile Warm compile Gzipped entry JS
Existing Webpack config 729 ms 14 ms 1,082 B
Replace the loader 373 ms 14 ms 1,082 B
Replace the minifier 570 ms 15 ms 3,132 B
Replace both 182 ms 15 ms 3,132 B

For this fixture, I’d choose the loader swap for day-to-day builds. It saved 356 ms on a cold compile and produced the same compressed byte count as the baseline. Changing the minifier added 2,050 bytes in both rows where I tried it. That gives me a specific change to investigate.

This is a tiny component. A larger app with split points, shared chunks, CSS, and custom transforms gives the tools a different workload. The useful benchmark is the one you can repeat on your own project.

Try it on a Webpack 5 project

From your project root, using Node.js 20 or later:

npm install -D buildtrial@0.2.0 esbuild-loader@4
npx buildtrial --config webpack.config.cjs --target es2020 --runs 3

Use your actual production config filename and a target that matches your supported browsers or Node runtime. The command above runs the comparison.

To produce files for deployment, add the build subcommand:

npx buildtrial build --config webpack.config.cjs --target es2020

There’s a detail worth knowing if your app uses lazy loading. The report’s entryGzipBytes counts initial JavaScript and CSS. The production build selector sums the gzipped sizes of all emitted JavaScript and CSS, including lazy chunks. Otherwise, moving code out of the entry point could make a candidate look smaller even if the total grew.

The smallest passing candidate wins. Ties favor the baseline when it’s eligible. BuildTrial copies the complete winning output into dist/, including HTML, images, fonts, and declarations when Webpack emits them. Gzip is the measuring stick; the output files keep their normal formats.

A successful build replaces the previous directory. If every candidate fails, the previous dist/ stays in place.

Give “passing” a meaning for your project

One of our test fixtures is a TypeScript SDK whose package manifest promises a declaration file. Swapping out ts-loader still produced working JavaScript, but the new build dropped dist/index.d.ts. BuildTrial rejected that candidate. Checking only the bundle size would have rewarded it for deleting something consumers needed.

BuildTrial checks compilation and declared package outputs under the build directory. To check runtime behavior too, pass a verifier. For example, if your SDK exports a function called double from dist/index.cjs, save this as scripts/check-build.cjs:

const assert = require('node:assert/strict');
const path = require('node:path');
const entry = path.join(process.env.BUILDTRIAL_OUTPUT, 'index.cjs');
const { double } = require(entry);
assert.equal(double(3), 6);
npx buildtrial build --config webpack.config.cjs --target es2020 --verify scripts/check-build.cjs

Read from BUILDTRIAL_OUTPUT so the script tests the current candidate. Hard-coding ./dist could accidentally test yesterday’s successful build four times.

The first thing I’d check on a real app is the feature most likely to break when its loader changes. For a component library, I’d render a public component. For an SDK, I’d import its public entry point the same way a consumer does. What would you put in your verifier?

BuildTrial on npm · Documentation · Source and runnable examples

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.