man@mndev:~/portfolio/notes/frontend
← portfolio

Webpack 4 to 5: notes from a security-driven migration

Security findings forced a bundler upgrade. What broke, what changed, and how to make the jump without a big-bang rewrite.

Frontend··1 min read

Nobody plans a bundler upgrade for fun. Mine came from a list of software composition analysis (SCA) findings: vulnerable transitive dependencies that couldn’t be patched while the project was pinned to Webpack 4 and its loader ecosystem. Out of 35 findings on that project, we closed 33. The Webpack 5 migration unlocked a large share of them.

What actually breaks

1. Node.js core polyfills are gone. Webpack 4 silently bundled browser shims for buffer, crypto, stream, path and friends. Webpack 5 doesn’t. Builds fail with “Module not found”. You can either:

// webpack.config.js
resolve: {
  fallback: {
    buffer: require.resolve('buffer/'),
    stream: require.resolve('stream-browserify'),
    crypto: false, // better: remove the browser dependency on it
  },
},

Treat each fallback as a question: why is browser code importing a Node module? Often the answer is a library you can replace or drop.

2. Asset loaders change. file-loader, url-loader and raw-loader are replaced by built-in asset modules:

{ test: /\.(png|svg|jpg)$/i, type: 'asset' } // inline small, emit large

3. Plugins and loaders need majors too. Upgrade them together and read each changelog. This is where most of the time goes.

How I’d sequence it

  1. Upgrade the build tooling only: no app code changes, no “while I’m here” refactors.
  2. Get a green build, then compare bundle output (size per chunk) against the old build.
  3. Run the full regression suite plus a visual smoke test of key pages.
  4. Re-run the SCA scan and confirm the findings are actually gone.
  5. Ship behind a normal release. Keep the diff easy to revert.

Bonus wins

Security work often feels like chores. Here it bought a faster build and a codebase that’s cheaper to keep patched.

// related