Bazel rules for esbuild
This is a Bazel rule which wraps the esbuild CLI.
- Same API as the
@bazel/esbuildpackage, so it's easy to migrate.
- Use the Bazel downloader to fetch the npm package and the native binaries as described here:
This means that the toolchain is fully self-contained and hermetic, and doesn't require you to
put esbuild in your package.json. These rules never run
Need help? This ruleset has support provided by https://aspect.dev.
From the release you wish to use:
copy the WORKSPACE snippet into your
From a BUILD file
The simplest usage is with the
If needed, instead of the macro you could call the underlying
esbuild_bundle rule directly.
In a macro
You could write a Bazel macro which uses esbuild, by calling it from a
For this purpose, you can use the
ESBUILD_BIN Make variable exposed by the
@[esbuild repo name]_toolchains//:resolved_toolchain.
This is illustrated in examples/macro.
In a custom rule
The most advanced usage is to write your own custom rule.
This is a good choice if you need to integrate with other Bazel rules via Providers.
You can follow the example of
/esbuild/defs.bzl by re-using the
lib starlark struct exposed by
Note that this is a private API which can change without notice.
You can register your own toolchain to provide an esbuild binary. For example, you could build esbuild from source within the Bazel build, so that you can freely edit or patch esbuild and have those changes immediately reflected. You'll need these things:
- A rule which builds or loads an esbuild binary, for example a
esbuild_toolchainrule which depends on that binary from step 1 as the
toolchainrule which depends on that target from step 2 as its
- A call to the
register_toolchainsfunction in your
WORKSPACEthat refers to the
toolchainrule defined in step 3.