Upgrading Webpack 4 to 5

An overview of upgrading from Webpack 4 to 5, including benefits, key changes, upgrade steps, and references to help you upgrade efficiently.

Mar 23, 2021 · 1 min read · 189 Words · -Views -Comments · Programming
Build Tools 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

It’s been about five months since Webpack 5 was released, and most of the ecosystem plugins are ready. With some spare time, I started the project upgrade.

Key Changes in Webpack 5

  1. Improved persistent caching for faster builds
  2. Better tree-shaking, reducing bundle size to some extent
  3. Several breaking changes to prepare for future evolution

In short, Webpack is a build tool and won’t affect the runtime functionality of your bundled JS. It’s worth upgrading.

Upgrade Steps

For full details, refer to the official docs. Here are the important steps:

  1. Update Webpack and plugins to the latest versions

    Recommend using yarn upgrade-interactive to select and update in bulk—more efficient.

  2. Adjust some settings

    • HtmlWebpackPlugin: the chunksSortMode option changed; set it to auto or similar
    • webpack-merge: merge is no longer the default export
    • CLI: use webpack-cli serve instead of the webpack-dev-server command
  3. If your frontend code depends on Node-specific packages, either remove the dependency or install and configure browser-ready alternatives.

    • For example, my project used content-disposition. I rewrote it with browser JS instead.

Final Thoughts

  • The upgrade is fairly straightforward with only a handful of changes, though there are a few gotchas.
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover