Skip to content

Lesson 9 of 9 · 25m

Migrating Legacy webpack Configs to Vite

Inventory behavior before translating syntax — aliases, env injection, loaders, proxies, HTML generation, and build artifacts each need a deliberate migration decision.

A webpack migration fails when teams try to transliterate webpack.config.js line by line. Vite is not webpack with nicer defaults; it has a different dev architecture. The successful sequence is inventory, classify, translate, verify. You are migrating behavior, not syntax.

Step one: inventory what the old config really does

rg "alias|DefinePlugin|CopyWebpackPlugin|HtmlWebpackPlugin|devServer|loader|resolve" webpack.config.js config/

Classify every hit:

webpack behaviorVite destination
resolve.aliasresolve.alias plus matching TS paths
DefinePlugin / EnvironmentPlugindefine or import.meta.env
loaders for TS/JSX/CSSusually built-in or plugin transform
devServer.proxyserver.proxy
HtmlWebpackPluginplain index.html or plugin/virtual HTML
file copyingpublic/ unless processing is required

Environment variables need a policy change

webpack's process.env.API_URL injection is compile-time replacement. Vite exposes client env through import.meta.env and only exposes VITE_-prefixed variables by default:

VITE_API_BASE_URL=https://api.internal.example npm run dev
const apiBase = import.meta.env.VITE_API_BASE_URL;

This is a security feature. Legacy builds often accidentally ship secrets because any environment variable can be substituted. Use the migration to audit what is genuinely forums.

Common hard cases

Node polyfills. webpack 4 auto-polyfilled buffer, process, and Node builtins. Modern tools do not. Browser libraries depending on them must be replaced or polyfilled deliberately.

Dynamic requires. require("./pages/" + name) is context-module magic. Replace with explicit glob imports:

const pages = import.meta.glob("./pages/*.tsx");

Non-standard assets. A webpack loader chain does not imply an equivalent Vite behavior exists. Decide whether the asset is source, static file, or generated artifact.

Migrate in slices

  1. Build the app with Vite while webpack remains the release path.
  2. Fix aliases, env, proxy, and asset loading.
  3. Compare production behavior behind a staging flag.
  4. Migrate tests and scripts.
  5. Remove webpack only after rollback is boring.

The critical trick is not running both forever. Dual maintenance rots quickly; set a removal date once parity is demonstrated.

Field notes

  • process.env.NODE_ENV compatibility shims are useful temporarily, but do not make them permanent architecture.
  • Watch for case-sensitive imports and deep package imports; strict tools expose old sins.
  • A proxy rewrite that strips /api differently is a migration bug generator — verify actual upstream paths.
  • Measure dev startup and build time after migration, but judge success by artifact behavior and developer trust, not benchmark screenshots.

Was this lesson helpful?

Sign in to rate