跪拜 Guibai
← All articles
Frontend

Vite vs Webpack: How They Serve Modules Differently and When to Use Each

By Kevin666 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Picking the wrong build tool for a React+TypeScript project costs you development speed and maintenance effort. Vite's on-demand module serving makes it the default for new projects, while Webpack remains the right call for existing complex setups that would be too expensive to migrate.

Summary

Vite and Webpack both turn source files into deployable assets, but they diverge sharply in development mode. Webpack analyzes module dependencies, transforms them through loaders, and produces a bundle before the browser loads anything. Vite skips that upfront bundling: the browser requests modules directly over native ES modules, and Vite transforms and returns only what's needed. That on-demand model gives Vite faster cold starts and hot updates on large projects.

The article walks through creating a React+TypeScript project with Vite, configuring the dev server and an API proxy, and building for production. It also covers Webpack's core concepts, entry and output, and how to read a basic webpack.config.js. A table clarifies the roles of Webpack, Vite, and Babel: Babel only converts syntax like JSX, it isn't a bundler.

The recommendation is clear: start new React+TypeScript projects with Vite, but don't migrate a working Webpack project just to switch. Both tools still need full bundling and optimization in production. For business developers, the article says you don't need to study bundler internals; knowing how to start the dev server, wire a proxy, and deploy is enough.

Takeaways
— Webpack bundles the entire dependency graph before the browser loads anything; Vite serves modules on demand via native ES modules in development.
— Vite's production build still bundles and optimizes assets; it doesn't skip that step.
— Vite 8 uses Rolldown as its unified build toolchain; earlier versions used Rollup for production builds.
— A basic Vite project with the react-ts template is created with `npm create vite@latest my-app -- --template react-ts`.
— In vite.config.ts, the `server.proxy` option forwards requests like `/api` to a backend, e.g., `http://localhost:8080`.
— A minimal webpack.config.js sets `mode`, `entry`, and `output` to produce a bundle like `dist/bundle.js`.
— Babel transforms JSX and modern JavaScript syntax; it is not a bundler.
— For new React+TypeScript projects, Vite is recommended; existing Webpack projects shouldn't be migrated without evaluating plugins, build pipeline, and maintenance cost.
— The article's priority table puts Vite vs Webpack, ESM, npm scripts, vite.config.ts, and proxy at P0, with loaders/plugins and HMR at P1, and custom plugins at P2.
Conclusions

The article's advice to prefer Vite for new projects but avoid migrating existing Webpack ones suggests both tools will coexist for years, so teams need fluency in both rather than a single winner.

The P0 learning list consists entirely of operational tasks like starting the dev server and configuring proxy, which implies that for most business developers, build tooling is something to operate, not to rebuild from scratch.

The repeated warning that Vite still bundles in production counters a common misconception that Vite is a no-bundle tool; the performance advantage is confined to development mode.

Concepts & terms
ESM (ES Module)
JavaScript's native module system that lets you import and export code between files. Browsers can load these directly, which is why Vite can serve modules on demand instead of pre-bundling everything.
HMR (Hot Module Replacement)
A development feature that updates changed modules in the browser without a full page reload, preserving application state. Vite and Webpack both support it.
Tree Shaking
A build-time optimization that removes unused exported code from the final bundle, shrinking file size.
Code Splitting
A technique that splits the bundle into smaller pieces loaded on demand, reducing initial load time.
Rolldown
A Rust-based bundler that Vite 8 uses as its unified build toolchain, replacing Rollup for production builds.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗