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 behavior | Vite destination |
|---|---|
resolve.alias | resolve.alias plus matching TS paths |
DefinePlugin / EnvironmentPlugin | define or import.meta.env |
| loaders for TS/JSX/CSS | usually built-in or plugin transform |
devServer.proxy | server.proxy |
HtmlWebpackPlugin | plain index.html or plugin/virtual HTML |
| file copying | public/ 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 devconst 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
- Build the app with Vite while webpack remains the release path.
- Fix aliases, env, proxy, and asset loading.
- Compare production behavior behind a staging flag.
- Migrate tests and scripts.
- 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_ENVcompatibility 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
/apidifferently 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?