Skip to main content
Documentation for @aspect_rules_js@v3.0.3 View source
EXPERIMENTAL: Protobuf and gRPC support for JavaScript and TypeScript. This API is subject to breaking changes outside our usual semver policy. In a future release of rules_js this should become stable.

Typical setup

  1. Choose any code generator plugin for protoc. In this example we’ll show @bufbuild/protoc-gen-es that produces both message marshaling and service stubs for JavaScript and TypeScript. It should be added as a devDependency in your package.json, typically under a /tools directory. The generator is expected to produce .js and .d.ts files for each .proto file.
  2. Declare a binary target that runs the generator, typically using its package_json.bzl entry point, for example in tools/toolchains/BUILD:
  1. Define a js_proto_toolchain that uses the plugin. See the rule documentation below.
  2. Update MODULE.bazel to register it, typically with a simple statement like register_toolchains("//tools/toolchains:all")

Usage

Just write proto_library targets as usual, or have Gazelle generate them. Then reference them anywhere a js_library could appear, for example:
The generator you setup earlier will be invoked automatically as an action to generate the .js and .d.ts files.

Function: js_proto_toolchain

Define a proto_lang_toolchain that uses the plugin. Example:

Parameters

unknown
required
The name of the toolchain. A target named [name]_toolchain is also created, which is the one to be used in register_toolchains.
unknown
required
The NAME of the plugin program, used in command-line flags to protoc, as follows:
protoc --plugin=protoc-gen-NAME=path/to/mybinary --NAME_out=OUT_DIR
See https://protobuf.dev/reference/cpp/api-docs/google.protobuf.compiler.plugin
unknown
required
(List of strings) Command line flags used to invoke the plugin, based on documentation for the generator.For example, for @bufbuild/protoc-gen-es, reference the documentation at https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#plugin-options to arrive at a value like ["import_extension=js"]
unknown
required
The plugin to use. This should be the label of a binary target that you declared in step 2 above.
unknown
required
The runtime to use, which is imported by the generated code. For example, ”//:node_modules/@bufbuild/protobuf”.Note that node module resolution requires the runtime to be in a parent folder of any package containing generated code.
unknown
Additional arguments to pass to the proto_lang_toolchain rule.