Bundlers: Webpack, Rollup, esbuild, and Others
What you'll learn: How modern JavaScript bundlers handle TypeScript files during the build process and why they're different from tsc.
The Bundler Landscape
You've learned that tsc type-checks and transpiles TypeScript files individually—one .ts file becomes one .js file. But modern applications need more: they need all those separate files combined (bundled) into optimized output files that browsers or Node.js can efficiently load.
This is where bundlers come in. A bundler takes your TypeScript files, transpiles them to JavaScript, resolves all imports and dependencies, and packages everything into one or more output files. Think of tsc as a translator working on individual pages, while a bundler is like an editor who translates and combines an entire book into a final published edition.
Popular Bundlers and Their Approaches
Webpack is the veteran powerhouse. It uses ts-loader or babel-loader to handle TypeScript files during its bundling process. Webpack focuses on ultimate flexibility—it can bundle anything (CSS, images, fonts) alongside your TypeScript code. The trade-off? Configuration complexity and slower build times.
Rollup specializes in library bundling. It excels at tree-shaking (removing unused code) and produces cleaner output. For TypeScript, you'd typically use the @rollup/plugin-typescript plugin. Rollup is popular for creating npm packages because it generates multiple module formats efficiently.
esbuild is the speed demon—written in Go, it can transpile TypeScript 10-100x faster than traditional tools. It handles TypeScript directly but with an important caveat: it doesn't type-check, only transpiles. You still need tsc --noEmit running separately to catch type errors.
Others include Parcel (zero-config approach), Vite (uses esbuild under the hood for dev speed), and swc (Rust-based, similar philosophy to esbuild).
The Critical Trade-off
Most modern bundlers prioritize speed by skipping full type-checking during bundling. They strip types and transpile syntax, but don't validate your types. This means your build pipeline typically has two parallel steps: a bundler for fast output generation, and tsc --noEmit for type safety verification.
Key Takeaway: Modern bundlers handle TypeScript transpilation but often skip type-checking for speed; run tsc --noEmit alongside your bundler to maintain type safety while getting fast builds.