esbuild
esbuild is an extremely fast JavaScript and CSS bundler and minifier. The current stable version is v0.28.0. The project maintains an active release schedule, frequently delivering patch updates and minor versions (e.g., v0.27.x, v0.28.x) within short periods, sometimes multiple per month, indicating rapid development and responsiveness to issues.
Common errors
-
SyntaxError: Class field initializers are not supported by the current target environment
cause TypeScript parameter properties were incorrectly lowered for target environments that don't support class fields in esbuild versions 0.27.0-0.27.6.fixUpgrade esbuild to version `0.27.7` or newer. -
[ERROR] Expected "and", "or", or end of input but found "{"cause A regression in esbuild (v0.25.11 - v0.27.3) caused incorrect parsing of CSS media queries with `or` clauses, leading to invalid CSS.fixUpgrade esbuild to version `0.27.4` or newer. -
ReferenceError: [variable_name] is not defined
cause A bug in the bundler (prior to v0.27.1) incorrectly handled `var` declarations nested inside `if` statements when importing ES modules via `require`, leading to hoisting issues.fixUpgrade esbuild to version `0.27.1` or newer.
Warnings
- breaking esbuild v0.27.0 introduced deliberate backwards-incompatible changes. Users are strongly advised to pin exact versions or use patch-only version ranges to prevent unexpected upgrades.
- gotcha Prior to v0.27.7, esbuild might incorrectly generate class fields for TypeScript parameter properties in target environments that don't support them, leading to runtime errors.
- gotcha esbuild versions from 0.25.11 to 0.27.3 had a regression in CSS media query parsing, specifically failing to correctly wrap `or` clauses with parentheses, resulting in malformed CSS output.
- gotcha esbuild v0.25.11 introduced a minification regression where duplicate CSS media rules were not correctly removed, especially for rules with `<media-type> and <media-condition-without-or>` grammar.
Install
-
npm install esbuild -
yarn add esbuild -
pnpm add esbuild
Imports
- build
import { build } from 'esbuild'
Quickstart
import { build } from 'esbuild';
const entryPoint = 'src/index.ts';
const outFile = 'dist/bundle.js';
await build({
entryPoints: [entryPoint],
bundle: true,
outfile: outFile,
platform: 'node',
target: 'es2022'
}).then(() => {
console.log(`Successfully bundled ${entryPoint} to ${outFile}`);
}).catch(() => {
process.exit(1);
});