🌐 Web Development

esbuild

esbuild

An ultra-fast JavaScript bundler written in Go. A core tool of Vite.

Detailed Explanation

esbuild is an ultra-fast JavaScript/TypeScript bundler written in the Go language. It was developed by Evan Wallace (CTO of Figma) and boasts speeds 10 to 100 times faster than existing bundlers.

Unlike existing bundlers written in JavaScript (Webpack, Rollup), it builds even large-scale projects in an instant thanks to Go's parallel processing and efficient memory usage. It is also well known that Vite uses esbuild in development mode.

However, its plugin ecosystem is not as rich as Webpack's, so it is mainly used for development mode as in Vite or for simple bundling.

Code Example

Using the CLI

# Install npm install esbuild # Bundle a single file npx esbuild app.js --bundle --outfile=out.js # Production build (minify + sourcemap) npx esbuild src/index.ts --bundle --minify --sourcemap --outfile=dist/bundle.js # TypeScript + React JSX npx esbuild app.tsx --bundle --loader:.tsx=tsx --outfile=out.js

Using the JavaScript API

import * as esbuild from 'esbuild'; // Basic bundling await esbuild.build({ entryPoints: ['src/index.ts'], bundle: true, minify: true, sourcemap: true, target: ['es2020'], outfile: 'dist/bundle.js', }); // Code splitting (multiple chunks) await esbuild.build({ entryPoints: ['src/index.ts'], bundle: true, splitting: true, format: 'esm', outdir: 'dist', });

Watch Mode (for development)

const ctx = await esbuild.context({ entryPoints: ['src/index.ts'], bundle: true, outfile: 'dist/bundle.js', }); // Watch for file changes and rebuild automatically await ctx.watch(); // Development server (optional) await ctx.serve({ servedir: 'public', port: 3000, });

Practical Conversation Example

Developer

"The build takes 30 seconds. What if we apply esbuild?"

Senior

"esbuild itself is fast, but can it replace all of Webpack's complex configuration? Things like handling CSS modules or environment variables."

Developer

"That's a bit limited. So I think using Vite would be better. It uses esbuild for fast development mode and Rollup for stable production builds."

Cautions

Limited plugin ecosystem

It does not have a plugin ecosystem as rich as Webpack's. If you need complex loaders or plugins, you need to consider other tools.

No ES5 target support

Only ES6+ and above can be targeted. If you need to support legacy browsers such as IE11, additional transpiling is required.

No TypeScript type checking

It only transpiles and does not perform type checking. Type checking must be performed separately with tsc or in an IDE.

Related Terms

Learn More