How to Choose Between esbuild and Rollup
Use esbuild for raw speed and Rollup for optimised output, and combine them so fast development does not cost you bundle quality.
On this page
Introduction
Two bundlers dominate the JavaScript build toolchain, and they optimise for different things. esbuild is written in Go and is one or two orders of magnitude faster than a JavaScript bundler, which transforms the development feedback loop. Rollup produces tighter output, with better tree shaking and smaller bundles, at the cost of speed. Choosing between them is not about which is better but about which phase of the workflow you are optimising, and the practical answer for many projects is to use both: esbuild during development, Rollup for the production bundle.
Two Roles: Speed and Optimisation
The development loop runs the bundler on every save, so its speed dominates the feedback the developer feels. The production build runs once and then serves every user, so its output size dominates the cost the user feels. The two phases have opposite priorities, and a bundler tuned for one is not ideal for the other.
esbuild is fast because it is compiled, parallel, and does less: fewer optimisations, less plugin overhead per module, a simpler pipeline. Rollup is slower, partly because it is JavaScript and partly because it does more: accurate tree shaking, better handling of side effects, and output that is smaller. Recognising the trade-off lets you pick per phase instead of forcing one tool to do both.
esbuild for Development Speed
In development, use esbuild for its speed and its incremental rebuild. It can transform TypeScript and JSX, bundle, and serve, all fast enough that the rebuild is imperceptible. The correctness you need in development is that the code runs and the error is accurate; you do not need the smallest possible bundle. Use source maps so a stack trace points at your source, and accept that the output is larger than production.
// esbuild: fast dev build with source maps and no minification
require('esbuild').context({
entryPoints: ['src/index.tsx'],
bundle: true,
outfile: 'dist/dev.js',
platform: 'browser',
sourcemap: 'inline',
minify: false,
define: { 'process.env.NODE_ENV': '"development"' },
loader: { '.png': 'dataurl' },
}).then((ctx) => ctx.watch()); // incremental rebuild on change
Rollup for Output Quality
For production, use Rollup for its tree shaking and code splitting. It analyses the module graph to drop unused exports, splits shared code into chunks that the browser can cache, and lets plugins refine the output. The result is a bundle the audience downloads once, so the extra build time is paid back in reduced transfer.
// rollup.config.js: production build with tree shaking and chunks
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
export default {
input: 'src/index.js',
output: {
dir: 'dist',
format: 'esm',
sourcemap: true,
manualChunks: { vendor: ['react', 'react-dom'] }, // cacheable chunk
},
plugins: [
nodeResolve({ browser: true }),
commonjs(),
terser({ compress: { passes: 2 } }),
],
treeshake: { moduleSideEffects: false }, // aggressive shaking
};
Combining Them
The common combination is esbuild as the transformer and Rollup as the bundler: a Rollup plugin runs each module through esbuild’s fast TypeScript and JSX transform, then Rollup does the graph analysis and output. This gets most of the speed for the transform step and all of the output quality for the bundling step. Vite popularised this arrangement for development, using esbuild to pre-bundle dependencies and serve source fast, and Rollup for the production build.
If you adopt one tool only, pick based on the constraint you feel most. If slow rebuilds are the pain, esbuild. If the bundle is too large, Rollup. Most projects that grow past a prototype benefit from the hybrid, because both pains appear as the codebase and the audience grow.
Feature Comparison
Tree shaking: Rollup is more precise, especially around side effects and re-exports. Code splitting: both support it; Rollup gives more control over chunking. Plugins: Rollup has the larger ecosystem because it has been the bundler of libraries for years. TypeScript: both transform, but neither type-checks; run tsc separately. Minification: both minify; esbuild’s is fast, terser’s is often smaller. Source maps: both support them, with different fidelity trade-offs.
A practical rule: build libraries with Rollup for the cleanest output and best tree shaking for consumers, and build applications with a hybrid or with the bundler whose trade-off matches the app’s constraint. Test the actual output rather than assuming, because the differences vary with the code.
Choosing in Practice
Measure before you choose. Build the same project with both and compare the output size, the build time, and the correctness of tree shaking on your real imports. The generic advice is a starting point; your dependency graph decides. Watch for a dependency that does not tree shake well, because that can dominate the difference.
Decide with the whole pipeline in mind: a fast bundler with a slow type-check or a slow minifier does not deliver the fast loop. Configure the type-check to run in parallel with the build, and use the fast minifier in development. The goal is a fast feedback loop and a small production bundle, and the tools are a means to those two ends.
Conclusion
esbuild and Rollup optimise for different phases: esbuild for the speed a developer feels on every save, Rollup for the output size a user feels on every load. Use esbuild for development and Rollup for production, or combine them with a Rollup plugin that runs esbuild’s transform. Measure the real output on your dependency graph instead of trusting generic advice, and keep the type-check and minification from negating the speed. Revisit the choice when a dependency changes, because a single library that does not tree shake well can shift which bundler produces the smaller bundle. Then the feedback loop is fast and the bundle is small, which is what each tool is good at.
Sources: esbuild documentation, Rollup documentation, Vite: why the esbuild/Rollup split.
FAQ
- Does esbuild or Rollup type-check TypeScript?
- Neither. Both strip and transform types but do not verify them. Run tsc --noEmit in parallel with the build to type-check, so a build with type errors still fails without slowing the bundler.
- Why is Rollup output often smaller than esbuild output?
- Rollup has more precise tree shaking, especially around side effects and re-exports, and more control over chunking. esbuild trades some of that precision for speed, which is the right trade in development and often not in production.
Report a problem with this articleOpens a pre-filled issue on GitHub.